Interface Multi<T>

    • 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 - if supplier is null
      • map

        default <U> Multi<U> map​(Mapper<T,​U> mapper)
        Map this Multi instance to a new Multi of another type using the given Mapper.
        Type Parameters:
        U - mapped item type
        Parameters:
        mapper - mapper
        Returns:
        Multi
        Throws:
        NullPointerException - if mapper is null
      • 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 - if defaultItem is null
      • 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 - if other is null
      • peek

        default Multi<T> peek​(Consumer<T> consumer)
        Invoke provided consumer for every item in stream.
        Parameters:
        consumer - consumer to be invoked
        Returns:
        Multi
      • distinct

        default Multi<T> distinct()
        Filter out all duplicates.
        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 resulting Flow.Publisher to downstream.
        Type Parameters:
        U - output item type
        Parameters:
        publisherMapper - Function receiving item as parameter and returning Flow.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 resulting Flow.Publisher to downstream while limiting the maximum number of concurrent inner Flow.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 - Function receiving item as parameter and returning Flow.Publisher
        maxConcurrency - the maximum number of inner sources to run
        delayErrors - if true, any error from the main and inner sources are aggregated and delayed until all of them terminate
        prefetch - 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 resulting Iterable to downstream.
        Type Parameters:
        U - output item type
        Parameters:
        iterableMapper - Function receiving item as parameter and returning Iterable
        Returns:
        Multi
      • flatMapIterable

        default <U> Multi<U> flatMapIterable​(Function<? super T,​? extends Iterable<? extends U>> iterableMapper,
                                             int prefetch)
        Transform item with supplied function and flatten resulting Iterable to downstream.
        Type Parameters:
        U - output item type
        Parameters:
        iterableMapper - Function receiving item as parameter and returning Iterable
        prefetch - the number of upstream items to request upfront, then 75% of this value after 75% received and mapped
        Returns:
        Multi
      • 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
      • collectList

        default Single<List<T>> collectList()
        Collect the items of this Multi instance into a Single of List.
        Returns:
        Single
      • collect

        default <U> Single<U> collect​(Collector<T,​U> collector)
        Collect the items of this Multi instance into a Single.
        Type Parameters:
        U - collector container type
        Parameters:
        collector - collector to use
        Returns:
        Single
        Throws:
        NullPointerException - if collector is null
      • collect

        default <U> Single<U> collect​(Supplier<U> collectionSupplier,
                                      BiConsumer<U,​T> accumulator)
        Collect the items of this Multi into a collection provided via a Supplier and mutated by a BiConsumer callback.
        Type Parameters:
        U - the type of the collection and result
        Parameters:
        collectionSupplier - the Supplier that is called for each incoming Flow.Subscriber to create a fresh collection to collect items into
        accumulator - the BiConsumer that receives the collection and the current item to put in
        Returns:
        Single
        Throws:
        NullPointerException - if collectionSupplier or combiner is null
      • collectStream

        default <A,​R> Single<R> collectStream​(Collector<T,​A,​R> collector)
        Collects up upstream items with the help of a the callbacks of a Collector.
        Type Parameters:
        A - the accumulator type
        R - the result type
        Parameters:
        collector - the collector whose supplier(), accumulator() and finisher() callbacks are used for collecting upstream items into a final form.
        Returns:
        Single
        Throws:
        NullPointerException - if collector is null
      • 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 Subscriber
        reducer - the function that receives the current accumulator value, the current upstream value and should return a new accumulator value
        Returns:
        Single
      • first

        default Single<T> first()
        Get the first item of this Multi instance as a Single.
        Returns:
        Single
      • from

        static <T> Multi<T> from​(Flow.Publisher<T> source)
        Create a Multi instance wrapped around the given publisher.
        Type Parameters:
        T - item type
        Parameters:
        source - source publisher
        Returns:
        Multi
        Throws:
        NullPointerException - if source is null
      • from

        static <T> Multi<T> from​(Iterable<T> iterable)
        Create a Multi instance that publishes the given iterable.
        Type Parameters:
        T - item type
        Parameters:
        iterable - iterable to publish
        Returns:
        Multi
        Throws:
        NullPointerException - if iterable is null
      • from

        static <T> Multi<T> from​(Stream<T> stream)
        Create a Multi instance that publishes the given Stream.

        Note that Streams can be only consumed once, therefore, the returned Multi will signal IllegalStateException if 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 the Stream into an Iterable via BaseStream.iterator() and use from(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 - if stream is null
      • just

        static <T> Multi<T> just​(Collection<T> items)
        Create a Multi instance that publishes the given items to a single subscriber.
        Type Parameters:
        T - item type
        Parameters:
        items - items to publish
        Returns:
        Multi
        Throws:
        NullPointerException - if items is null
      • just

        @SafeVarargs
        static <T> Multi<T> just​(T... items)
        Create a Multi instance that publishes the given items to a single subscriber.
        Type Parameters:
        T - item type
        Parameters:
        items - items to publish
        Returns:
        Multi
        Throws:
        NullPointerException - if items is null
      • singleton

        static <T> Multi<T> singleton​(T item)
        Create a Multi that 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 - if item is null
      • empty

        static <T> Multi<T> empty()
        Get a Multi instance that completes immediately.
        Type Parameters:
        T - item type
        Returns:
        Multi
      • never

        static <T> Multi<T> never()
        Get a Multi instance 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 stream
        secondMulti - second stream
        Returns:
        Multi
      • onTerminate

        default Multi<T> onTerminate​(Runnable onTerminate)
        Executes given Runnable when any of signals onComplete, onCancel or onError is received.
        Parameters:
        onTerminate - Runnable to be executed.
        Returns:
        Multi
      • onComplete

        default Multi<T> onComplete​(Runnable onTerminate)
        Executes given Runnable when onComplete signal is received.
        Parameters:
        onTerminate - Runnable to be executed.
        Returns:
        Multi
      • onError

        default Multi<T> onError​(Consumer<Throwable> onErrorConsumer)
        Executes given Runnable when onError signal is received.
        Parameters:
        onErrorConsumer - Runnable to 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 - if other is null
      • range

        static Multi<Integer> range​(int start,
                                    int count)
        Emits a range of ever increasing integers.
        Parameters:
        start - the initial integer value
        count - the number of integers to emit
        Returns:
        Multi
        Throws:
        IllegalArgumentException - if count is negative
      • rangeLong

        static Multi<Long> rangeLong​(long start,
                                     long count)
        Emits a range of ever increasing longs.
        Parameters:
        start - the initial long value
        count - the number of longs to emit
        Returns:
        Multi
        Throws:
        IllegalArgumentException - if count is 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 completion
        unit - the unit of time
        executor - the executor to run the waiting on
        Returns:
        Multi
        Throws:
        NullPointerException - if unit or executor is null
      • 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 time
        unit - the time unit
        executor - the scheduled executor to use for the periodic emission
        Returns:
        Multi
        Throws:
        NullPointerException - if unit or executor is null
      • 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 0L
        period - the in-between wait time for values 1L, 2L and so on
        unit - the time unit
        executor - the scheduled executor to use for the periodic emission
        Returns:
        Multi
        Throws:
        NullPointerException - if unit or executor is null
      • onErrorResume

        default Multi<T> onErrorResume​(Function<? super Throwable,​? extends T> onError)
        Function providing one item to be submitted as onNext in case of onError signal is received.
        Parameters:
        onError - Function receiving Throwable as argument and producing one item to resume stream with.
        Returns:
        Multi
      • onErrorResumeWith

        default Multi<T> onErrorResumeWith​(Function<? super Throwable,​? extends Flow.Publisher<? extends T>> onError)
        Resume stream from supplied publisher if onError signal is intercepted.
        Parameters:
        onError - supplier of new stream publisher
        Returns:
        Multi