From d0137a9877c735b70a0319f35e6326317f9f5c89 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Wed, 8 Feb 2017 15:27:37 +0200 Subject: [PATCH] Extract data provider callbacks to named and documented interfaces (#8508) Fixes #8488 --- .../data/provider/CallbackDataProvider.java | 86 +++++++++++++++---- .../vaadin/data/provider/DataProvider.java | 18 ++-- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/com/vaadin/data/provider/CallbackDataProvider.java b/server/src/main/java/com/vaadin/data/provider/CallbackDataProvider.java index 2f68e57e06..bd34cc939f 100644 --- a/server/src/main/java/com/vaadin/data/provider/CallbackDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/CallbackDataProvider.java @@ -15,12 +15,11 @@ */ package com.vaadin.data.provider; +import java.io.Serializable; import java.util.Objects; import java.util.stream.Stream; import com.vaadin.data.ValueProvider; -import com.vaadin.server.SerializableFunction; -import com.vaadin.server.SerializableToIntFunction; /** * Data provider that uses one callback for fetching items from a back end and @@ -35,8 +34,60 @@ import com.vaadin.server.SerializableToIntFunction; */ public class CallbackDataProvider extends AbstractBackEndDataProvider { - private final SerializableFunction, Stream> fetchCallback; - private final SerializableToIntFunction> sizeCallback; + /** + * Callback interface for fetching a stream of items from a backend based on + * a query. + * + * @param + * the type of the items to fetch + * @param + * the type of the optional filter in the query, + * Void if filtering is not supported + */ + @FunctionalInterface + public interface FetchCallback extends Serializable { + /** + * Fetches a stream of items based on a query. The query defines the + * paging of the items to fetch through {@link Query#getOffset()} and + * {@link Query#getLimit()}, the sorting through + * {@link Query#getSortOrders()} and optionally also any filtering to + * use through {@link Query#getFilter()}. + * + * @param query + * the query that defines which items to fetch + * @return a stream of items + */ + public Stream fetch(Query query); + } + + /** + * Callback interface for counting the number of items in a backend based on + * a query. + * + * @param + * the type of the items to count + * @param + * the type of the optional filter in the query, + * Void if filtering is not supported + */ + @FunctionalInterface + public interface CountCallback extends Serializable { + /** + * Counts the number of available items based on a query. The query + * optionally defines any filtering to use through + * {@link Query#getFilter()}. The query also contains information about + * paging and sorting although that information is generally not + * applicable for determining the number of items. + * + * @param query + * the query that defines which items to count + * @return the number of available items + */ + public int count(Query query); + } + + private final FetchCallback fetchCallback; + private final CountCallback countCallback; private final ValueProvider idGetter; /** @@ -46,17 +97,15 @@ public class CallbackDataProvider * @param fetchCallback * function that returns a stream of items from the back end for * a query - * @param sizeCallback + * @param countCallback * function that return the number of items in the back end for a * query * - * @see #CallbackDataProvider(SerializableFunction, - * SerializableToIntFunction, ValueProvider) + * @see #CallbackDataProvider(FetchCallback, CountCallback, ValueProvider) */ - public CallbackDataProvider( - SerializableFunction, Stream> fetchCallback, - SerializableToIntFunction> sizeCallback) { - this(fetchCallback, sizeCallback, t -> t); + public CallbackDataProvider(FetchCallback fetchCallback, + CountCallback countCallback) { + this(fetchCallback, countCallback, t -> t); } /** @@ -65,32 +114,31 @@ public class CallbackDataProvider * * @param fetchCallBack * function that requests data from back end based on query - * @param sizeCallback + * @param countCallback * function that returns the amount of data in back end for query * @param identifierGetter * function that returns the identifier for a given item */ - public CallbackDataProvider( - SerializableFunction, Stream> fetchCallBack, - SerializableToIntFunction> sizeCallback, + public CallbackDataProvider(FetchCallback fetchCallBack, + CountCallback countCallback, ValueProvider identifierGetter) { Objects.requireNonNull(fetchCallBack, "Request function can't be null"); - Objects.requireNonNull(sizeCallback, "Size callback can't be null"); + Objects.requireNonNull(countCallback, "Count callback can't be null"); Objects.requireNonNull(identifierGetter, "Identifier getter function can't be null"); this.fetchCallback = fetchCallBack; - this.sizeCallback = sizeCallback; + this.countCallback = countCallback; this.idGetter = identifierGetter; } @Override public Stream fetchFromBackEnd(Query query) { - return fetchCallback.apply(query); + return fetchCallback.fetch(query); } @Override protected int sizeInBackEnd(Query query) { - return sizeCallback.applyAsInt(query); + return countCallback.count(query); } @Override diff --git a/server/src/main/java/com/vaadin/data/provider/DataProvider.java b/server/src/main/java/com/vaadin/data/provider/DataProvider.java index f9c3c80146..6a5f9c0bb3 100644 --- a/server/src/main/java/com/vaadin/data/provider/DataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/DataProvider.java @@ -24,6 +24,8 @@ import java.util.stream.Stream; import com.vaadin.data.HasDataProvider; import com.vaadin.data.HasFilterableDataProvider; +import com.vaadin.data.provider.CallbackDataProvider.CountCallback; +import com.vaadin.data.provider.CallbackDataProvider.FetchCallback; import com.vaadin.server.SerializableBiFunction; import com.vaadin.server.SerializableFunction; import com.vaadin.server.SerializableToIntFunction; @@ -290,15 +292,15 @@ public interface DataProvider extends Serializable { * @param fetchCallback * function that returns a stream of items from the back end for * a query - * @param sizeCallback + * @param countCallback * function that returns the number of items in the back end for * a query * @return a new callback data provider */ public static CallbackDataProvider fromFilteringCallbacks( - SerializableFunction, Stream> fetchCallback, - SerializableToIntFunction> sizeCallback) { - return new CallbackDataProvider<>(fetchCallback, sizeCallback); + FetchCallback fetchCallback, + CountCallback countCallback) { + return new CallbackDataProvider<>(fetchCallback, countCallback); } /** @@ -311,14 +313,14 @@ public interface DataProvider extends Serializable { * @param fetchCallback * function that returns a stream of items from the back end for * a query - * @param sizeCallback + * @param countCallback * function that returns the number of items in the back end for * a query * @return a new callback data provider */ public static CallbackDataProvider fromCallbacks( - SerializableFunction, Stream> fetchCallback, - SerializableToIntFunction> sizeCallback) { - return fromFilteringCallbacks(fetchCallback, sizeCallback); + FetchCallback fetchCallback, + CountCallback countCallback) { + return fromFilteringCallbacks(fetchCallback, countCallback); } } -- 2.39.5