Class MultiCollect<T>

java.lang.Object
io.smallrye.mutiny.groups.MultiCollect<T>
Type Parameters:
T - the type of item sent by the upstream.

public class MultiCollect<T> extends 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 Details

    • MultiCollect

      public MultiCollect(Multi<T> upstream)
  • Method Details

    • first

      @CheckReturnValue 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

      @CheckReturnValue 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

      @CheckReturnValue public Uni<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.
    • asSet

      @CheckReturnValue public Uni<Set<T>> asSet()
      Creates a Uni emitting an item containing all elements emitted by this Multi into a Set. The produced Uni emits its item when this Multi completes.
      Returns:
      the Uni emitting the set of items from this Multi.
    • with

      @CheckReturnValue public <X, A> Uni<X> with(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:
      X - the item type
      A - the accumulation type
      Parameters:
      collector - the Collector, must not be null
      Returns:
      a Uni emitted the collected object as item, when the Multi completes
    • in

      @CheckReturnValue public <X> Uni<X> in(Supplier<X> supplier, 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

      @CheckReturnValue public <K> Uni<Map<K,T>> asMap(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

      @CheckReturnValue public <K, V> Uni<Map<K,V>> asMap(Function<? super T,? extends K> keyMapper, 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
    • asMap

      @CheckReturnValue public <K, V> Uni<Map<K,V>> asMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valueMapper, BinaryOperator<V> mergeFunction)
      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 mergeFunction is used to choose which item should be emitted. 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
      mergeFunction - a BinaryOperator used to resolve collisions between values associated with the same key. Must not be null. In case it returns null the owner key will be removed from the Map
      Returns:
      a Uni emitting an item with the collected Map. The uni emits the item when this Multi completes
    • asMultiMap

      @CheckReturnValue public <K, V> Uni<Map<K,Collection<V>>> asMultiMap(Function<? super T,? extends K> keyMapper, 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

      @CheckReturnValue public <K> Uni<Map<K,Collection<T>>> asMultiMap(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

      @CheckReturnValue public MultiCollect<T> where(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

      @CheckReturnValue public MultiCollect<T> when(Function<? super T,Uni<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.