From ecde311474591931b8457f0b653aefa14350f0a6 Mon Sep 17 00:00:00 2001 From: elmot Date: Wed, 17 Aug 2016 11:05:30 +0300 Subject: Rename InMemoryDataSource into ListDataSource Change-Id: Ic0a8f5ced9139a0f712aac917b6a5841b8e1b0ab --- .../java/com/vaadin/server/data/DataSource.java | 18 ++-- .../com/vaadin/server/data/InMemoryDataSource.java | 109 --------------------- .../com/vaadin/server/data/ListDataSource.java | 109 +++++++++++++++++++++ .../data/datasource/InMemoryDataSourceTest.java | 104 -------------------- .../server/data/datasource/ListDataSourceTest.java | 104 ++++++++++++++++++++ 5 files changed, 222 insertions(+), 222 deletions(-) delete mode 100644 server/src/main/java/com/vaadin/server/data/InMemoryDataSource.java create mode 100644 server/src/main/java/com/vaadin/server/data/ListDataSource.java delete mode 100644 server/src/test/java/com/vaadin/server/data/datasource/InMemoryDataSourceTest.java create mode 100644 server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java (limited to 'server') diff --git a/server/src/main/java/com/vaadin/server/data/DataSource.java b/server/src/main/java/com/vaadin/server/data/DataSource.java index 4f6db29535..167ef07606 100644 --- a/server/src/main/java/com/vaadin/server/data/DataSource.java +++ b/server/src/main/java/com/vaadin/server/data/DataSource.java @@ -29,7 +29,7 @@ import java.util.stream.Stream; * @param * data type * - * @see InMemoryDataSource + * @see ListDataSource * @see BackEndDataSource */ public interface DataSource extends Function>, @@ -53,21 +53,21 @@ public interface DataSource extends Function>, int size(Query t); /** - * This method creates a new {@link InMemoryDataSource} from a given - * Collection. The InMemoryDataSource creates a protective List copy of all + * This method creates a new {@link ListDataSource} from a given + * Collection. The ListDataSource creates a protective List copy of all * the contents in the Collection. * * @param data * collection of data * @return in-memory data source */ - public static InMemoryDataSource create(Collection data) { - return new InMemoryDataSource<>(data); + public static ListDataSource create(Collection data) { + return new ListDataSource<>(data); } /** - * This method creates a new {@link InMemoryDataSource} from given - * objects.The InMemoryDataSource creates a protective List copy of all the + * This method creates a new {@link ListDataSource} from given + * objects.The ListDataSource creates a protective List copy of all the * contents in the array. * * @param data @@ -75,7 +75,7 @@ public interface DataSource extends Function>, * @return in-memory data source */ @SafeVarargs - public static InMemoryDataSource create(T... data) { - return new InMemoryDataSource<>(Arrays.asList(data)); + public static ListDataSource create(T... data) { + return new ListDataSource<>(Arrays.asList(data)); } } \ No newline at end of file diff --git a/server/src/main/java/com/vaadin/server/data/InMemoryDataSource.java b/server/src/main/java/com/vaadin/server/data/InMemoryDataSource.java deleted file mode 100644 index bb75f45ffb..0000000000 --- a/server/src/main/java/com/vaadin/server/data/InMemoryDataSource.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2000-2014 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.server.data; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.function.Function; -import java.util.stream.Stream; - -/** - * {@link DataSource} wrapper for {@link Collection}s. This class does not - * actually handle the {@link Query} parameters. - * - * @param - * data type - */ -public class InMemoryDataSource implements DataSource { - - private Function> request; - private int size; - - /** - * Constructs a new ListDataSource. This method makes a protective copy of - * the contents of the Collection. - * - * @param collection - * initial data - */ - public InMemoryDataSource(Collection collection) { - final List backend = new ArrayList<>(collection); - request = query -> backend.stream(); - size = backend.size(); - } - - /** - * Chaining constructor for making modified {@link InMemoryDataSource}s. - * This Constructor is used internally for making sorted and filtered - * variants of a base data source with actual data. - * - * @param request - * request for the new data source - */ - protected InMemoryDataSource(Function> request) { - this.request = request; - } - - @Override - public Stream apply(Query query) { - return request.apply(query); - } - - /** - * Sets a default sorting order to the data source. - * - * @param sortOrder - * a {@link Comparator} providing the needed sorting order - * @return new data source with modified sorting - */ - public InMemoryDataSource sortingBy(Comparator sortOrder) { - return new InMemoryDataSource<>(q -> request.apply(q) - .sorted(sortOrder)); - } - - /** - * Sets a default sorting order to the data source. This method is a - * short-hand for {@code sortingBy(Comparator.comparing(sortOrder))}. - * - * @param sortOrder - * function to sort by - * @param - * the type of the Comparable sort key - * @return new data source with modified sorting - */ - public > InMemoryDataSource sortingBy( - Function sortOrder) { - return sortingBy(Comparator.comparing(sortOrder)); - } - - @Override - public boolean isInMemory() { - return true; - } - - /** - * {@inheritDoc} - *

- * For in-memory data source the query is not handled, and it will always - * return the full size. - */ - @Override - public int size(Query t) { - return size; - } -} diff --git a/server/src/main/java/com/vaadin/server/data/ListDataSource.java b/server/src/main/java/com/vaadin/server/data/ListDataSource.java new file mode 100644 index 0000000000..fe478996c3 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/ListDataSource.java @@ -0,0 +1,109 @@ +/* + * Copyright 2000-2014 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.server.data; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Stream; + +/** + * {@link DataSource} wrapper for {@link Collection}s. This class does not + * actually handle the {@link Query} parameters. + * + * @param + * data type + */ +public class ListDataSource implements DataSource { + + private Function> request; + private int size; + + /** + * Constructs a new ListDataSource. This method makes a protective copy of + * the contents of the Collection. + * + * @param collection + * initial data + */ + public ListDataSource(Collection collection) { + final List backend = new ArrayList<>(collection); + request = query -> backend.stream(); + size = backend.size(); + } + + /** + * Chaining constructor for making modified {@link ListDataSource}s. + * This Constructor is used internally for making sorted and filtered + * variants of a base data source with actual data. + * + * @param request + * request for the new data source + */ + protected ListDataSource(Function> request) { + this.request = request; + } + + @Override + public Stream apply(Query query) { + return request.apply(query); + } + + /** + * Sets a default sorting order to the data source. + * + * @param sortOrder + * a {@link Comparator} providing the needed sorting order + * @return new data source with modified sorting + */ + public ListDataSource sortingBy(Comparator sortOrder) { + return new ListDataSource<>(q -> request.apply(q) + .sorted(sortOrder)); + } + + /** + * Sets a default sorting order to the data source. This method is a + * short-hand for {@code sortingBy(Comparator.comparing(sortOrder))}. + * + * @param sortOrder + * function to sort by + * @param + * the type of the Comparable sort key + * @return new data source with modified sorting + */ + public > ListDataSource sortingBy( + Function sortOrder) { + return sortingBy(Comparator.comparing(sortOrder)); + } + + @Override + public boolean isInMemory() { + return true; + } + + /** + * {@inheritDoc} + *

+ * For in-memory data source the query is not handled, and it will always + * return the full size. + */ + @Override + public int size(Query t) { + return size; + } +} diff --git a/server/src/test/java/com/vaadin/server/data/datasource/InMemoryDataSourceTest.java b/server/src/test/java/com/vaadin/server/data/datasource/InMemoryDataSourceTest.java deleted file mode 100644 index 5c71b1e48f..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/InMemoryDataSourceTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.vaadin.server.data.datasource; - -import static org.junit.Assert.assertTrue; - -import java.util.Comparator; -import java.util.List; -import java.util.stream.Collectors; - -import com.vaadin.server.data.DataSource; -import com.vaadin.server.data.InMemoryDataSource; -import com.vaadin.server.data.Query; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class InMemoryDataSourceTest { - - private InMemoryDataSource dataSource; - private List data; - - @Before - public void setUp() { - data = StrBean.generateRandomBeans(100); - dataSource = DataSource.create(data); - } - - @Test - public void testListContainsAllData() { - dataSource.apply(new Query()).forEach( - str -> assertTrue( - "Data source contained values not in original data", - data.remove(str))); - assertTrue("Not all values from original data were in data source", - data.isEmpty()); - } - - @Test - public void testSortByComparatorListsDiffer() { - Comparator comp = Comparator.comparing(StrBean::getValue) - .thenComparing(StrBean::getRandomNumber) - .thenComparing(StrBean::getId); - List list = dataSource.sortingBy(comp).apply(new Query()) - .collect(Collectors.toList()); - - // First value in data is { Xyz, 10, 100 } which should be last in list - Assert.assertNotEquals("First value should not match", data.get(0), - list.get(0)); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - data.sort(comp); - for (int i = 0; i < data.size(); ++i) { - Assert.assertEquals("Sorting result differed", data.get(i), - list.get(i)); - } - } - - @Test - public void testDefatulSortWithSpecifiedPostSort() { - Comparator comp = Comparator.comparing(StrBean::getValue) - .thenComparing(Comparator.comparing(StrBean::getId).reversed()); - List list = dataSource.sortingBy(comp).apply(new Query()) - // The sort here should come e.g from a Component - .sorted(Comparator.comparing(StrBean::getRandomNumber)) - .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 specific sort - Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber()); - - if (prev.getRandomNumber() == cur.getRandomNumber()) { - // Test default sort - Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); - if (prev.getValue().equals(cur.getValue())) { - Assert.assertTrue(prev.getId() > cur.getId()); - } - } - } - } - - @Test - public void testDefatulSortWithFunction() { - List list = dataSource.sortingBy(StrBean::getValue) - .apply(new Query()).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); - } - } -} diff --git a/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java b/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java new file mode 100644 index 0000000000..e1d57ad959 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java @@ -0,0 +1,104 @@ +package com.vaadin.server.data.datasource; + +import static org.junit.Assert.assertTrue; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.Query; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ListDataSourceTest { + + private ListDataSource dataSource; + private List data; + + @Before + public void setUp() { + data = StrBean.generateRandomBeans(100); + dataSource = DataSource.create(data); + } + + @Test + public void testListContainsAllData() { + dataSource.apply(new Query()).forEach( + str -> assertTrue( + "Data source contained values not in original data", + data.remove(str))); + assertTrue("Not all values from original data were in data source", + data.isEmpty()); + } + + @Test + public void testSortByComparatorListsDiffer() { + Comparator comp = Comparator.comparing(StrBean::getValue) + .thenComparing(StrBean::getRandomNumber) + .thenComparing(StrBean::getId); + List list = dataSource.sortingBy(comp).apply(new Query()) + .collect(Collectors.toList()); + + // First value in data is { Xyz, 10, 100 } which should be last in list + Assert.assertNotEquals("First value should not match", data.get(0), + list.get(0)); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + data.sort(comp); + for (int i = 0; i < data.size(); ++i) { + Assert.assertEquals("Sorting result differed", data.get(i), + list.get(i)); + } + } + + @Test + public void testDefatulSortWithSpecifiedPostSort() { + Comparator comp = Comparator.comparing(StrBean::getValue) + .thenComparing(Comparator.comparing(StrBean::getId).reversed()); + List list = dataSource.sortingBy(comp).apply(new Query()) + // The sort here should come e.g from a Component + .sorted(Comparator.comparing(StrBean::getRandomNumber)) + .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 specific sort + Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber()); + + if (prev.getRandomNumber() == cur.getRandomNumber()) { + // Test default sort + Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); + if (prev.getValue().equals(cur.getValue())) { + Assert.assertTrue(prev.getId() > cur.getId()); + } + } + } + } + + @Test + public void testDefatulSortWithFunction() { + List list = dataSource.sortingBy(StrBean::getValue) + .apply(new Query()).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); + } + } +} -- cgit v1.2.3