-
- Type Parameters:
T- item type
- All Superinterfaces:
Flow.Publisher<T>,Subscribable<T>
- All Known Subinterfaces:
HybridProcessor<T,R>
- All Known Implementing Classes:
ConcatPublisher,MultiCoupledProcessor,MultiDistinctProcessor,MultiDropWhileProcessor,MultiFilterProcessor,MultiFlatMapProcessor,MultiLimitProcessor,MultiMapProcessor,MultiOnErrorResumeProcessor,MultiPeekProcessor,MultiSkipProcessor,MultiTakeWhileProcessor,MultiTappedProcessor
public interface Multi<T> extends Subscribable<T>
Multiple items publisher facility.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default <U> Single<U>collect(Collector<T,U> collector)default <U> Single<U>collect(Supplier<U> collectionSupplier, BiConsumer<U,T> accumulator)default Single<List<T>>collectList()default <A,R>
Single<R>collectStream(Collector<T,A,R> collector)Collects up upstream items with the help of a the callbacks of aCollector.static <T> Multi<T>concat(Multi<T> firstMulti, Multi<T> secondMulti)Concat streams to one.default Multi<T>defaultIfEmpty(T defaultItem)Signals the default item if the upstream is empty.static <T> Multi<T>defer(Supplier<? extends Flow.Publisher<? extends T>> supplier)Call the given supplier function for each individual downstream Subscriber to return a Flow.Publisher to subscribe to.default Multi<T>distinct()Filter out all duplicates.default Multi<T>dropWhile(Predicate<? super T> predicate)Drop the longest prefix of elements from this stream that satisfy the given predicate.static <T> Multi<T>empty()Get aMultiinstance that completes immediately.static <T> Multi<T>error(Throwable error)Create aMultiinstance that reports the given exception to its subscriber(s).default Multi<T>filter(Predicate<T> predicate)Filter stream items with provided predicate.default Single<T>first()default <U> Multi<U>flatMap(Function<T,Flow.Publisher<U>> publisherMapper)Transform item with supplied function and flatten resultingFlow.Publisherto downstream.default <U> Multi<U>flatMap(Function<T,Flow.Publisher<U>> mapper, long maxConcurrency, boolean delayErrors, long prefetch)Transform item with supplied function and flatten resultingFlow.Publisherto downstream while limiting the maximum number of concurrent innerFlow.Publishers and their in-flight item count, optionally aggregating and delaying all errors until all sources terminate.default <U> Multi<U>flatMapIterable(Function<? super T,? extends Iterable<? extends U>> iterableMapper)Transform item with supplied function and flatten resultingIterableto downstream.default <U> Multi<U>flatMapIterable(Function<? super T,? extends Iterable<? extends U>> iterableMapper, int prefetch)Transform item with supplied function and flatten resultingIterableto downstream.default voidforEach(Consumer<T> consumer)Terminal stage, invokes provided consumer for every item in the stream.static <T> Multi<T>from(Iterable<T> iterable)Create aMultiinstance that publishes the given iterable.static <T> Multi<T>from(Flow.Publisher<T> source)Create aMultiinstance wrapped around the given publisher.static <T> Multi<T>from(Stream<T> stream)static Multi<Long>interval(long initialDelay, long period, TimeUnit unit, ScheduledExecutorService executor)Signal 0L after an initial delay, then 1L, 2L and so on periodically to the downstream.static Multi<Long>interval(long period, TimeUnit unit, ScheduledExecutorService executor)Signal 0L, 1L and so on periodically to the downstream.static <T> Multi<T>just(Collection<T> items)Create aMultiinstance that publishes the given items to a single subscriber.static <T> Multi<T>just(T... items)Create aMultiinstance that publishes the given items to a single subscriber.default Multi<T>limit(long limit)Limit stream to allow only specified number of items to pass.default <U> Multi<U>map(Mapper<T,U> mapper)static <T> Multi<T>never()Get aMultiinstance that never completes.default Multi<T>onComplete(Runnable onTerminate)Executes givenRunnablewhen onComplete signal is received.default Multi<T>onError(Consumer<Throwable> onErrorConsumer)Executes givenRunnablewhen onError signal is received.default Multi<T>onErrorResume(Function<? super Throwable,? extends T> onError)Functionproviding one item to be submitted as onNext in case of onError signal is received.default Multi<T>onErrorResumeWith(Function<? super Throwable,? extends Flow.Publisher<? extends T>> onError)Resume stream from supplied publisher if onError signal is intercepted.default Multi<T>onTerminate(Runnable onTerminate)Executes givenRunnablewhen any of signals onComplete, onCancel or onError is received.default Multi<T>peek(Consumer<T> consumer)Invoke provided consumer for every item in stream.static Multi<Integer>range(int start, int count)Emits a range of ever increasing integers.static Multi<Long>rangeLong(long start, long count)Emits a range of ever increasing longs.default Single<T>reduce(BiFunction<T,T,T> reducer)Combine subsequent items via a callback function and emit the final value result as a Single.default <R> Single<R>reduce(Supplier<? extends R> supplier, BiFunction<R,T,R> reducer)Combine every upstream item with an accumulator value to produce a new accumulator value and emit the final accumulator value as a Single.static <T> Multi<T>singleton(T item)Create aMultithat emits a pre-existing item and then completes.default Multi<T>skip(long skip)Skip first n items, all the others are emitted.default Multi<T>switchIfEmpty(Flow.Publisher<T> other)Switch to the other publisher if the upstream is empty.default <U> Multi<T>takeUntil(Flow.Publisher<U> other)Relay upstream items until the other source signals an item or completes.default Multi<T>takeWhile(Predicate<T> predicate)Take the longest prefix of elements from this stream that satisfy the given predicate.static Multi<Long>timer(long time, TimeUnit unit, ScheduledExecutorService executor)Signal 0L and complete the sequence after the given time elapsed.-
Methods inherited from interface java.util.concurrent.Flow.Publisher
subscribe
-
Methods inherited from interface io.helidon.common.reactive.Subscribable
subscribe, subscribe, subscribe, subscribe
-
-
-
-
Method Detail
-
defer
static <T> Multi<T> defer(Supplier<? extends Flow.Publisher<? extends T>> supplier)
Call the given supplier function for each individual downstream Subscriber to return a Flow.Publisher to subscribe to.- Type Parameters:
T- the element type of the sequence- Parameters:
supplier- the callback to return a Flow.Publisher for each Subscriber- Returns:
- Multi
- Throws:
NullPointerException- ifsupplierisnull
-
map
default <U> Multi<U> map(Mapper<T,U> mapper)
- Type Parameters:
U- mapped item type- Parameters:
mapper- mapper- Returns:
- Multi
- Throws:
NullPointerException- if mapper isnull
-
defaultIfEmpty
default Multi<T> defaultIfEmpty(T defaultItem)
Signals the default item if the upstream is empty.- Parameters:
defaultItem- the item to signal if the upstream is empty- Returns:
- Multi
- Throws:
NullPointerException- ifdefaultItemisnull
-
switchIfEmpty
default Multi<T> switchIfEmpty(Flow.Publisher<T> other)
Switch to the other publisher if the upstream is empty.- Parameters:
other- the publisher to switch to if the upstream is empty.- Returns:
- Multi
- Throws:
NullPointerException- ifotherisnull
-
peek
default Multi<T> peek(Consumer<T> consumer)
Invoke provided consumer for every item in stream.- Parameters:
consumer- consumer to be invoked- Returns:
- Multi
-
filter
default Multi<T> filter(Predicate<T> predicate)
Filter stream items with provided predicate.- Parameters:
predicate- predicate to filter stream with- Returns:
- Multi
-
takeWhile
default Multi<T> takeWhile(Predicate<T> predicate)
Take the longest prefix of elements from this stream that satisfy the given predicate. As long as predicate returns true, items from upstream are sent to downstream, when predicate returns false stream is completed.- Parameters:
predicate- predicate to filter stream with- Returns:
- Multi
-
dropWhile
default Multi<T> dropWhile(Predicate<? super T> predicate)
Drop the longest prefix of elements from this stream that satisfy the given predicate. As long as predicate returns true, items from upstream are NOT sent to downstream but being dropped, predicate is never called again after it returns false for the first time.- Parameters:
predicate- predicate to filter stream with- Returns:
- Multi
-
limit
default Multi<T> limit(long limit)
Limit stream to allow only specified number of items to pass.- Parameters:
limit- with expected number of items to be produced- Returns:
- Multi
-
skip
default Multi<T> skip(long skip)
Skip first n items, all the others are emitted.- Parameters:
skip- number of items to be skipped- Returns:
- Multi
-
flatMap
default <U> Multi<U> flatMap(Function<T,Flow.Publisher<U>> publisherMapper)
Transform item with supplied function and flatten resultingFlow.Publisherto downstream.- Type Parameters:
U- output item type- Parameters:
publisherMapper-Functionreceiving item as parameter and returningFlow.Publisher- Returns:
- Multi
-
flatMap
default <U> Multi<U> flatMap(Function<T,Flow.Publisher<U>> mapper, long maxConcurrency, boolean delayErrors, long prefetch)
Transform item with supplied function and flatten resultingFlow.Publisherto downstream while limiting the maximum number of concurrent innerFlow.Publishers and their in-flight item count, optionally aggregating and delaying all errors until all sources terminate.- Type Parameters:
U- output item type- Parameters:
mapper-Functionreceiving item as parameter and returningFlow.PublishermaxConcurrency- the maximum number of inner sources to rundelayErrors- if true, any error from the main and inner sources are aggregated and delayed until all of them terminateprefetch- the number of items to request upfront from the inner sources, then request 75% more after 75% has been delivered- Returns:
- Multi
-
flatMapIterable
default <U> Multi<U> flatMapIterable(Function<? super T,? extends Iterable<? extends U>> iterableMapper)
Transform item with supplied function and flatten resultingIterableto downstream.
-
flatMapIterable
default <U> Multi<U> flatMapIterable(Function<? super T,? extends Iterable<? extends U>> iterableMapper, int prefetch)
Transform item with supplied function and flatten resultingIterableto downstream.
-
forEach
default void forEach(Consumer<T> consumer)
Terminal stage, invokes provided consumer for every item in the stream.- Parameters:
consumer- consumer to be invoked for each item
-
collect
default <U> Single<U> collect(Collector<T,U> collector)
- Type Parameters:
U- collector container type- Parameters:
collector- collector to use- Returns:
- Single
- Throws:
NullPointerException- if collector isnull
-
collect
default <U> Single<U> collect(Supplier<U> collectionSupplier, BiConsumer<U,T> accumulator)
Collect the items of thisMultiinto a collection provided via aSupplierand mutated by aBiConsumercallback.- Type Parameters:
U- the type of the collection and result- Parameters:
collectionSupplier- theSupplierthat is called for each incomingFlow.Subscriberto create a fresh collection to collect items intoaccumulator- theBiConsumerthat receives the collection and the current item to put in- Returns:
- Single
- Throws:
NullPointerException- ifcollectionSupplierorcombinerisnull
-
collectStream
default <A,R> Single<R> collectStream(Collector<T,A,R> collector)
Collects up upstream items with the help of a the callbacks of aCollector.- Type Parameters:
A- the accumulator typeR- the result type- Parameters:
collector- the collector whosesupplier(),accumulator()andfinisher()callbacks are used for collecting upstream items into a final form.- Returns:
- Single
- Throws:
NullPointerException- ifcollectorisnull
-
reduce
default Single<T> reduce(BiFunction<T,T,T> reducer)
Combine subsequent items via a callback function and emit the final value result as a Single.If the upstream is empty, the resulting Single is also empty. If the upstream contains only one item, the reducer function is not invoked and the resulting Single will have only that single item.
- Parameters:
reducer- the function called with the first value or the previous result, the current upstream value and should return a new value- Returns:
- Single
-
reduce
default <R> Single<R> reduce(Supplier<? extends R> supplier, BiFunction<R,T,R> reducer)
Combine every upstream item with an accumulator value to produce a new accumulator value and emit the final accumulator value as a Single.- Type Parameters:
R- the accumulator and result type- Parameters:
supplier- the function to return the initial accumulator value for each incoming Subscriberreducer- the function that receives the current accumulator value, the current upstream value and should return a new accumulator value- Returns:
- Single
-
from
static <T> Multi<T> from(Flow.Publisher<T> source)
Create aMultiinstance wrapped around the given publisher.- Type Parameters:
T- item type- Parameters:
source- source publisher- Returns:
- Multi
- Throws:
NullPointerException- if source isnull
-
from
static <T> Multi<T> from(Iterable<T> iterable)
Create aMultiinstance that publishes the given iterable.- Type Parameters:
T- item type- Parameters:
iterable- iterable to publish- Returns:
- Multi
- Throws:
NullPointerException- if iterable isnull
-
from
static <T> Multi<T> from(Stream<T> stream)
Create aMultiinstance that publishes the givenStream.Note that Streams can be only consumed once, therefore, the returned Multi will signal
IllegalStateExceptionif multiple subscribers try to consume it.The operator calls
BaseStream.close()when the stream finishes, fails or the flow gets canceled. To avoid closing the stream automatically, it is recommended to turn theStreaminto anIterableviaBaseStream.iterator()and usefrom(Iterable):Stream<T> stream = ... Multi<T> multi = Multi.from(stream::iterator);- Type Parameters:
T- item type- Parameters:
stream- the Stream to publish- Returns:
- Multi
- Throws:
NullPointerException- ifstreamisnull
-
just
static <T> Multi<T> just(Collection<T> items)
Create aMultiinstance that publishes the given items to a single subscriber.- Type Parameters:
T- item type- Parameters:
items- items to publish- Returns:
- Multi
- Throws:
NullPointerException- ifitemsisnull
-
just
@SafeVarargs static <T> Multi<T> just(T... items)
Create aMultiinstance that publishes the given items to a single subscriber.- Type Parameters:
T- item type- Parameters:
items- items to publish- Returns:
- Multi
- Throws:
NullPointerException- ifitemsisnull
-
singleton
static <T> Multi<T> singleton(T item)
Create aMultithat emits a pre-existing item and then completes.- Type Parameters:
T- the type of the item- Parameters:
item- the item to emit.- Returns:
- Multi
- Throws:
NullPointerException- ifitemisnull
-
error
static <T> Multi<T> error(Throwable error)
Create aMultiinstance that reports the given exception to its subscriber(s). The exception is reported by invokingFlow.Subscriber.onError(java.lang.Throwable)whenFlow.Publisher.subscribe(Subscriber)is called.- Type Parameters:
T- item type- Parameters:
error- exception to hold- Returns:
- Multi
- Throws:
NullPointerException- if error isnull
-
empty
static <T> Multi<T> empty()
Get aMultiinstance that completes immediately.- Type Parameters:
T- item type- Returns:
- Multi
-
never
static <T> Multi<T> never()
Get aMultiinstance that never completes.- Type Parameters:
T- item type- Returns:
- Multi
-
concat
static <T> Multi<T> concat(Multi<T> firstMulti, Multi<T> secondMulti)
Concat streams to one.- Type Parameters:
T- item type- Parameters:
firstMulti- first streamsecondMulti- second stream- Returns:
- Multi
-
onTerminate
default Multi<T> onTerminate(Runnable onTerminate)
Executes givenRunnablewhen any of signals onComplete, onCancel or onError is received.- Parameters:
onTerminate-Runnableto be executed.- Returns:
- Multi
-
onComplete
default Multi<T> onComplete(Runnable onTerminate)
Executes givenRunnablewhen onComplete signal is received.- Parameters:
onTerminate-Runnableto be executed.- Returns:
- Multi
-
onError
default Multi<T> onError(Consumer<Throwable> onErrorConsumer)
Executes givenRunnablewhen onError signal is received.- Parameters:
onErrorConsumer-Runnableto be executed.- Returns:
- Multi
-
takeUntil
default <U> Multi<T> takeUntil(Flow.Publisher<U> other)
Relay upstream items until the other source signals an item or completes.- Type Parameters:
U- the element type of the other sequence- Parameters:
other- the other sequence to signal the end of the main sequence- Returns:
- Multi
- Throws:
NullPointerException- ifotherisnull
-
range
static Multi<Integer> range(int start, int count)
Emits a range of ever increasing integers.- Parameters:
start- the initial integer valuecount- the number of integers to emit- Returns:
- Multi
- Throws:
IllegalArgumentException- ifcountis negative
-
rangeLong
static Multi<Long> rangeLong(long start, long count)
Emits a range of ever increasing longs.- Parameters:
start- the initial long valuecount- the number of longs to emit- Returns:
- Multi
- Throws:
IllegalArgumentException- ifcountis negative
-
timer
static Multi<Long> timer(long time, TimeUnit unit, ScheduledExecutorService executor)
Signal 0L and complete the sequence after the given time elapsed.- Parameters:
time- the time to wait before signaling 0L and completionunit- the unit of timeexecutor- the executor to run the waiting on- Returns:
- Multi
- Throws:
NullPointerException- ifunitorexecutorisnull
-
interval
static Multi<Long> interval(long period, TimeUnit unit, ScheduledExecutorService executor)
Signal 0L, 1L and so on periodically to the downstream.Note that if the downstream applies backpressure, subsequent values may be delivered instantly upon further requests from the downstream.
- Parameters:
period- the initial and in-between timeunit- the time unitexecutor- the scheduled executor to use for the periodic emission- Returns:
- Multi
- Throws:
NullPointerException- ifunitorexecutorisnull
-
interval
static Multi<Long> interval(long initialDelay, long period, TimeUnit unit, ScheduledExecutorService executor)
Signal 0L after an initial delay, then 1L, 2L and so on periodically to the downstream.Note that if the downstream applies backpressure, subsequent values may be delivered instantly upon further requests from the downstream.
- Parameters:
initialDelay- the time before signaling 0Lperiod- the in-between wait time for values 1L, 2L and so onunit- the time unitexecutor- the scheduled executor to use for the periodic emission- Returns:
- Multi
- Throws:
NullPointerException- ifunitorexecutorisnull
-
onErrorResume
default Multi<T> onErrorResume(Function<? super Throwable,? extends T> onError)
Functionproviding one item to be submitted as onNext in case of onError signal is received.- Parameters:
onError- Function receivingThrowableas argument and producing one item to resume stream with.- Returns:
- Multi
-
-