From 5b0919ce9e3f8899fb5b0c5331afd9ffa2a56c0c Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Mon, 5 Dec 2016 15:17:30 +0200 Subject: Add Listing.setItems(Stream) (#90) * Add Listings.setItems(Stream) Fixes vaadin/framework8-issues#447 Change-Id: I4d557d346117d308b80a20effbf5359bebe1e8bb --- server/src/main/java/com/vaadin/data/Listing.java | 27 +++++++++++++++++++++- .../java/com/vaadin/server/data/DataProvider.java | 24 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/data/Listing.java b/server/src/main/java/com/vaadin/data/Listing.java index 527c913912..0ecc13b3fb 100644 --- a/server/src/main/java/com/vaadin/data/Listing.java +++ b/server/src/main/java/com/vaadin/data/Listing.java @@ -17,7 +17,9 @@ package com.vaadin.data; import java.io.Serializable; import java.util.Collection; +import java.util.stream.Stream; +import com.vaadin.server.data.BackEndDataProvider; import com.vaadin.server.data.DataProvider; /** @@ -51,7 +53,7 @@ public interface Listing> extends Serializable { void setDataProvider(D dataProvider); /** - * Sets the collection of data items of this listing. + * Sets the data items of this listing provided as a collection. *

* Note for component developers: If the component * implementing this interface uses a custom data provider and/or filter @@ -83,4 +85,27 @@ public interface Listing> extends Serializable { setDataProvider((D) DataProvider.create(items)); } + /** + * Sets the data items of this listing provided as a stream. + *

+ * This is just a shorthand for {@link #setItems(Collection)}, by + * collecting all the items in the stream to a list. + *

+ * Using big streams is not recommended, you should instead use a + * lazy data provider. See {@link BackEndDataProvider} for more + * info. + *

+ * Note for component developers: If the component + * implementing this interface uses a custom data provider and/or filter + * types, this method should be overridden to provide the same functionality + * with the correct data provider type. This might require filter conversion + * or a completely custom implementation. + * + * @param streamOfItems + * the stream of data items to display, not {@code null} + */ + default void setItems(Stream streamOfItems) { + setDataProvider((D) DataProvider.create(streamOfItems)); + } + } diff --git a/server/src/main/java/com/vaadin/server/data/DataProvider.java b/server/src/main/java/com/vaadin/server/data/DataProvider.java index ef71378b8f..ad6185b51a 100644 --- a/server/src/main/java/com/vaadin/server/data/DataProvider.java +++ b/server/src/main/java/com/vaadin/server/data/DataProvider.java @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import java.util.Objects; +import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.server.SerializableFunction; @@ -168,4 +169,27 @@ public interface DataProvider extends Serializable { public static ListDataProvider create(T... items) { return new ListDataProvider<>(Arrays.asList(items)); } + + /** + * This method creates a new {@link ListDataProvider} from the given stream. + * The ListDataProvider collects all the items in the stream to a + * list. + *

+ * This is just a shorthand for using {@link #create(Collection)} after + * collecting the items in the stream to a list with e.g. + * {@code stream.collect(Collectors.toList));}. + *

+ * Using big streams is not recommended, you should instead use a + * lazy data provider. See {@link BackEndDataProvider} for more + * info. + * + * @param + * the data item type + * @param items + * a stream of data items, not {@code null} + * @return a new list data provider + */ + public static ListDataProvider create(Stream items) { + return new ListDataProvider<>(items.collect(Collectors.toList())); + } } -- cgit v1.2.3