From 6c6161caeb8d4524aba11fa7976366981e090e85 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 25 Jan 2017 09:36:29 +0200 Subject: [PATCH] Use non-Generic SortOrder in Query. (#8239) * Use non-Generic SortOrder in Query. Fixes #8215 --- .../provider/AbstractBackEndDataProvider.java | 6 +-- .../data/provider/BackEndDataProvider.java | 6 +-- .../data/provider/DataCommunicator.java | 6 +-- .../java/com/vaadin/data/provider/Query.java | 6 +-- .../vaadin/data/provider/QuerySortOrder.java | 48 +++++++++++++++++++ .../java/com/vaadin/data/provider/Sort.java | 24 +++++----- .../com/vaadin/data/provider/SortOrder.java | 1 - server/src/main/java/com/vaadin/ui/Grid.java | 21 ++++---- .../ui/components/grid/SortOrderProvider.java | 6 +-- .../provider/BackendDataProviderTest.java | 5 +- .../data/provider/DataProviderTestBase.java | 12 ++--- .../data/provider/ListDataProviderTest.java | 2 +- .../provider/bov/DataProviderBoVTest.java | 4 +- 13 files changed, 94 insertions(+), 53 deletions(-) create mode 100644 server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java diff --git a/server/src/main/java/com/vaadin/data/provider/AbstractBackEndDataProvider.java b/server/src/main/java/com/vaadin/data/provider/AbstractBackEndDataProvider.java index 8da5d7a726..9b08bb8c1d 100644 --- a/server/src/main/java/com/vaadin/data/provider/AbstractBackEndDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/AbstractBackEndDataProvider.java @@ -33,7 +33,7 @@ import java.util.stream.Stream; public abstract class AbstractBackEndDataProvider extends AbstractDataProvider implements BackEndDataProvider { - private List> sortOrders = new ArrayList<>(); + private List sortOrders = new ArrayList<>(); private Query mixInSortOrders(Query query) { if (sortOrders.isEmpty()) { @@ -43,7 +43,7 @@ public abstract class AbstractBackEndDataProvider extends Set sortedPropertyNames = query.getSortOrders().stream() .map(SortOrder::getSorted).collect(Collectors.toSet()); - List> combinedSortOrders = Stream + List combinedSortOrders = Stream .concat(query.getSortOrders().stream(), sortOrders.stream() .filter(order -> !sortedPropertyNames @@ -86,7 +86,7 @@ public abstract class AbstractBackEndDataProvider extends protected abstract int sizeInBackEnd(Query query); @Override - public void setSortOrders(List> sortOrders) { + public void setSortOrders(List sortOrders) { this.sortOrders = Objects.requireNonNull(sortOrders, "Sort orders cannot be null"); refreshAll(); diff --git a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java index f20b22fa17..36d875a21a 100644 --- a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java @@ -37,12 +37,12 @@ public interface BackEndDataProvider extends DataProvider { * sorting is also used to determine the ordering of items that are * considered equal by the sorting defined in the query. * - * @see #setSortOrder(SortOrder) + * @see #setSortOrder(QuerySortOrder) * * @param sortOrders * a list of sort orders to set, not null */ - void setSortOrders(List> sortOrders); + void setSortOrders(List sortOrders); /** * Sets a single sort order to use as the default sorting for this data @@ -59,7 +59,7 @@ public interface BackEndDataProvider extends DataProvider { * a sort order to set, or null to clear any * previously set sort orders */ - default void setSortOrder(SortOrder sortOrder) { + default void setSortOrder(QuerySortOrder sortOrder) { if (sortOrder == null) { setSortOrders(Collections.emptyList()); } else { diff --git a/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java b/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java index 12fd323bc1..7b909b831d 100644 --- a/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java +++ b/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java @@ -201,7 +201,7 @@ public class DataCommunicator extends AbstractExtension { private F filter; private Comparator inMemorySorting; - private final List> backEndSorting = new ArrayList<>(); + private final List backEndSorting = new ArrayList<>(); private final DataCommunicatorClientRpc rpc; public DataCommunicator() { @@ -421,12 +421,12 @@ public class DataCommunicator extends AbstractExtension { } /** - * Sets the {@link SortOrder}s to use with backend sorting. + * Sets the {@link QuerySortOrder}s to use with backend sorting. * * @param sortOrder * list of sort order information to pass to a query */ - public void setBackEndSorting(List> sortOrder) { + public void setBackEndSorting(List sortOrder) { backEndSorting.clear(); backEndSorting.addAll(sortOrder); reset(); diff --git a/server/src/main/java/com/vaadin/data/provider/Query.java b/server/src/main/java/com/vaadin/data/provider/Query.java index a4e0197d83..abf905658c 100644 --- a/server/src/main/java/com/vaadin/data/provider/Query.java +++ b/server/src/main/java/com/vaadin/data/provider/Query.java @@ -36,7 +36,7 @@ public class Query implements Serializable { private final int offset; private final int limit; - private final List> sortOrders; + private final List sortOrders; private final Comparator inMemorySorting; private final F filter; @@ -83,7 +83,7 @@ public class Query implements Serializable { * @param filter * filtering for fetching; can be null */ - public Query(int offset, int limit, List> sortOrders, + public Query(int offset, int limit, List sortOrders, Comparator inMemorySorting, F filter) { this.offset = offset; this.limit = limit; @@ -123,7 +123,7 @@ public class Query implements Serializable { * * @return list of sort orders */ - public List> getSortOrders() { + public List getSortOrders() { return sortOrders; } diff --git a/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java new file mode 100644 index 0000000000..ca8d743fae --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2016 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 com.vaadin.shared.data.sort.SortDirection; + +/** + * Sorting information for {@link Query}. + * + * @see Query + */ +public class QuerySortOrder extends SortOrder { + + /** + * Constructs sorting information for usage in a {@link Query}. + * + * @param sorted + * sorting information, usually field id + * @param direction + * sorting direction + */ + public QuerySortOrder(String sorted, SortDirection direction) { + super(sorted, direction); + } + + /** + * Gets sorting information. + * + * @return sorting entity, usually field id + */ + @Override + public String getSorted() { + return super.getSorted(); + } +} diff --git a/server/src/main/java/com/vaadin/data/provider/Sort.java b/server/src/main/java/com/vaadin/data/provider/Sort.java index 303ee05f74..0655c7d0e4 100644 --- a/server/src/main/java/com/vaadin/data/provider/Sort.java +++ b/server/src/main/java/com/vaadin/data/provider/Sort.java @@ -35,16 +35,14 @@ public abstract class Sort implements Serializable { * lists. When the sort order is ready to be passed on, calling * {@link #build()} will create the list of sort orders * - * @param - * sort order data type * * @see Sort * @see Sort#asc(Object) * @see Sort#desc(Object) * @see #build() */ - public static class SortBuilder implements Serializable { - private List> sortOrder = new ArrayList<>(); + public static class SortBuilder implements Serializable { + private List sortOrder = new ArrayList<>(); /** * Constructs an empty SortBuilder. @@ -59,7 +57,7 @@ public abstract class Sort implements Serializable { * the object to sort by * @return this sort builder */ - public SortBuilder thenAsc(S by) { + public SortBuilder thenAsc(String by) { return append(by, SortDirection.ASCENDING); } @@ -70,7 +68,7 @@ public abstract class Sort implements Serializable { * the object to sort by * @return this sort builder */ - public SortBuilder thenDesc(S by) { + public SortBuilder thenDesc(String by) { return append(by, SortDirection.DESCENDING); } @@ -84,8 +82,8 @@ public abstract class Sort implements Serializable { * * @return this sort builder */ - protected SortBuilder append(S by, SortDirection direction) { - sortOrder.add(new SortOrder<>(by, direction)); + protected SortBuilder append(String by, SortDirection direction) { + sortOrder.add(new QuerySortOrder(by, direction)); return this; } @@ -95,7 +93,7 @@ public abstract class Sort implements Serializable { * * @return the unmodifiable sort order list */ - public List> build() { + public List build() { return Collections.unmodifiableList(sortOrder); } } @@ -111,8 +109,8 @@ public abstract class Sort implements Serializable { * * @return the sort builder */ - public static SortBuilder asc(S by) { - return new SortBuilder().thenAsc(by); + public static SortBuilder asc(String by) { + return new SortBuilder().thenAsc(by); } /** @@ -126,7 +124,7 @@ public abstract class Sort implements Serializable { * * @return the sort builder */ - public static SortBuilder desc(S by) { - return new SortBuilder().thenDesc(by); + public static SortBuilder desc(String by) { + return new SortBuilder().thenDesc(by); } } diff --git a/server/src/main/java/com/vaadin/data/provider/SortOrder.java b/server/src/main/java/com/vaadin/data/provider/SortOrder.java index 34e560e2d1..cb6a62668a 100644 --- a/server/src/main/java/com/vaadin/data/provider/SortOrder.java +++ b/server/src/main/java/com/vaadin/data/provider/SortOrder.java @@ -22,7 +22,6 @@ import com.vaadin.shared.data.sort.SortDirection; /** * Sorting information for one field. * - * @see Query * @param * the type of the sorting information, usually a String (field id) * or a {@link java.util.Comparator}. diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index ae78d5486b..b3410c57f0 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -47,6 +47,7 @@ import com.vaadin.data.ValueProvider; import com.vaadin.data.provider.DataCommunicator; import com.vaadin.data.provider.DataProvider; import com.vaadin.data.provider.Query; +import com.vaadin.data.provider.QuerySortOrder; import com.vaadin.data.provider.SortOrder; import com.vaadin.event.ConnectorEvent; import com.vaadin.event.ContextClickEvent; @@ -122,8 +123,8 @@ import elemental.json.JsonValue; * @param * the grid bean type */ -public class Grid extends AbstractListing - implements HasComponents, HasDataProvider, SortNotifier> { +public class Grid extends AbstractListing implements HasComponents, + HasDataProvider, SortNotifier> { @Deprecated private static final Method COLUMN_REORDER_METHOD = ReflectTools.findMethod( @@ -787,8 +788,8 @@ public class Grid extends AbstractListing private String userId; /** - * Constructs a new Column configuration with given renderer and - * value provider. + * Constructs a new Column configuration with given renderer and value + * provider. * * @param valueProvider * the function to get values from items @@ -1078,14 +1079,14 @@ public class Grid extends AbstractListing public Column setSortProperty(String... properties) { Objects.requireNonNull(properties, "Sort properties can't be null"); sortOrderProvider = dir -> Arrays.stream(properties) - .map(s -> new SortOrder<>(s, dir)); + .map(s -> new QuerySortOrder(s, dir)); return this; } /** * Sets the sort orders when sorting this column. The sort order - * provider is a function which provides {@link SortOrder} objects to - * describe how to sort by this column. + * provider is a function which provides {@link QuerySortOrder} objects + * to describe how to sort by this column. * * @param provider * the function to use when generating sort orders with the @@ -1107,7 +1108,7 @@ public class Grid extends AbstractListing * the sorting direction * @return stream of sort orders */ - public Stream> getSortOrder(SortDirection direction) { + public Stream getSortOrder(SortDirection direction) { return sortOrderProvider.apply(direction); } @@ -2772,7 +2773,7 @@ public class Grid extends AbstractListing } /** - * Sort this Grid in user-specified {@link SortOrder} by a column. + * Sort this Grid in user-specified {@link QuerySortOrder} by a column. * * @param column * a column to sort against @@ -3200,7 +3201,7 @@ public class Grid extends AbstractListing getDataCommunicator().setInMemorySorting(comparator); // Back-end sort properties - List> sortProperties = new ArrayList<>(); + List sortProperties = new ArrayList<>(); sortOrder.stream().map( order -> order.getSorted().getSortOrder(order.getDirection())) .forEach(s -> s.forEach(sortProperties::add)); diff --git a/server/src/main/java/com/vaadin/ui/components/grid/SortOrderProvider.java b/server/src/main/java/com/vaadin/ui/components/grid/SortOrderProvider.java index 046719e978..79cc9d4a4d 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/SortOrderProvider.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/SortOrderProvider.java @@ -17,7 +17,7 @@ package com.vaadin.ui.components.grid; import java.util.stream.Stream; -import com.vaadin.data.provider.SortOrder; +import com.vaadin.data.provider.QuerySortOrder; import com.vaadin.server.SerializableFunction; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.ui.Grid.Column; @@ -32,7 +32,7 @@ import com.vaadin.ui.Grid.Column; */ @FunctionalInterface public interface SortOrderProvider - extends SerializableFunction>> { + extends SerializableFunction> { /** * Generates the sort orders when rows are sorted by a column. @@ -43,6 +43,6 @@ public interface SortOrderProvider * @return sort information */ @Override - public Stream> apply(SortDirection sortDirection); + public Stream apply(SortDirection sortDirection); } diff --git a/server/src/test/java/com/vaadin/data/provider/BackendDataProviderTest.java b/server/src/test/java/com/vaadin/data/provider/BackendDataProviderTest.java index e2f5211d00..b91a6fe3af 100644 --- a/server/src/test/java/com/vaadin/data/provider/BackendDataProviderTest.java +++ b/server/src/test/java/com/vaadin/data/provider/BackendDataProviderTest.java @@ -61,8 +61,9 @@ public class BackendDataProviderTest extends } @Override - protected void setSortOrder(List> sortOrder, + protected void setSortOrder(List sortOrder, Comparator comp) { getDataProvider().setSortOrders(sortOrder); } -} + +} \ No newline at end of file diff --git a/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java b/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java index 0aa9bb80ba..ebdcdd105c 100644 --- a/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java +++ b/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java @@ -2,7 +2,6 @@ package com.vaadin.data.provider; import static org.junit.Assert.assertTrue; -import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -49,25 +48,20 @@ public abstract class DataProviderTestBase> sortOrder, + protected abstract void setSortOrder(List sortOrder, Comparator comp); private Query> createQuery( - List> sortOrder, Comparator comp) { + List sortOrder, Comparator comp) { return createQuery(sortOrder, comp, null); } private Query> createQuery( - List> sortOrder, Comparator comp, + List sortOrder, Comparator comp, SerializablePredicate filter) { return new Query<>(0, Integer.MAX_VALUE, sortOrder, comp, filter); } - private Query> createQuery( - SerializablePredicate filter) { - return createQuery(Collections.emptyList(), null, filter); - } - // Tests start here. @Test diff --git a/server/src/test/java/com/vaadin/data/provider/ListDataProviderTest.java b/server/src/test/java/com/vaadin/data/provider/ListDataProviderTest.java index 7e32449628..1ffa444f50 100644 --- a/server/src/test/java/com/vaadin/data/provider/ListDataProviderTest.java +++ b/server/src/test/java/com/vaadin/data/provider/ListDataProviderTest.java @@ -218,7 +218,7 @@ public class ListDataProviderTest } @Override - protected void setSortOrder(List> sortOrder, + protected void setSortOrder(List sortOrder, Comparator comp) { SerializableComparator serializableComp = comp::compare; getDataProvider().setSortComparator(serializableComp); diff --git a/server/src/test/java/com/vaadin/data/provider/bov/DataProviderBoVTest.java b/server/src/test/java/com/vaadin/data/provider/bov/DataProviderBoVTest.java index cecb6bf511..b8d659fc47 100644 --- a/server/src/test/java/com/vaadin/data/provider/bov/DataProviderBoVTest.java +++ b/server/src/test/java/com/vaadin/data/provider/bov/DataProviderBoVTest.java @@ -26,7 +26,7 @@ import org.junit.Before; import org.junit.Test; import com.vaadin.data.provider.DataProvider; -import com.vaadin.data.provider.SortOrder; +import com.vaadin.data.provider.QuerySortOrder; import com.vaadin.shared.data.sort.SortDirection; /** @@ -133,7 +133,7 @@ public class DataProviderBoVTest { // First callback fetches items based on a query query -> { List sortOrders = new ArrayList<>(); - for (SortOrder queryOrder : query.getSortOrders()) { + for (QuerySortOrder queryOrder : query.getSortOrders()) { PersonService.PersonSort sort = personService .createSort( // The name of the sorted property -- 2.39.5