aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
diff options
context:
space:
mode:
authorLeif Åstrand <legioth@gmail.com>2017-01-19 09:12:24 +0200
committerDenis <denis@vaadin.com>2017-01-19 09:12:24 +0200
commitdafc8310259a2e79bb203c7f786c9aba5354937b (patch)
treefb79a51b356e867f405238ec013faa58c6861930 /server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
parent67d69c8d5d6bb066c792cfaa65f2b68f6ec8cc2b (diff)
downloadvaadin-framework-dafc8310259a2e79bb203c7f786c9aba5354937b.tar.gz
vaadin-framework-dafc8310259a2e79bb203c7f786c9aba5354937b.zip
Change BackEndDataProvider into an interface (#8268)
* Change BackEndDataProvider into an interface BackEndDataProvider is now an interface with methods for setting sorting options based on SortOrder instances. AbstractBackEndDataProvider stores sorting options, combines them with the sorting provided in the query and invokes its own abstract fetch and size methods. CallbackDataProvider implements a BackEndDataProvider based on two lambdas. This is one of many steps towards #8245
Diffstat (limited to 'server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java')
-rw-r--r--server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java72
1 files changed, 5 insertions, 67 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 3606a99ade..96d5e54394 100644
--- a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
+++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java
@@ -15,75 +15,18 @@
*/
package com.vaadin.data.provider;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import java.util.Objects;
-import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import com.vaadin.server.SerializableFunction;
-import com.vaadin.server.SerializableToIntFunction;
/**
- * A {@link DataProvider} for any back end.
+ * A data provider that lazy loads items from a back end.
*
* @param <T>
* data provider data type
* @param <F>
* data provider filter type
*/
-public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> {
-
- private List<SortOrder<String>> sortOrders = new ArrayList<>();
-
- private final SerializableFunction<Query<T, F>, Stream<T>> request;
- private final SerializableToIntFunction<Query<T, F>> sizeCallback;
-
- /**
- * Constructs a new DataProvider to request data from an arbitrary back end
- * request function.
- *
- * @param request
- * function that requests data from back end based on query
- * @param sizeCallback
- * function that return the amount of data in back end for query
- */
- public BackEndDataProvider(
- SerializableFunction<Query<T, F>, Stream<T>> request,
- SerializableToIntFunction<Query<T, F>> sizeCallback) {
- Objects.requireNonNull(request, "Request function can't be null");
- Objects.requireNonNull(sizeCallback, "Size callback can't be null");
- this.request = request;
- this.sizeCallback = sizeCallback;
- }
-
- @Override
- public Stream<T> fetch(Query<T, F> query) {
- return request.apply(mixInSortOrders(query));
- }
-
- @Override
- public int size(Query<T, F> query) {
- return sizeCallback.applyAsInt(mixInSortOrders(query));
- }
-
- private Query<T, F> mixInSortOrders(Query<T, F> query) {
- Set<String> sortedPropertyNames = query.getSortOrders().stream()
- .map(SortOrder::getSorted).collect(Collectors.toSet());
-
- List<SortOrder<String>> combinedSortOrders = Stream
- .concat(query.getSortOrders().stream(),
- sortOrders.stream()
- .filter(order -> !sortedPropertyNames
- .contains(order.getSorted())))
- .collect(Collectors.toList());
-
- return new Query<>(query.getOffset(), query.getLimit(),
- combinedSortOrders, query.getInMemorySorting(),
- query.getFilter().orElse(null));
- }
+public interface BackEndDataProvider<T, F> extends DataProvider<T, F> {
/**
* Sets a list of sort orders to use as the default sorting for this data
@@ -99,11 +42,7 @@ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> {
* @param sortOrders
* a list of sort orders to set, not <code>null</code>
*/
- public void setSortOrders(List<SortOrder<String>> sortOrders) {
- this.sortOrders = Objects.requireNonNull(sortOrders,
- "Sort orders cannot be null");
- refreshAll();
- }
+ void setSortOrders(List<SortOrder<String>> sortOrders);
/**
* Sets a single sort order to use as the default sorting for this data
@@ -120,7 +59,7 @@ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> {
* a sort order to set, or <code>null</code> to clear any
* previously set sort orders
*/
- public void setSortOrder(SortOrder<String> sortOrder) {
+ default void setSortOrder(SortOrder<String> sortOrder) {
if (sortOrder == null) {
setSortOrders(Collections.emptyList());
} else {
@@ -129,8 +68,7 @@ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> {
}
@Override
- public boolean isInMemory() {
+ default boolean isInMemory() {
return false;
}
-
}