1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.data.provider;
import java.util.Locale;
import java.util.Objects;
import com.vaadin.data.ValueProvider;
import com.vaadin.server.SerializableBiPredicate;
import com.vaadin.server.SerializableComparator;
import com.vaadin.server.SerializablePredicate;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.ui.UI;
/**
* A mixin interface for in-memory data providers. Contains methods for
* configuring sorting and filtering.
*
* @author Vaadin Ltd
* @since 8.1
*
* @param <T>
* data type
*/
public interface InMemoryDataProvider<T> extends
ConfigurableFilterDataProvider<T, SerializablePredicate<T>, SerializablePredicate<T>> {
@Override
public default boolean isInMemory() {
return true;
}
/**
* Gets the current filter of this data provider.
*
* @return the filter of this data provider
*/
public SerializablePredicate<T> getFilter();
/**
* Sets a filter to be applied to all queries. The filter replaces any
* filter that has been set or added previously.
*
* @see #setFilter(ValueProvider, SerializablePredicate)
* @see #setFilterByValue(ValueProvider, Object)
* @see #addFilter(SerializablePredicate)
*
* @param filter
* the filter to set, or <code>null</code> to remove any set
* filters
*/
@Override
public void setFilter(SerializablePredicate<T> filter);
/**
* Sets a filter for an item property. The filter replaces any filter that
* has been set or added previously.
*
* @see #setFilter(SerializablePredicate)
* @see #setFilterByValue(ValueProvider, Object)
* @see #addFilter(ValueProvider, SerializablePredicate)
*
* @param valueProvider
* value provider that gets the property value, not
* <code>null</code>
* @param valueFilter
* filter for testing the property value, not <code>null</code>
*/
public default <V> void setFilter(ValueProvider<T, V> valueProvider,
SerializablePredicate<V> valueFilter) {
setFilter(InMemoryDataProviderHelpers
.createValueProviderFilter(valueProvider, valueFilter));
}
/**
* Adds a filter to be applied to all queries. The filter will be used in
* addition to any filter that has been set or added previously.
*
* @see #addFilter(ValueProvider, SerializablePredicate)
* @see #addFilterByValue(ValueProvider, Object)
* @see #setFilter(SerializablePredicate)
*
* @param filter
* the filter to add, not <code>null</code>
*/
public default void addFilter(SerializablePredicate<T> filter) {
Objects.requireNonNull(filter, "Filter cannot be null");
if (getFilter() == null) {
setFilter(filter);
} else {
SerializablePredicate<T> oldFilter = getFilter();
setFilter(item -> oldFilter.test(item) && filter.test(item));
}
}
/**
* Adds a filter for an item property. The filter will be used in addition
* to any filter that has been set or added previously.
*
* @see #addFilter(SerializablePredicate)
* @see #addFilterByValue(ValueProvider, Object)
* @see #setFilter(ValueProvider, SerializablePredicate)
*
* @param valueProvider
* value provider that gets the property value, not
* <code>null</code>
* @param valueFilter
* filter for testing the property value, not <code>null</code>
*/
public default <V> void addFilter(ValueProvider<T, V> valueProvider,
SerializablePredicate<V> valueFilter) {
Objects.requireNonNull(valueProvider, "Value provider cannot be null");
Objects.requireNonNull(valueFilter, "Value filter cannot be null");
addFilter(InMemoryDataProviderHelpers
.createValueProviderFilter(valueProvider, valueFilter));
}
/**
* Sets a filter that requires an item property to have a specific value.
* The property value and the provided value are compared using
* {@link Object#equals(Object)}. The filter replaces any filter that has
* been set or added previously.
*
* @see #setFilter(SerializablePredicate)
* @see #setFilter(ValueProvider, SerializablePredicate)
* @see #addFilterByValue(ValueProvider, Object)
*
* @param valueProvider
* value provider that gets the property value, not
* <code>null</code>
* @param requiredValue
* the value that the property must have for the filter to pass
*/
public default <V> void setFilterByValue(ValueProvider<T, V> valueProvider,
V requiredValue) {
setFilter(InMemoryDataProviderHelpers.createEqualsFilter(valueProvider,
requiredValue));
}
/**
* Adds a filter that requires an item property to have a specific value.
* The property value and the provided value are compared using
* {@link Object#equals(Object)}.The filter will be used in addition to any
* filter that has been set or added previously.
*
* @see #setFilterByValue(ValueProvider, Object)
* @see #addFilter(SerializablePredicate)
* @see #addFilter(ValueProvider, SerializablePredicate)
*
* @param valueProvider
* value provider that gets the property value, not
* <code>null</code>
* @param requiredValue
* the value that the property must have for the filter to pass
*/
public default <V> void addFilterByValue(ValueProvider<T, V> valueProvider,
V requiredValue) {
addFilter(InMemoryDataProviderHelpers.createEqualsFilter(valueProvider,
requiredValue));
}
/**
* Removes any filter that has been set or added previously.
*
* @see #setFilter(SerializablePredicate)
*/
public default void clearFilters() {
setFilter(null);
}
/**
* Gets the current sort comparator of this data provider.
*
* @return the sort comparator of this data provider
*/
public SerializableComparator<T> getSortComparator();
/**
* Sets the comparator to use as the default sorting for this data provider.
* This overrides the sorting set by any other method that manipulates the
* default sorting of this data provider.
* <p>
* The default sorting is used if the query defines no sorting. The default
* sorting is also used to determine the ordering of items that are
* considered equal by the sorting defined in the query.
*
* @see #setSortOrder(ValueProvider, SortDirection)
* @see #addSortComparator(SerializableComparator)
*
* @param comparator
* a comparator to use, or <code>null</code> to clear any
* previously set sort order
*/
public void setSortComparator(SerializableComparator<T> comparator);
/**
* Adds a comparator to the default sorting for this data provider. If no
* default sorting has been defined, then the provided comparator will be
* used as the default sorting. If a default sorting has been defined, then
* the provided comparator will be used to determine the ordering of items
* that are considered equal by the previously defined default sorting.
* <p>
* The default sorting is used if the query defines no sorting. The default
* sorting is also used to determine the ordering of items that are
* considered equal by the sorting defined in the query.
*
* @see #setSortComparator(SerializableComparator)
* @see #addSortOrder(ValueProvider, SortDirection)
*
* @param comparator
* a comparator to add, not <code>null</code>
*/
public default void addSortComparator(
SerializableComparator<T> comparator) {
Objects.requireNonNull(comparator, "Comparator to add cannot be null");
SerializableComparator<T> originalComparator = getSortComparator();
if (originalComparator == null) {
setSortComparator(comparator);
} else {
setSortComparator((a, b) -> {
int result = originalComparator.compare(a, b);
if (result == 0) {
result = comparator.compare(a, b);
}
return result;
});
}
}
/**
* Sets the property and direction to use as the default sorting for this
* data provider. This overrides the sorting set by any other method that
* manipulates the default sorting of this data provider.
* <p>
* The default sorting is used if the query defines no sorting. The default
* sorting is also used to determine the ordering of items that are
* considered equal by the sorting defined in the query.
*
* @see #setSortComparator(SerializableComparator)
* @see #addSortOrder(ValueProvider, SortDirection)
*
* @param valueProvider
* the value provider that defines the property do sort by, not
* <code>null</code>
* @param sortDirection
* the sort direction to use, not <code>null</code>
*/
public default <V extends Comparable<? super V>> void setSortOrder(
ValueProvider<T, V> valueProvider, SortDirection sortDirection) {
setSortComparator(InMemoryDataProviderHelpers
.propertyComparator(valueProvider, sortDirection));
}
/**
* Adds a property and direction to the default sorting for this data
* provider. If no default sorting has been defined, then the provided sort
* order will be used as the default sorting. If a default sorting has been
* defined, then the provided sort order will be used to determine the
* ordering of items that are considered equal by the previously defined
* default sorting.
* <p>
* The default sorting is used if the query defines no sorting. The default
* sorting is also used to determine the ordering of items that are
* considered equal by the sorting defined in the query.
*
* @see #setSortOrder(ValueProvider, SortDirection)
* @see #addSortComparator(SerializableComparator)
*
* @param valueProvider
* the value provider that defines the property do sort by, not
* <code>null</code>
* @param sortDirection
* the sort direction to use, not <code>null</code>
*/
public default <V extends Comparable<? super V>> void addSortOrder(
ValueProvider<T, V> valueProvider, SortDirection sortDirection) {
addSortComparator(InMemoryDataProviderHelpers
.propertyComparator(valueProvider, sortDirection));
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by comparing an item to the filter value provided in the query.
* <p>
* The predicate receives the item as the first parameter and the query
* filter value as the second parameter, and should return <code>true</code>
* if the corresponding item should be included. The query filter value is
* never <code>null</code> – all items are included without running the
* predicate if the query doesn't define any filter.
*
* @param predicate
* a predicate to use for comparing the item to the query filter,
* not <code>null</code>
*
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default <Q> DataProvider<T, Q> filteringBy(
SerializableBiPredicate<T, Q> predicate) {
Objects.requireNonNull(predicate, "Predicate cannot be null");
return withConvertedFilter(
filterValue -> item -> predicate.test(item, filterValue));
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by comparing an item property value to the filter value provided in the
* query.
* <p>
* The predicate receives the property value as the first parameter and the
* query filter value as the second parameter, and should return
* <code>true</code> if the corresponding item should be included. The query
* filter value is never <code>null</code> – all items are included without
* running either callback if the query doesn't define any filter.
*
* @param valueProvider
* a value provider that gets the property value, not
* <code>null</code>
* @param predicate
* a predicate to use for comparing the property value to the
* query filter, not <code>null</code>
*
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default <V, Q> DataProvider<T, Q> filteringBy(
ValueProvider<T, V> valueProvider,
SerializableBiPredicate<V, Q> predicate) {
Objects.requireNonNull(valueProvider, "Value provider cannot be null");
Objects.requireNonNull(predicate, "Predicate cannot be null");
return filteringBy((item, filterValue) -> predicate
.test(valueProvider.apply(item), filterValue));
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by testing whether the value of a property is equals to the filter value
* provided in the query. Equality is tested using
* {@link Objects#equals(Object, Object)}.
*
* @param valueProvider
* a value provider that gets the property value, not
* <code>null</code>
*
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default <V> DataProvider<T, V> filteringByEquals(
ValueProvider<T, V> valueProvider) {
return filteringBy(valueProvider, Objects::equals);
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by a string by checking whether the lower case representation of the
* filter value provided in the query is a substring of the lower case
* representation of an item property value. The filter never passes if the
* item property value is <code>null</code>.
*
* @param valueProvider
* a value provider that gets the string property value, not
* <code>null</code>
* @param locale
* the locale to use for converting the strings to lower case,
* not <code>null</code>
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default DataProvider<T, String> filteringBySubstring(
ValueProvider<T, String> valueProvider, Locale locale) {
Objects.requireNonNull(locale, "Locale cannot be null");
return InMemoryDataProviderHelpers.filteringByCaseInsensitiveString(
this, valueProvider, String::contains, () -> locale);
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by a string by checking whether the lower case representation of the
* filter value provided in the query is a substring of the lower case
* representation of an item property value. Conversion to lower case is
* done using the locale of the {@link UI#getCurrent() current UI} if
* available, or otherwise {@link Locale#getDefault() the default locale}.
* The filter never passes if the item property value is <code>null</code>.
*
* @param valueProvider
* a value provider that gets the string property value, not
* <code>null</code>
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default DataProvider<T, String> filteringBySubstring(
ValueProvider<T, String> valueProvider) {
return InMemoryDataProviderHelpers.filteringByCaseInsensitiveString(
this, valueProvider, String::contains,
InMemoryDataProviderHelpers.CURRENT_LOCALE_SUPPLIER);
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by a string by checking whether the lower case representation of an item
* property value starts with the lower case representation of the filter
* value provided in the query. The filter never passes if the item property
* value is <code>null</code>.
*
* @param valueProvider
* a value provider that gets the string property value, not
* <code>null</code>
* @param locale
* the locale to use for converting the strings to lower case,
* not <code>null</code>
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default DataProvider<T, String> filteringByPrefix(
ValueProvider<T, String> valueProvider, Locale locale) {
return InMemoryDataProviderHelpers.filteringByCaseInsensitiveString(
this, valueProvider, String::startsWith, () -> locale);
}
/**
* Wraps this data provider to create a new data provider that is filtered
* by a string by checking whether the lower case representation of an item
* property value starts with the lower case representation of the filter
* value provided in the query. Conversion to lower case is done using the
* locale of the {@link UI#getCurrent() current UI} if available, or
* otherwise {@link Locale#getDefault() the default locale}. The filter
* never passes if the item property value is <code>null</code>.
*
* @param valueProvider
* a value provider that gets the string property value, not
* <code>null</code>
* @return a data provider that filters accordingly, not <code>null</code>
*/
public default DataProvider<T, String> filteringByPrefix(
ValueProvider<T, String> valueProvider) {
return InMemoryDataProviderHelpers.filteringByCaseInsensitiveString(
this, valueProvider, String::startsWith,
InMemoryDataProviderHelpers.CURRENT_LOCALE_SUPPLIER);
}
}
|