blob: 55048611655e2573d11c28d597ddaa540a44f9f5 (
plain)
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
|
package com.vaadin.data.provider;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.vaadin.server.SerializablePredicate;
import com.vaadin.shared.data.sort.SortDirection;
public class BackendDataProviderTest extends
DataProviderTestBase<BackEndDataProvider<StrBean, SerializablePredicate<StrBean>>> {
private static Map<String, Comparator<StrBean>> propertyToComparatorMap = new HashMap<>();
static {
propertyToComparatorMap.put("value",
Comparator.comparing(StrBean::getValue));
propertyToComparatorMap.put("id", Comparator.comparing(StrBean::getId));
propertyToComparatorMap.put("randomNumber",
Comparator.comparing(StrBean::getRandomNumber));
}
private Comparator<StrBean> getComparator(SortOrder<String> so) {
Comparator<StrBean> comparator = propertyToComparatorMap
.get(so.getSorted());
if (so.getDirection() == SortDirection.DESCENDING) {
comparator = comparator.reversed();
}
return comparator;
}
@Override
protected BackEndDataProvider<StrBean, SerializablePredicate<StrBean>> createDataProvider() {
return dataProvider = new BackEndDataProvider<>(query -> {
Stream<StrBean> stream = data.stream()
.filter(t -> query.getFilter().orElse(s -> true).test(t));
if (!query.getSortOrders().isEmpty()) {
Comparator<StrBean> sorting = query.getSortOrders().stream()
.map(this::getComparator)
.reduce((c1, c2) -> c1.thenComparing(c2)).get();
stream = stream.sorted(sorting);
}
List<StrBean> list = stream.skip(query.getOffset())
.limit(query.getLimit()).collect(Collectors.toList());
list.forEach(s -> System.err.println(s.toString()));
return list.stream();
}, query -> (int) data.stream()
.filter(t -> query.getFilter().orElse(s -> true).test(t))
.count());
}
@Override
protected void setSortOrder(List<SortOrder<String>> sortOrder,
Comparator<StrBean> comp) {
getDataProvider().setSortOrders(sortOrder);
}
}
|