ListDataProvider extends AbstractDataProvider<T, SerializablePredicate<T>> implements InMemoryDataProvider<T> { @Override public Stream<T> fetch(Query<T, SerializablePredicate<T>> query) { Stream<T> stream = getFilteredStream(query); Optional<Comparator<T>> comparing = Stream .of(query.getInMemorySorting(), sortOrder) .filter(Objects::nonNull) .reduce((c1, c2) -> c1.thenComparing(c2)); if (comparing.isPresent()) { stream = stream.sorted(comparing.get()); } return stream.skip(query.getOffset()).limit(query.getLimit()); } ListDataProvider(Collection<T> items); Collection<T> getItems(); @Override Stream<T> fetch(Query<T, SerializablePredicate<T>> query); @Override int size(Query<T, SerializablePredicate<T>> query); @Override SerializableComparator<T> getSortComparator(); @Override void setSortComparator(SerializableComparator<T> comparator); @Override SerializablePredicate<T> getFilter(); @Override void setFilter(SerializablePredicate<T> filter);  }
@Test public void setSortByProperty_ascending() { ListDataProvider<StrBean> dataProvider = getDataProvider(); dataProvider.setSortOrder(StrBean::getId, SortDirection.ASCENDING); int[] threeFirstIds = dataProvider.fetch(new Query<>()) .mapToInt(StrBean::getId).limit(3).toArray(); Assert.assertArrayEquals(new int[] { 0, 1, 2 }, threeFirstIds); }
@Test public void setSortByProperty_descending() { ListDataProvider<StrBean> dataProvider = getDataProvider(); dataProvider.setSortOrder(StrBean::getId, SortDirection.DESCENDING); int[] threeFirstIds = dataProvider.fetch(new Query<>()) .mapToInt(StrBean::getId).limit(3).toArray(); Assert.assertArrayEquals(new int[] { 98, 97, 96 }, threeFirstIds); }
@Test public void testMultipleSortOrder_firstAddedWins() { ListDataProvider<StrBean> dataProvider = getDataProvider(); dataProvider.addSortOrder(StrBean::getValue, SortDirection.DESCENDING); dataProvider.addSortOrder(StrBean::getId, SortDirection.DESCENDING); List<StrBean> threeFirstItems = dataProvider.fetch(new Query<>()) .limit(3).collect(Collectors.toList()); Assert.assertEquals(new StrBean("Xyz", 10, 100), threeFirstItems.get(0)); Assert.assertEquals(new StrBean("Foo", 93, 2), threeFirstItems.get(1)); Assert.assertEquals(new StrBean("Foo", 91, 2), threeFirstItems.get(2)); }