Class MultiCollect<T>

  • Type Parameters:
    T - the type of item sent by the upstream.

    public class MultiCollect<T>
    extends java.lang.Object
    Collects / aggregates items from the upstream and send the resulting collection / structure when the upstream completes. The resulting structure is emitted through a Uni. IMPORTANT: Do not use on unbounded streams, as it would lead to OutOfMemoryError.
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiCollect​(Multi<T> upstream)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Uni<java.util.List<T>> asList()
      Creates a Uni emitting an item containing all elements emitted by this Multi into a List.
      <K> Uni<java.util.Map<K,​T>> asMap​(java.util.function.Function<? super T,​? extends K> keyMapper)
      Produces an Uni emitting a Map of key -> item for each item emitted by this Multi.
      <K,​V>
      Uni<java.util.Map<K,​V>>
      asMap​(java.util.function.Function<? super T,​? extends K> keyMapper, java.util.function.Function<? super T,​? extends V> valueMapper)
      Produces an Uni emitting a Map of key -> mapped item for each item emitted by this Multi.
      <K> Uni<java.util.Map<K,​java.util.Collection<T>>> asMultiMap​(java.util.function.Function<? super T,​? extends K> keyMapper)
      Produces an Uni emitting a Map of key -> Collection of items for each item emitted by this Multi.
      <K,​V>
      Uni<java.util.Map<K,​java.util.Collection<V>>>
      asMultiMap​(java.util.function.Function<? super T,​? extends K> keyMapper, java.util.function.Function<? super T,​? extends V> valueMapper)
      Produces an Uni emitting a Map of key -> Collection of mapped values for each item emitted by this Multi.
      Uni<T> first()
      Creates a Uni receiving the first item emitted by the upstream Multi.
      <X> Uni<X> in​(java.util.function.Supplier<X> supplier, java.util.function.BiConsumer<X,​T> accumulator)
      Produces a new Uni emitting a container with all items emitted by this Multi.
      Uni<T> last()
      Creates a Uni receiving the last item emitted by the upstream Multi.
      MultiCollect<T> when​(java.util.function.Function<? super T,​Uni<java.lang.Boolean>> predicate)
      Collects only the items from the upstream that passes the given predicate.
      MultiCollect<T> where​(java.util.function.Predicate<T> predicate)
      Collects only the items from the upstream that passes the given predicate.
      <X,​A>
      Uni<X>
      with​(java.util.stream.Collector<? super T,​A,​? extends X> collector)
      Creates a Uni emitting an item with the object computed by the given Collector.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultiCollect

        public MultiCollect​(Multi<T> upstream)
    • Method Detail

      • first

        public Uni<T> first()
        Creates a Uni receiving the first item emitted by the upstream Multi. If the Multi is empty, the produced Uni fires null as item when the Multi emits the completion event. If the Multi emits a failure before having emitted an item, the produced Uni propagates the failure.
        Returns:
        the produced uni
      • last

        public Uni<T> last()
        Creates a Uni receiving the last item emitted by the upstream Multi. The last item is item fired just before the completion event.

        If the Multi is empty, the produced Uni fires null as item when the Multi emits the completion event. If the Multi emits a failure, the produced Uni propagates the failure.

        Returns:
        the produced uni
      • asList

        public Uni<java.util.List<T>> asList()
        Creates a Uni emitting an item containing all elements emitted by this Multi into a List. The produced Uni emits its item when this Multi completes.
        Returns:
        the Uni emitting the list of items from this Multi.
      • with

        public <X,​A> Uni<X> with​(java.util.stream.Collector<? super T,​A,​? extends X> collector)
        Creates a Uni emitting an item with the object computed by the given Collector. The collector behaves the same way as on a Java stream.
        Type Parameters:
        A - the accumulation type
        X - the item type
        Parameters:
        collector - the Collector, must not be null
        Returns:
        a Uni emitted the collected object as item, when the Multi completes
      • in

        public <X> Uni<X> in​(java.util.function.Supplier<X> supplier,
                             java.util.function.BiConsumer<X,​T> accumulator)
        Produces a new Uni emitting a container with all items emitted by this Multi.

        It produces the container instance using the passed Supplier (at subscription time) and then call the accumulator bi-consumer for each item emitted by the Multi.

        The collected item will be emitted when this Multi fires the completion event.

        If the Multi propagates a failure, the produces Uni propagates the same failure, even if some items have been collected. If the Multi is empty, the supplied container is returned empty

        Type Parameters:
        X - the type of the container produced by the supplier.
        Parameters:
        supplier - the supplier of the container instance, called at Subscription time. Must not be null. Must not produce null
        accumulator - a consumer called on every item with the container instance and the item. It should add the item into the container. Must not be null
        Returns:
        a Uni emitting the collected container as item when this Multi completes
      • asMap

        public <K> Uni<java.util.Map<K,​T>> asMap​(java.util.function.Function<? super T,​? extends K> keyMapper)
        Produces an Uni emitting a Map of key -> item for each item emitted by this Multi. The collected map is emitted by the produced Uni when the Multi fires the completion event.

        The key is extracted from each item by applying the keyMapper function. In case of conflict, the associated value will be the most recently emitted item.

        Type Parameters:
        K - the type of the key extracted from each item emitted by this Multi
        Parameters:
        keyMapper - a Function to map item to a key for the Map. Must not be null, must not produce null
        Returns:
        a Uni emitting an item with the collected Map. The uni emits the item when this Multi completes
      • asMap

        public <K,​V> Uni<java.util.Map<K,​V>> asMap​(java.util.function.Function<? super T,​? extends K> keyMapper,
                                                               java.util.function.Function<? super T,​? extends V> valueMapper)
        Produces an Uni emitting a Map of key -> mapped item for each item emitted by this Multi. The collected map is emitted by the produced Uni when the Multi fires the completion event.

        The key is extracted from each item by applying the keyMapper function. In case of conflict, the associated value will be the most recently emitted item. The value is computed by applying the valueMapper function.

        Type Parameters:
        K - the type of the key extracted from each item emitted by this Multi
        V - the type of the value extracted from each item emitted by this Multi
        Parameters:
        keyMapper - a Function to map item to a key for the Map. Must not be null, must not produce null
        valueMapper - a Function to map item to a value for the Map. Must not be null, must not produce null
        Returns:
        a Uni emitting an item with the collected Map. The uni emits the item when this Multi completes
      • asMultiMap

        public <K,​V> Uni<java.util.Map<K,​java.util.Collection<V>>> asMultiMap​(java.util.function.Function<? super T,​? extends K> keyMapper,
                                                                                          java.util.function.Function<? super T,​? extends V> valueMapper)
        Produces an Uni emitting a Map of key -> Collection of mapped values for each item emitted by this Multi. The collected map is emitted by the produced Uni when the Multi fires the completion event.

        The key is extracted from each item by applying the keyMapper function. The value is a collection containing all the values mapped to the specific key. The value is computed by applying the valueMapper function.

        Type Parameters:
        K - the type of the key extracted from each item emitted by this Multi
        V - the type of the value extracted from each item emitted by this Multi
        Parameters:
        keyMapper - a Function to map item to a key for the Map. Must not be null, must not produce null
        valueMapper - a Function to map item to a value for the Map. Must not be null, must not produce null
        Returns:
        a Uni emitting an item with the collected Map. The uni emits the item when this Multi completes
      • asMultiMap

        public <K> Uni<java.util.Map<K,​java.util.Collection<T>>> asMultiMap​(java.util.function.Function<? super T,​? extends K> keyMapper)
        Produces an Uni emitting a Map of key -> Collection of items for each item emitted by this Multi. The collected map is emitted by the produced Uni when the Multi fires the completion event.

        The key is extracted from each item by applying the keyMapper function. The value is a collection containing all the items emitted associated to the specific key.

        Type Parameters:
        K - the type of the key extracted from each item emitted by this Multi
        Parameters:
        keyMapper - a Function to map item to a key for the Map.Must not be null, must not produce null
        Returns:
        a Uni emitting an item with the collected Map. The uni emits the item when this Multi completes
      • where

        public MultiCollect<T> where​(java.util.function.Predicate<T> predicate)
        Collects only the items from the upstream that passes the given predicate. This method is equivalent to upstream.select().when(predicate).collect(). For each item, it calls the predicate. If the predicate returns true, it collects the item, otherwise it discards the item. If the predicate throws an exception, it propagates that exception as failure.
        Parameters:
        predicate - the predicate, must not be null.
        Returns:
        the object to configure the item collection.
      • when

        public MultiCollect<T> when​(java.util.function.Function<? super T,​Uni<java.lang.Boolean>> predicate)
        Collects only the items from the upstream that passes the given predicate. Unlike where(Predicate), the predicate returns a Uni<Boolean>, which support asynchronous tests. This method is equivalent to upstream.select().where(predicate).collect(). For each item, it calls the predicate. If the predicate emits the item true, it collects the item, otherwise it discards the item. If the predicate throws an exception or emits a failure, it propagates that exception as failure.
        Parameters:
        predicate - the predicate, must not be null.
        Returns:
        the object to configure the item collection.