From dafc8310259a2e79bb203c7f786c9aba5354937b Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Thu, 19 Jan 2017 09:12:24 +0200 Subject: 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 --- .../vaadin/data/provider/BackEndDataProvider.java | 72 ++-------------------- 1 file changed, 5 insertions(+), 67 deletions(-) (limited to 'server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java') 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 * data provider data type * @param * data provider filter type */ -public class BackEndDataProvider extends AbstractDataProvider { - - private List> sortOrders = new ArrayList<>(); - - private final SerializableFunction, Stream> request; - private final SerializableToIntFunction> 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, Stream> request, - SerializableToIntFunction> 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 fetch(Query query) { - return request.apply(mixInSortOrders(query)); - } - - @Override - public int size(Query query) { - return sizeCallback.applyAsInt(mixInSortOrders(query)); - } - - private Query mixInSortOrders(Query query) { - Set sortedPropertyNames = query.getSortOrders().stream() - .map(SortOrder::getSorted).collect(Collectors.toSet()); - - List> 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 extends DataProvider { /** * Sets a list of sort orders to use as the default sorting for this data @@ -99,11 +42,7 @@ public class BackEndDataProvider extends AbstractDataProvider { * @param sortOrders * a list of sort orders to set, not null */ - public void setSortOrders(List> sortOrders) { - this.sortOrders = Objects.requireNonNull(sortOrders, - "Sort orders cannot be null"); - refreshAll(); - } + void setSortOrders(List> sortOrders); /** * Sets a single sort order to use as the default sorting for this data @@ -120,7 +59,7 @@ public class BackEndDataProvider extends AbstractDataProvider { * a sort order to set, or null to clear any * previously set sort orders */ - public void setSortOrder(SortOrder sortOrder) { + default void setSortOrder(SortOrder sortOrder) { if (sortOrder == null) { setSortOrders(Collections.emptyList()); } else { @@ -129,8 +68,7 @@ public class BackEndDataProvider extends AbstractDataProvider { } @Override - public boolean isInMemory() { + default boolean isInMemory() { return false; } - } -- cgit v1.2.3