Flow.Publisher.
There are convenience methods for creating Flow.Publisher from in-memory data.
The general-purpose abstraction is to use a Tube and the create(TubeConfiguration, Consumer)
factory method.
-
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <T> Flow.Publisher<T>create(TubeConfiguration configuration, Consumer<Tube<T>> tubeConsumer) Create a newFlow.Publisherwith the general-purposeTubeAPI.static <T> Flow.Publisher<T>empty()Create an emptyFlow.Publisherthat completes upon subscription without ever sending any item.static <T> Flow.Publisher<T>fromCompletionStage(Supplier<CompletionStage<T>> completionStageSupplier) Create aFlow.Publisherfrom aCompletionStage.static <T> Flow.Publisher<T>fromFailure(Throwable failure) Create aFlow.Publisherfrom a known failure.static <S,T> Flow.Publisher<T> fromGenerator(Supplier<S> stateSupplier, Function<S, Iterator<T>> generator) Create aFlow.Publisherfrom a generator over some state.static <T> Flow.Publisher<T>fromItems(T... items) Create aFlow.Publisherfrom existing items.static <T> Flow.Publisher<T>fromIterable(Iterable<T> iterable) Create aFlow.Publisherfrom an iterable object.static <T> Flow.Publisher<T>fromStream(Supplier<Stream<T>> supplier) Create aFlow.Publisherfrom aStream.static <T> CompletionStage<Optional<T>>toCompletionStage(Flow.Publisher<T> publisher) Create aCompletionStagefrom aFlow.Publisher.
-
Method Details
-
fromItems
Create aFlow.Publisherfrom existing items.- Type Parameters:
T- the items type- Parameters:
items- the existing items, cannot be anull array- Returns:
- a new
Flow.Publisher
-
fromIterable
Create aFlow.Publisherfrom an iterable object.Note that this assumes an in-memory, non-blocking
Iterator. Do not try to force an iterator as a way to bridge an API withFlow.Publisheras it does not behave like an in-memory data structure.- Type Parameters:
T- the items type- Parameters:
iterable- the iterable object, cannot benull- Returns:
- a nes
Flow.Publisher
-
fromStream
Create aFlow.Publisherfrom aStream.Note that this assumes an in-memory, non-blocking data structure, just like
fromIterable(Iterable). Also note that aStreamcan only be traversed once, hence the use of a supplier because multiple subscriptions would fail.- Type Parameters:
T- the items type- Parameters:
supplier- the stream supplier, cannot benull- Returns:
- a new
Flow.Publisher
-
fromGenerator
static <S,T> Flow.Publisher<T> fromGenerator(Supplier<S> stateSupplier, Function<S, Iterator<T>> generator) Create aFlow.Publisherfrom a generator over some state.Note that this assumes an in-memory, non-blocking data structure, just like
fromIterable(Iterable).- Type Parameters:
S- the initial state typeT- the items type- Parameters:
stateSupplier- the initial state supplier, cannot benullbut can supplynullgenerator- a generator function over the initial state and an iterator, cannot benull, cannot yieldnull- Returns:
- a new
Flow.Publisher
-
fromCompletionStage
static <T> Flow.Publisher<T> fromCompletionStage(Supplier<CompletionStage<T>> completionStageSupplier) Create aFlow.Publisherfrom aCompletionStage.- Type Parameters:
T- the item type- Parameters:
completionStageSupplier- the completion stage supplier, cannot benull, cannot yieldnull- Returns:
- a new
Flow.Publisher
-
toCompletionStage
Create aCompletionStagefrom aFlow.Publisher.The
Flow.Publisheris requested exactly 1 element and the subscription is cancelled after it has been received.- Type Parameters:
T- the item type- Parameters:
publisher- the publisher, cannot benull- Returns:
- a new
CompletionStage
-
fromFailure
Create aFlow.Publisherfrom a known failure.- Type Parameters:
T- the items type- Parameters:
failure- the failure, cannot benull- Returns:
- a new
Flow.Publisher
-
empty
Create an emptyFlow.Publisherthat completes upon subscription without ever sending any item.- Type Parameters:
T- the items type- Returns:
- a new
Flow.Publisher
-
create
static <T> Flow.Publisher<T> create(TubeConfiguration configuration, Consumer<Tube<T>> tubeConsumer) Create a newFlow.Publisherwith the general-purposeTubeAPI.- Type Parameters:
T- the items type- Parameters:
configuration- the tube configurationtubeConsumer- the tube consumer, cannot benull- Returns:
- a new
Flow.Publisher
-