Skip to content

Commit be20d78

Browse files
AB-xdevJohannesRabauerlcarrasco-xdev
committed
Init
Copied from original Co-Authored-By: Johannes Rabauer <[email protected]> Co-Authored-By: Luis <[email protected]>
1 parent 33817de commit be20d78

25 files changed

+3564
-0
lines changed

vaadin-grid-filter/src/main/java/software/xdev/vaadin/FilterComponent.java

Lines changed: 1408 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.builder;
17+
18+
import com.vaadin.flow.function.ValueProvider;
19+
import software.xdev.vaadin.model.FilterField;
20+
import software.xdev.vaadin.model.FilterFieldEnumExtension;
21+
import software.xdev.vaadin.model.FilterProvider;
22+
23+
import java.time.LocalDate;
24+
import java.time.LocalDateTime;
25+
import java.util.ArrayList;
26+
import java.util.Objects;
27+
28+
/**
29+
* Used to create a customizable FilterField.
30+
*
31+
* @see FilterField
32+
*/
33+
public final class CustomizableFilterBuilder
34+
{
35+
private CustomizableFilterBuilder()
36+
{
37+
}
38+
39+
public static CustomizableFilterBuilder builder()
40+
{
41+
return new CustomizableFilterBuilder();
42+
}
43+
44+
45+
/**
46+
* Is used to create a new field for filtering.
47+
*
48+
* @param provider The field of the bean with type String.
49+
* @param description Is used to set the description of the selectable field.
50+
* @param <T> Type of the field.
51+
* @return Returns a field for filtering.
52+
*/
53+
public <T> FilterField<T, String> withValueProvider(final FilterProvider.StringProvider<T> provider,
54+
final String description)
55+
{
56+
return this.withValueProvider(provider, description, String.class);
57+
}
58+
59+
/**
60+
* Is used to create a new field for filtering.
61+
*
62+
* @param provider The field of the bean with type Number.
63+
* @param description Is used to set the description of the selectable field.
64+
* @param <T> Type of the field.
65+
* @return Returns a field for filtering.
66+
*/
67+
public <T> FilterField<T, Number> withValueProvider(final FilterProvider.NumberProvider<T> provider,
68+
final String description)
69+
{
70+
return this.withValueProvider(provider, description, Number.class);
71+
}
72+
73+
/**
74+
* Is used to create a new field for filtering.
75+
*
76+
* @param provider The field of the bean with type LocalDate.
77+
* @param description Is used to set the description of the selectable field.
78+
* @param <T> Type of the field.
79+
* @return Returns a field for filtering.
80+
*/
81+
public <T> FilterField<T, LocalDate> withValueProvider(
82+
final FilterProvider.LocalDateProvider<T> provider,
83+
final String description)
84+
{
85+
return this.withValueProvider(provider, description, LocalDate.class);
86+
}
87+
88+
/**
89+
* Is used to create a new field for filtering.
90+
*
91+
* @param provider The field of the bean with type LocalDateTime.
92+
* @param description Is used to set the description of the selectable field.
93+
* @param <T> Type of the field.
94+
* @return Returns a field for filtering.
95+
*/
96+
public <T> FilterField<T, LocalDateTime> withValueProvider(
97+
final FilterProvider.LocalDateTimeProvider<T> provider,
98+
final String description)
99+
{
100+
return this.withValueProvider(provider, description, LocalDateTime.class);
101+
}
102+
103+
/**
104+
* Is used to create a new field for filtering.
105+
*
106+
* @param provider The field of the bean with type Boolean.
107+
* @param description Is used to set the description of the selectable field.
108+
* @param <T> Type of the field.
109+
* @return Returns a field for filtering.
110+
*/
111+
public <T> FilterField<T, Boolean> withValueProvider(
112+
final FilterProvider.BooleanProvider<T> provider,
113+
final String description)
114+
{
115+
return this.withValueProvider(provider, description, Boolean.class);
116+
}
117+
118+
/**
119+
* Is used to create a new field for filtering.
120+
*
121+
* @param provider The field of the bean with type Enum.
122+
* @param description Is used to set the description of the selectable field.
123+
* @param <T> Type of the field.
124+
* @return Returns a field for filtering.
125+
*/
126+
public <T> FilterField<T, Enum> withValueProvider(final FilterProvider.EnumProvider<T> provider,
127+
final String description,
128+
final Enum<?>[] enumValues)
129+
{
130+
return new FilterFieldEnumExtension<>(provider, description, Enum.class, new ArrayList<>(), enumValues);
131+
}
132+
133+
/**
134+
* Is used to create a new field for filtering.
135+
*
136+
* @param provider The field of the bean.
137+
* @param description The name of the selectable field.
138+
* @param type The class of the field.
139+
* @param <T> The bean.
140+
* @param <X> The type of the field.
141+
* @return Returns a new filterField.
142+
*/
143+
public <T, X> FilterField<T, X> withValueProvider(final ValueProvider<T, X> provider,
144+
final String description,
145+
final Class<X> type)
146+
{
147+
Objects.requireNonNull(provider);
148+
Objects.requireNonNull(description);
149+
150+
return new FilterField<>(provider, description, type, new ArrayList<>());
151+
}
152+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.comparators;
17+
18+
19+
import java.util.function.Predicate;
20+
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import com.vaadin.flow.function.ValueProvider;
25+
26+
import software.xdev.vaadin.comparators.utl.TypeHelper;
27+
28+
/**
29+
* Used for comparison with contains.
30+
*/
31+
public final class ContainsComparator implements FilterComparator
32+
{
33+
private static final Logger LOGGER = LoggerFactory.getLogger(ContainsComparator.class);
34+
private static ContainsComparator instance;
35+
36+
private ContainsComparator()
37+
{
38+
}
39+
40+
public static ContainsComparator getInstance()
41+
{
42+
if (instance == null)
43+
{
44+
instance = new ContainsComparator();
45+
}
46+
47+
return instance;
48+
}
49+
50+
@Override
51+
public String getDescription()
52+
{
53+
return "contains";
54+
}
55+
56+
@Override
57+
public boolean isApplicable(final Class<?> clazz)
58+
{
59+
return String.class.isAssignableFrom(clazz)
60+
|| Number.class.isAssignableFrom(clazz)
61+
|| Enum.class.isAssignableFrom(clazz);
62+
}
63+
64+
@Override
65+
public <B, T> Predicate<B> compare(final ValueProvider<B, T> provider, final String searchQuery)
66+
{
67+
LOGGER.debug("Checking if the item contains {}", searchQuery);
68+
69+
return item ->
70+
{
71+
final T apply = provider.apply(item);
72+
73+
TypeHelper.checkIfTypeIsApplicable(this, apply.getClass());
74+
75+
if (apply instanceof final String strValue)
76+
{
77+
LOGGER.debug("Item is an instance of String.");
78+
return strValue.contains(searchQuery);
79+
}
80+
81+
if (apply instanceof final Number numb && TypeHelper.isDouble(searchQuery))
82+
{
83+
LOGGER.debug("Item is an instance of Number.");
84+
return numb.toString().contains(searchQuery);
85+
}
86+
87+
if (apply instanceof final Enum<?> enm)
88+
{
89+
LOGGER.debug("Item is an instance of Enum.");
90+
return enm.toString().contains(searchQuery);
91+
}
92+
93+
LOGGER.debug("Condition is false because the type is not applicable.");
94+
return false;
95+
};
96+
}
97+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.vaadin.comparators;
17+
18+
import java.time.LocalDate;
19+
import java.time.LocalDateTime;
20+
import java.time.temporal.TemporalAccessor;
21+
import java.util.function.Predicate;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import com.vaadin.flow.function.ValueProvider;
27+
28+
import software.xdev.vaadin.comparators.utl.TypeHelper;
29+
30+
31+
/**
32+
* Used for comparison with equals.
33+
*
34+
* @author tboehm
35+
*/
36+
public final class EqualComparator implements FilterComparator
37+
{
38+
private static final Logger LOGGER = LoggerFactory.getLogger(EqualComparator.class);
39+
private static EqualComparator instance;
40+
41+
private EqualComparator()
42+
{
43+
}
44+
45+
public static EqualComparator getInstance()
46+
{
47+
if (instance == null)
48+
{
49+
instance = new EqualComparator();
50+
}
51+
52+
return instance;
53+
}
54+
55+
@Override
56+
public String getDescription()
57+
{
58+
return "is equals to";
59+
}
60+
61+
@Override
62+
public boolean isApplicable(final Class<?> clazz)
63+
{
64+
return Number.class.isAssignableFrom(clazz)
65+
|| String.class.isAssignableFrom(clazz)
66+
|| TemporalAccessor.class.isAssignableFrom(clazz)
67+
|| Enum.class.isAssignableFrom(clazz)
68+
|| Boolean.class.isAssignableFrom(clazz);
69+
}
70+
71+
@Override
72+
public <B, T> Predicate<B> compare(final ValueProvider<B, T> provider, final String searchQuery)
73+
{
74+
LOGGER.debug("Checking if the item is equals to {}", searchQuery);
75+
76+
return item ->
77+
{
78+
final T apply = provider.apply(item);
79+
80+
TypeHelper.checkIfTypeIsApplicable(this, apply.getClass());
81+
82+
if (apply instanceof final String strValue)
83+
{
84+
LOGGER.debug("Item is an instance of String.");
85+
return strValue.equalsIgnoreCase(searchQuery);
86+
}
87+
88+
if (apply instanceof final Number numb && TypeHelper.isDouble(searchQuery))
89+
{
90+
LOGGER.debug("Item is an instance of Number.");
91+
return numb.doubleValue() == (Double.parseDouble(searchQuery));
92+
}
93+
94+
if (apply instanceof final LocalDate date && TypeHelper.isLocalDate(searchQuery))
95+
{
96+
LOGGER.debug("Item is an instance of LocalDate.");
97+
return LocalDate.from(date).equals(LocalDate.parse(searchQuery));
98+
}
99+
100+
if (apply instanceof final LocalDateTime date && TypeHelper.isLocalDateTime(searchQuery))
101+
{
102+
LOGGER.debug("Item is an instance of LocalDateTime.");
103+
return LocalDateTime.from(date).equals(LocalDateTime.parse(searchQuery));
104+
}
105+
106+
if (apply instanceof final Enum<?> enm)
107+
{
108+
LOGGER.debug("Item is an instance of Enum.");
109+
return enm.toString().equals(searchQuery);
110+
}
111+
112+
if (apply instanceof final Boolean bool)
113+
{
114+
LOGGER.debug("Item is an instance of Boolean.");
115+
return bool == Boolean.parseBoolean(searchQuery);
116+
}
117+
118+
return apply.equals(searchQuery);
119+
};
120+
}
121+
}

0 commit comments

Comments
 (0)