summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksi Hietanen <aleksi@vaadin.com>2017-01-27 09:07:29 +0200
committerGitHub <noreply@github.com>2017-01-27 09:07:29 +0200
commita1cc08d7b98b09e45a01bde6abe13ffdcb200598 (patch)
tree002e876c08290f2a8e847169ab62f09e91efdefa
parentddbb619651a9073666274592359124ce271725ed (diff)
downloadvaadin-framework-a1cc08d7b98b09e45a01bde6abe13ffdcb200598.tar.gz
vaadin-framework-a1cc08d7b98b09e45a01bde6abe13ffdcb200598.zip
Implement GridSortOrder and SortOrderBuilders (#8338)
-rw-r--r--server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java17
-rw-r--r--server/src/main/java/com/vaadin/data/provider/GridSortOrder.java80
-rw-r--r--server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java52
-rw-r--r--server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java26
-rw-r--r--server/src/main/java/com/vaadin/data/provider/QuerySortOrderBuilder.java47
-rw-r--r--server/src/main/java/com/vaadin/data/provider/SortOrderBuilder.java99
-rw-r--r--server/src/main/java/com/vaadin/event/SortEvent.java22
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java39
-rw-r--r--server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java9
-rw-r--r--server/src/test/java/com/vaadin/data/provider/SortOrderBuildersTest.java66
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java22
11 files changed, 441 insertions, 38 deletions
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 b50b5c7fc8..13694010c7 100644
--- a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
+++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
@@ -17,6 +17,7 @@ package com.vaadin.data.provider;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
/**
* A data provider that lazy loads items from a back end.
@@ -45,6 +46,22 @@ public interface BackEndDataProvider<T, F> extends DataProvider<T, F> {
void setSortOrders(List<QuerySortOrder> sortOrders);
/**
+ * Sets the sort order to use, given a {@link QuerySortOrderBuilder}.
+ * Shorthand for {@code setSortOrders(builder.build())}.
+ *
+ * @see QuerySortOrderBuilder
+ *
+ * @param builder
+ * the sort builder to retrieve the sort order from
+ * @throws NullPointerException
+ * if builder is null
+ */
+ default void setSortOrders(QuerySortOrderBuilder builder) {
+ Objects.requireNonNull("Sort builder cannot be null.");
+ setSortOrders(builder.build());
+ }
+
+ /**
* Sets a single sort order to use as the default sorting for this data
* provider. This overrides the sorting set by any other method that
* manipulates the default sorting of this data provider.
diff --git a/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java b/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java
new file mode 100644
index 0000000000..ffb17d301d
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/provider/GridSortOrder.java
@@ -0,0 +1,80 @@
+/*
+ * 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;
+import com.vaadin.ui.Grid.Column;
+
+/**
+ * Sorting information for {@link Grid}.
+ *
+ * @param <T>
+ * the grid type
+ */
+public class GridSortOrder<T> extends SortOrder<Column<T, ?>> {
+
+ /**
+ * Construct sorting information for usage in a {@link Grid}.
+ *
+ * @param column
+ * the column to be sorted
+ * @param direction
+ * sorting direction
+ */
+ public GridSortOrder(Column<T, ?> column, SortDirection direction) {
+ super(column, direction);
+ }
+
+ /**
+ * Gets the column this sorting information is attached to.
+ *
+ * @return the column being sorted
+ */
+ @Override
+ public Column<T, ?> getSorted() {
+ return super.getSorted();
+ }
+
+ /**
+ * Creates a new grid sort builder with given sorting using ascending sort
+ * direction.
+ *
+ * @param by
+ * the column to sort by
+ * @param <T>
+ * the grid type
+ *
+ * @return the grid sort builder
+ */
+ public static <T> GridSortOrderBuilder<T> asc(Column<T, ?> by) {
+ return new GridSortOrderBuilder<T>().thenAsc(by);
+ }
+
+ /**
+ * Creates a new grid sort builder with given sorting using descending sort
+ * direction.
+ *
+ * @param by
+ * the column to sort by
+ * @param <T>
+ * the grid type
+ *
+ * @return the grid sort builder
+ */
+ public static <T> GridSortOrderBuilder<T> desc(Column<T, ?> by) {
+ return new GridSortOrderBuilder<T>().thenDesc(by);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java b/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java
new file mode 100644
index 0000000000..3aba2eba6b
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/provider/GridSortOrderBuilder.java
@@ -0,0 +1,52 @@
+/*
+ * 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;
+import com.vaadin.ui.Grid.Column;
+
+/**
+ * Helper classes with fluent API for constructing {@link GridSortOrder} lists.
+ * When the sort order is ready to be passed on, calling {@link #build()} will
+ * create the list of sort orders.
+ *
+ * @see GridSortOrder
+ * @see #thenAsc(Column)
+ * @see #thenDesc(Column)
+ * @see #build()
+ *
+ * @param <T>
+ * the type of the grid
+ */
+public class GridSortOrderBuilder<T>
+ extends SortOrderBuilder<GridSortOrder<T>, Column<T, ?>> {
+
+ @Override
+ public GridSortOrderBuilder<T> thenAsc(Column<T, ?> by) {
+ return (GridSortOrderBuilder<T>) super.thenAsc(by);
+ }
+
+ @Override
+ public GridSortOrderBuilder<T> thenDesc(Column<T, ?> by) {
+ return (GridSortOrderBuilder<T>) super.thenDesc(by);
+ }
+
+ @Override
+ protected GridSortOrder<T> createSortOrder(Column<T, ?> by,
+ SortDirection direction) {
+ return new GridSortOrder<>(by, direction);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java
index ca8d743fae..7f0a76bf91 100644
--- a/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java
+++ b/server/src/main/java/com/vaadin/data/provider/QuerySortOrder.java
@@ -45,4 +45,30 @@ public class QuerySortOrder extends SortOrder<String> {
public String getSorted() {
return super.getSorted();
}
+
+ /**
+ * Creates a new query sort builder with given sorting using ascending sort
+ * direction.
+ *
+ * @param by
+ * the string to sort by
+ *
+ * @return the query sort builder
+ */
+ public static QuerySortOrderBuilder asc(String by) {
+ return new QuerySortOrderBuilder().thenAsc(by);
+ }
+
+ /**
+ * Creates a new query sort builder with given sorting using descending sort
+ * direction.
+ *
+ * @param by
+ * the string to sort by
+ *
+ * @return the query sort builder
+ */
+ public static QuerySortOrderBuilder desc(String by) {
+ return new QuerySortOrderBuilder().thenDesc(by);
+ }
}
diff --git a/server/src/main/java/com/vaadin/data/provider/QuerySortOrderBuilder.java b/server/src/main/java/com/vaadin/data/provider/QuerySortOrderBuilder.java
new file mode 100644
index 0000000000..1f8d1e1aab
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/provider/QuerySortOrderBuilder.java
@@ -0,0 +1,47 @@
+/*
+ * 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;
+
+/**
+ * Helper classes with fluent API for constructing {@link QuerySortOrder} lists.
+ * When the sort order is ready to be passed on, calling {@link #build()} will
+ * create the list of sort orders.
+ *
+ * @see QuerySortOrder
+ * @see #thenDesc(String)
+ * @see #thenDesc(String)
+ * @see #build()
+ */
+public class QuerySortOrderBuilder extends SortOrderBuilder<QuerySortOrder, String> {
+
+ @Override
+ public QuerySortOrderBuilder thenAsc(String by) {
+ return (QuerySortOrderBuilder) super.thenAsc(by);
+ }
+
+ @Override
+ public QuerySortOrderBuilder thenDesc(String by) {
+ return (QuerySortOrderBuilder) super.thenDesc(by);
+ }
+
+ @Override
+ protected QuerySortOrder createSortOrder(String by,
+ SortDirection direction) {
+ return new QuerySortOrder(by, direction);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/data/provider/SortOrderBuilder.java b/server/src/main/java/com/vaadin/data/provider/SortOrderBuilder.java
new file mode 100644
index 0000000000..29488b4415
--- /dev/null
+++ b/server/src/main/java/com/vaadin/data/provider/SortOrderBuilder.java
@@ -0,0 +1,99 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.vaadin.shared.data.sort.SortDirection;
+
+/**
+ * Base class for helper classes with fluent API for constructing sort order
+ * lists. When the sort order is ready to be passed on, calling {@link #build()}
+ * will create the list of sort orders.
+ *
+ * @param <T>
+ * the sort order type
+ * @param <V>
+ * the sorting type
+ *
+ * @see SortOrderBuilder#thenAsc(Object)
+ * @see SortOrderBuilder#thenDesc(Object)
+ * @see #build()
+ */
+public abstract class SortOrderBuilder<T extends SortOrder<V>, V>
+ implements Serializable {
+
+ private final List<T> sortOrders = new ArrayList<>();
+
+ /**
+ * Appends sorting with ascending sort direction.
+ *
+ * @param by
+ * the object to sort by
+ * @return this sort builder
+ */
+ public SortOrderBuilder<T, V> thenAsc(V by) {
+ return append(createSortOrder(by, SortDirection.ASCENDING));
+ }
+
+ /**
+ * Appends sorting with descending sort direction.
+ *
+ * @param by
+ * the object to sort by
+ * @return this sort builder
+ */
+ public SortOrderBuilder<T, V> thenDesc(V by) {
+ return append(createSortOrder(by, SortDirection.DESCENDING));
+ }
+
+ /**
+ * Returns an unmodifiable copy of the list of current sort orders in this
+ * sort builder.
+ *
+ * @return an unmodifiable sort order list
+ */
+ public final List<T> build() {
+ return Collections.unmodifiableList(new ArrayList<>(sortOrders));
+ }
+
+ /**
+ * Creates a sort order object with the given parameters.
+ *
+ * @param by
+ * the object to sort by
+ * @param direction
+ * the sort direction
+ *
+ * @return the sort order object
+ */
+ protected abstract T createSortOrder(V by, SortDirection direction);
+
+ /**
+ * Append a sort order to {@code sortOrders}.
+ *
+ * @param sortOrder
+ * the sort order to append
+ * @return this
+ */
+ private final SortOrderBuilder<T, V> append(T sortOrder) {
+ sortOrders.add(sortOrder);
+ return this;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/event/SortEvent.java b/server/src/main/java/com/vaadin/event/SortEvent.java
index b6948fd711..cca7bbd3b6 100644
--- a/server/src/main/java/com/vaadin/event/SortEvent.java
+++ b/server/src/main/java/com/vaadin/event/SortEvent.java
@@ -30,15 +30,14 @@ import com.vaadin.ui.Component;
* @see SortListener
* @see SortOrder
* @param <T>
- * the type of the sorting information, usually a String (field id)
- * or a {@link java.util.Comparator}.
+ * the type of the sorting information
*
* @since 8.0
* @author Vaadin Ltd
*/
-public class SortEvent<T> extends Component.Event {
+public class SortEvent<T extends SortOrder<?>> extends Component.Event {
- private final List<SortOrder<T>> sortOrder;
+ private final List<T> sortOrder;
private final boolean userOriginated;
/**
@@ -52,7 +51,7 @@ public class SortEvent<T> extends Component.Event {
* <code>true</code> if event is a result of user interaction,
* <code>false</code> if from API call
*/
- public SortEvent(Component source, List<SortOrder<T>> sortOrder,
+ public SortEvent(Component source, List<T> sortOrder,
boolean userOriginated) {
super(source);
this.sortOrder = sortOrder;
@@ -64,7 +63,7 @@ public class SortEvent<T> extends Component.Event {
*
* @return the sort order list
*/
- public List<SortOrder<T>> getSortOrder() {
+ public List<T> getSortOrder() {
return sortOrder;
}
@@ -81,11 +80,10 @@ public class SortEvent<T> extends Component.Event {
* Listener for sort order change events.
*
* @param <T>
- * the type of the sorting information, usually a String (field
- * id) or a {@link java.util.Comparator}.
+ * the type of the sorting information
*/
@FunctionalInterface
- public interface SortListener<T> extends Serializable {
+ public interface SortListener<T extends SortOrder<?>> extends Serializable {
/**
* Called when the sort order has changed.
*
@@ -100,10 +98,9 @@ public class SortEvent<T> extends Component.Event {
* SortEvents}.
*
* @param <T>
- * the type of the sorting information, usually a String (field
- * id) or a {@link java.util.Comparator}.
+ * the type of the sorting information
*/
- public interface SortNotifier<T> extends Serializable {
+ public interface SortNotifier<T extends SortOrder<?>> extends Serializable {
/**
* Adds a sort order change listener that gets notified when the sort
* order changes.
@@ -113,6 +110,5 @@ public class SortEvent<T> extends Component.Event {
* @return a registration object for removing the listener
*/
public Registration addSortListener(SortListener<T> listener);
-
}
}
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index b3410c57f0..16e452fbea 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -46,9 +46,10 @@ import com.vaadin.data.HasDataProvider;
import com.vaadin.data.ValueProvider;
import com.vaadin.data.provider.DataCommunicator;
import com.vaadin.data.provider.DataProvider;
+import com.vaadin.data.provider.GridSortOrder;
+import com.vaadin.data.provider.GridSortOrderBuilder;
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;
import com.vaadin.event.SortEvent;
@@ -124,7 +125,7 @@ import elemental.json.JsonValue;
* the grid bean type
*/
public class Grid<T> extends AbstractListing<T> implements HasComponents,
- HasDataProvider<T>, SortNotifier<Grid.Column<T, ?>> {
+ HasDataProvider<T>, SortNotifier<GridSortOrder<T>> {
@Deprecated
private static final Method COLUMN_REORDER_METHOD = ReflectTools.findMethod(
@@ -559,11 +560,11 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
assert columnInternalIds.length == directions.length : "Column and sort direction counts don't match.";
- List<SortOrder<Column<T, ?>>> list = new ArrayList<>(
+ List<GridSortOrder<T>> list = new ArrayList<>(
directions.length);
for (int i = 0; i < columnInternalIds.length; ++i) {
Column<T, ?> column = columnKeys.get(columnInternalIds[i]);
- list.add(new SortOrder<>(column, directions[i]));
+ list.add(new GridSortOrder<>(column, directions[i]));
}
setSortOrder(list, isUserOriginated);
}
@@ -1790,7 +1791,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
private final Map<String, Column<T, ?>> columnKeys = new HashMap<>();
private final Map<String, Column<T, ?>> columnIds = new HashMap<>();
- private final List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
+ private final List<GridSortOrder<T>> sortOrder = new ArrayList<>();
private final DetailsManager<T> detailsManager;
private final Set<Component> extensionComponents = new HashSet<>();
private StyleGenerator<T> styleGenerator = item -> null;
@@ -2783,7 +2784,8 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
*/
public void sort(Column<T, ?> column, SortDirection direction) {
setSortOrder(
- Collections.singletonList(new SortOrder<>(column, direction)));
+ Collections
+ .singletonList(new GridSortOrder<>(column, direction)));
}
/**
@@ -2803,11 +2805,27 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
* @throws IllegalArgumentException
* if order is null
*/
- public void setSortOrder(List<SortOrder<Column<T, ?>>> order) {
+ public void setSortOrder(List<GridSortOrder<T>> order) {
setSortOrder(order, false);
}
/**
+ * Sets the sort order to use, given a {@link GridSortOrderBuilder}.
+ * Shorthand for {@code setSortOrder(builder.build())}.
+ *
+ * @see GridSortOrderBuilder
+ *
+ * @param builder
+ * the sort builder to retrieve the sort order from
+ * @throws NullPointerException
+ * if builder is null
+ */
+ public void setSortOrder(GridSortOrderBuilder<T> builder) {
+ Objects.requireNonNull(builder, "Sort builder cannot be null");
+ setSortOrder(builder.build());
+ }
+
+ /**
* Adds a sort order change listener that gets notified when the sort order
* changes.
*
@@ -2815,7 +2833,8 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
* the sort order change listener to add
*/
@Override
- public Registration addSortListener(SortListener<Column<T, ?>> listener) {
+ public Registration addSortListener(
+ SortListener<GridSortOrder<T>> listener) {
return addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD);
}
@@ -2824,7 +2843,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
*
* @return a sort order list
*/
- public List<SortOrder<Column<T, ?>>> getSortOrder() {
+ public List<GridSortOrder<T>> getSortOrder() {
return Collections.unmodifiableList(sortOrder);
}
@@ -3172,7 +3191,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
return column.getInternalId();
}
- private void setSortOrder(List<SortOrder<Column<T, ?>>> order,
+ private void setSortOrder(List<GridSortOrder<T>> order,
boolean userOriginated) {
Objects.requireNonNull(order, "Sort order list cannot be null");
sortOrder.clear();
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 ebdcdd105c..84f011abf8 100644
--- a/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java
+++ b/server/src/test/java/com/vaadin/data/provider/DataProviderTestBase.java
@@ -82,7 +82,8 @@ public abstract class DataProviderTestBase<D extends DataProvider<StrBean, Seria
.thenComparing(StrBean::getId);
List<StrBean> list = dataProvider
- .fetch(createQuery(Sort.asc("value").thenAsc("randomNumber")
+ .fetch(createQuery(
+ QuerySortOrder.asc("value").thenAsc("randomNumber")
.thenAsc("id").build(), comp))
.collect(Collectors.toList());
@@ -104,10 +105,10 @@ public abstract class DataProviderTestBase<D extends DataProvider<StrBean, Seria
public void testDefaultSortWithSpecifiedPostSort() {
Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
.thenComparing(Comparator.comparing(StrBean::getId).reversed());
- setSortOrder(Sort.asc("value").thenDesc("id").build(), comp);
+ setSortOrder(QuerySortOrder.asc("value").thenDesc("id").build(), comp);
List<StrBean> list = dataProvider
- .fetch(createQuery(Sort.asc("randomNumber").build(),
+ .fetch(createQuery(QuerySortOrder.asc("randomNumber").build(),
Comparator.comparing(StrBean::getRandomNumber)))
.collect(Collectors.toList());
@@ -136,7 +137,7 @@ public abstract class DataProviderTestBase<D extends DataProvider<StrBean, Seria
@Test
public void testDefaultSortWithFunction() {
- setSortOrder(Sort.asc("value").build(),
+ setSortOrder(QuerySortOrder.asc("value").build(),
Comparator.comparing(StrBean::getValue));
List<StrBean> list = dataProvider.fetch(new Query<>())
diff --git a/server/src/test/java/com/vaadin/data/provider/SortOrderBuildersTest.java b/server/src/test/java/com/vaadin/data/provider/SortOrderBuildersTest.java
new file mode 100644
index 0000000000..e289686040
--- /dev/null
+++ b/server/src/test/java/com/vaadin/data/provider/SortOrderBuildersTest.java
@@ -0,0 +1,66 @@
+package com.vaadin.data.provider;
+
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.shared.data.sort.SortDirection;
+import com.vaadin.ui.Grid;
+import com.vaadin.ui.Grid.Column;
+import com.vaadin.ui.renderers.NumberRenderer;
+
+public class SortOrderBuildersTest {
+
+ @Test
+ public void gridSortOrderBuilder() {
+ Grid<String> grid = new Grid<>();
+ Column<String, String> col1 = grid.addColumn(string -> string);
+ Column<String, Number> col2 = grid.addColumn(string -> 1,
+ new NumberRenderer());
+ Column<String, ?> col3 = grid
+ .addColumn(string -> LocalDate.of(0, 0, 0));
+
+ // construct with asc
+ verifySortOrders(
+ Arrays.asList(
+ new GridSortOrder<>(col1, SortDirection.ASCENDING),
+ new GridSortOrder<>(col2, SortDirection.DESCENDING),
+ new GridSortOrder<>(col3, SortDirection.ASCENDING)),
+ GridSortOrder.asc(col1).thenDesc(col2).thenAsc(col3).build());
+ // construct with desc
+ verifySortOrders(
+ Arrays.asList(
+ new GridSortOrder<>(col1, SortDirection.DESCENDING),
+ new GridSortOrder<>(col2, SortDirection.DESCENDING),
+ new GridSortOrder<>(col3, SortDirection.ASCENDING)),
+ GridSortOrder.desc(col1).thenDesc(col2).thenAsc(col3).build());
+ }
+
+ @Test
+ public void querySortOrderBuilder() {
+ verifySortOrders(
+ Arrays.asList(new QuerySortOrder("a", SortDirection.ASCENDING),
+ new QuerySortOrder("b", SortDirection.DESCENDING),
+ new QuerySortOrder("c", SortDirection.ASCENDING)),
+ QuerySortOrder.asc("a").thenDesc("b").thenAsc("c").build());
+ verifySortOrders(
+ Arrays.asList(new QuerySortOrder("a", SortDirection.DESCENDING),
+ new QuerySortOrder("b", SortDirection.DESCENDING),
+ new QuerySortOrder("c", SortDirection.ASCENDING)),
+ QuerySortOrder.desc("a").thenDesc("b").thenAsc("c").build());
+ }
+
+ private <T extends SortOrder<?>> void verifySortOrders(List<T> order1,
+ List<T> order2) {
+ Assert.assertEquals(order1.size(), order2.size());
+ for (int i = 0; i < order1.size(); i++) {
+ Assert.assertEquals(order1.get(i).getDirection(),
+ order2.get(i).getDirection());
+ Assert.assertEquals(order1.get(i).getSorted(),
+ order1.get(i).getSorted());
+ }
+ }
+}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java
index 73ef2fceeb..601a63115e 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java
+++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java
@@ -16,7 +16,7 @@ import org.junit.Before;
import org.junit.Test;
import com.vaadin.data.ValueProvider;
-import com.vaadin.data.provider.SortOrder;
+import com.vaadin.data.provider.GridSortOrder;
import com.vaadin.event.selection.SelectionEvent;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.grid.HeightMode;
@@ -179,7 +179,7 @@ public class GridTest {
Column<String, ?> column = grid.getColumns().get(1);
grid.sort(column);
- SortOrder<Column<String, ?>> sortOrder = grid.getSortOrder().get(0);
+ GridSortOrder<String> sortOrder = grid.getSortOrder().get(0);
Assert.assertEquals(column, sortOrder.getSorted());
Assert.assertEquals(SortDirection.ASCENDING, sortOrder.getDirection());
}
@@ -189,7 +189,7 @@ public class GridTest {
Column<String, ?> column = grid.getColumns().get(1);
grid.sort(column, SortDirection.DESCENDING);
- SortOrder<Column<String, ?>> sortOrder = grid.getSortOrder().get(0);
+ GridSortOrder<String> sortOrder = grid.getSortOrder().get(0);
Assert.assertEquals(column, sortOrder.getSorted());
Assert.assertEquals(SortDirection.DESCENDING, sortOrder.getDirection());
}
@@ -198,12 +198,12 @@ public class GridTest {
public void setSortOrder() {
Column<String, ?> column1 = grid.getColumns().get(1);
Column<String, ?> column2 = grid.getColumns().get(2);
- List<SortOrder<Column<String, ?>>> order = Arrays.asList(
- new SortOrder<>(column2, SortDirection.DESCENDING),
- new SortOrder<>(column1, SortDirection.ASCENDING));
+ List<GridSortOrder<String>> order = Arrays.asList(
+ new GridSortOrder<>(column2, SortDirection.DESCENDING),
+ new GridSortOrder<>(column1, SortDirection.ASCENDING));
grid.setSortOrder(order);
- List<SortOrder<Column<String, ?>>> sortOrder = grid.getSortOrder();
+ List<GridSortOrder<String>> sortOrder = grid.getSortOrder();
Assert.assertEquals(column2, sortOrder.get(0).getSorted());
Assert.assertEquals(SortDirection.DESCENDING,
sortOrder.get(0).getDirection());
@@ -228,7 +228,7 @@ public class GridTest {
Column<String, ?> column1 = grid.getColumns().get(1);
Column<String, ?> column2 = grid.getColumns().get(2);
- List<SortOrder<Column<String, ?>>> list = new ArrayList<>();
+ List<GridSortOrder<String>> list = new ArrayList<>();
AtomicReference<Boolean> fired = new AtomicReference<>();
grid.addSortListener(event -> {
Assert.assertTrue(list.isEmpty());
@@ -241,9 +241,9 @@ public class GridTest {
Assert.assertEquals(SortDirection.DESCENDING,
list.get(0).getDirection());
- List<SortOrder<Column<String, ?>>> order = Arrays.asList(
- new SortOrder<>(column2, SortDirection.DESCENDING),
- new SortOrder<>(column1, SortDirection.ASCENDING));
+ List<GridSortOrder<String>> order = Arrays.asList(
+ new GridSortOrder<>(column2, SortDirection.DESCENDING),
+ new GridSortOrder<>(column1, SortDirection.ASCENDING));
list.clear();
grid.setSortOrder(order);