diff options
-rw-r--r-- | server/src/main/java/com/vaadin/data/Listing.java | 27 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/server/data/DataProvider.java | 24 |
2 files changed, 50 insertions, 1 deletions
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<T, D extends DataProvider<T, ?>> 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. * <p> * <strong>Note for component developers: </strong> If the component * implementing this interface uses a custom data provider and/or filter @@ -83,4 +85,27 @@ public interface Listing<T, D extends DataProvider<T, ?>> extends Serializable { setDataProvider((D) DataProvider.create(items)); } + /** + * Sets the data items of this listing provided as a stream. + * <p> + * This is just a shorthand for {@link #setItems(Collection)}, by + * <b>collecting all the items in the stream to a list</b>. + * <p> + * <strong>Using big streams is not recommended, you should instead use a + * lazy data provider.</strong> See {@link BackEndDataProvider} for more + * info. + * <p> + * <strong>Note for component developers: </strong> 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<T> 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<T, F> extends Serializable { public static <T> ListDataProvider<T> create(T... items) { return new ListDataProvider<>(Arrays.asList(items)); } + + /** + * This method creates a new {@link ListDataProvider} from the given stream. + * The ListDataProvider <b>collects all the items in the stream to a + * list</b>. + * <p> + * 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));}. + * <p> + * <strong>Using big streams is not recommended, you should instead use a + * lazy data provider.</strong> See {@link BackEndDataProvider} for more + * info. + * + * @param <T> + * the data item type + * @param items + * a stream of data items, not {@code null} + * @return a new list data provider + */ + public static <T> ListDataProvider<T> create(Stream<T> items) { + return new ListDataProvider<>(items.collect(Collectors.toList())); + } } |