aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-07-12 14:35:33 +0300
committerTeemu Suo-Anttila <teemusa@vaadin.com>2016-07-12 14:35:33 +0300
commit4ebfe3e0177137ca6261d0fd9d0e87fa8f3cb4e7 (patch)
tree38ea0ad7a3d1518a04d88d6aa7a10ab75f3c916c
parent9f0668b91f1a3f6c34fcf2b23d75e0d1c1a71b45 (diff)
downloadvaadin-framework-4ebfe3e0177137ca6261d0fd9d0e87fa8f3cb4e7.tar.gz
vaadin-framework-4ebfe3e0177137ca6261d0fd9d0e87fa8f3cb4e7.zip
Add sortingBy with function parameter to in-memory data source
Change-Id: Ib622bba1a845121cfff8dd23544c95dbce8d8974
-rw-r--r--server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java8
-rw-r--r--server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java17
2 files changed, 23 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java b/server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java
index 7b4b84e5b9..a23d337652 100644
--- a/server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java
+++ b/server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java
@@ -30,7 +30,6 @@ import java.util.stream.Stream;
*/
public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
- // FIXME: Missing Query object
private Function<Object, Stream<T>> request;
/**
@@ -63,8 +62,13 @@ public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
}
@Override
- public DataSource<T, Comparator<T>> sortingBy(Comparator<T> sortOrder) {
+ public InMemoryDataSource<T> sortingBy(Comparator<T> sortOrder) {
return new InMemoryDataSource<T>(q -> request.apply(q)
.sorted(sortOrder));
}
+
+ public <U extends Comparable<? super U>> InMemoryDataSource<T> sortingBy(
+ Function<T, U> sortOrder) {
+ return sortingBy(Comparator.comparing(sortOrder));
+ }
}
diff --git a/server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java b/server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java
index 2bd96e9017..545c4f834b 100644
--- a/server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java
+++ b/server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java
@@ -83,4 +83,21 @@ public class InMemoryDataSourceTest {
}
}
}
+
+ @Test
+ public void testDefatulSortWithFunction() {
+ List<StrBean> list = dataSource.sortingBy(StrBean::getValue)
+ .apply(null).collect(Collectors.toList());
+
+ Assert.assertEquals("Sorted data and original data sizes don't match",
+ data.size(), list.size());
+
+ for (int i = 1; i < list.size(); ++i) {
+ StrBean prev = list.get(i - 1);
+ StrBean cur = list.get(i);
+
+ // Test default sort
+ Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0);
+ }
+ }
}