Class MultiSelect<T>

java.lang.Object
io.smallrye.mutiny.groups.MultiSelect<T>
Type Parameters:
T - the type of item

public class MultiSelect<T> extends Object
Selects items from the upstream Multi.
See Also:
  • Constructor Details

    • MultiSelect

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

    • first

      @CheckReturnValue public Multi<T> first()
      Select the first item from the Multi.

      If the upstream Multi contains more than one item, the others are dropped. If the upstream emits a failure before emitting an item, the produced Multi emits the same failure. If the upstream completes without emitting an item first, the produced Multi is empty.

      Returns:
      the resulting Multi
    • last

      @CheckReturnValue public Multi<T> last()
      Select the last item from the Multi.

      If the upstream Multi contains more than one item, the others are dropped, only the last one is emitted by the produced Multi. If the upstream emits a failure, the produced Multi emits the same failure. If the upstream completes without emitting an item first, the produced Multi is empty.

      Returns:
      the resulting Multi
    • first

      @CheckReturnValue public Multi<T> first(long n)
      Selects the first n items from the Multi.

      If the upstream Multi contains more than n items, the others are dropped. If the upstream Multi emits less than n items, all the items are emitted by the produced Multi. If the upstream emits a failure before emitting n items, the produced Multi emits the same failure after having emitted the first items. If the upstream completes without emitting an item first, the produced Multi is empty.

      Parameters:
      n - the number of items to select, must be positive. If 0, the resulting Multi is empty.
      Returns:
      the resulting Multi
    • last

      @CheckReturnValue public Multi<T> last(int n)
      Selects the last n items from the Multi.

      If the upstream Multi contains more than n items, the others are dropped. If the upstream Multi emits less than n items, all the items are emitted by the produced Multi. If the upstream emits a failure, the produced Multi emits the same failure after. No items will be emitted by the produced Multi. If the upstream completes without emitting an item first, the produced Multi is empty.

      Parameters:
      n - the number of items to select, must be positive. If 0, the resulting Multi is empty.
      Returns:
      the resulting Multi
    • first

      @CheckReturnValue public Multi<T> first(Predicate<? super T> predicate)
      Selects the first items while the given predicate returns true. It calls the predicates for each items, until the predicate returns false. Each item for which the predicates returned true is emitted by the produced Multi. As soon as the predicate returns false for an item, it stops emitting the item and sends the completion event. The last checked item is not emitted.

      If the upstream Multi is empty, the produced Multi is empty. If the upstream Multi is emitting a failure, while the predicate has not returned false yet, the failure is emitted by the produced Multi. If the predicates throws an exception while testing an item, the produced Multi emits that exception as failure. No more items will be tested or emitted. If the predicates returns true for each items from upstream, all the items are selected. Once the predicate returns false, it cancels the subscription to the upstream, and completes the produced Multi.

      Parameters:
      predicate - the predicate to test the items, must not be null
      Returns:
      the resulting Multi
    • first

      @CheckReturnValue public Multi<T> first(Duration duration)
      Selects the first items for the given duration. It selects each items emitted after the subscription for the given duration.

      If the upstream Multi is empty, the produced Multi is empty. If the upstream Multi is emitting a failure, before the duration expires, the failure is emitted by the produced Multi. If the upstream completes before the given duration, all the items are selected.

      Once the duration expires, it cancels the subscription to the upstream, and completes the produced Multi.

      Parameters:
      duration - the duration, must not be null, must be strictly positive.
      Returns:
      the resulting Multi
    • where

      @CheckReturnValue public Multi<T> where(Predicate<? super T> predicate)
      Selects the items where the given predicate returns true. It calls the predicates for each items. Each item for which the predicates returned true is emitted by the produced Multi. Others are dropped.

      If the upstream Multi is empty, the produced Multi is empty. If the upstream Multi is emitting a failure, the failure is emitted by the produced Multi. If the predicates throws an exception while testing an item, the produced Multi emits that exception as failure. No more items will be tested or emitted. If the predicates returns true for each items from upstream, all the items are selected. The produced Multi completes when the upstream completes.

      Parameters:
      predicate - the predicate to test the items, must not be null
      Returns:
      the resulting Multi
      See Also:
    • where

      @CheckReturnValue public Multi<T> where(Predicate<? super T> predicate, int limit)
      Like when(Function), but select at most limit items.
      Parameters:
      predicate - the predicate to test the items, must not be null
      limit - the maximum number of item to select, must be positive. 0 would produce an empty Multi
      Returns:
      the resulting Multi
      See Also:
    • when

      @CheckReturnValue public Multi<T> when(Function<? super T,Uni<Boolean>> predicate)
      Selects the items where the given function produced a Uni emitting true. This method is the asynchronous version of where(Predicate). Instead of a synchronous predicate, it accepts a function producing Uni. It calls the function for every item, and depending of the produced Uni, it emits the item downstream or drops it. If the returned Uni produces true, the item is selected and emitted by the produced Multi, otherwise the item is dropped. The item is only emitted when Uni produced for that item emits true.

      If the upstream Multi is empty, the produced Multi is empty. If the upstream Multi is emitting a failure, the failure is emitted by the produced Multi. If the function throws an exception while testing an item, the produced Multi emits that exception as failure. No more items will be tested or emitted. If the function produced a null Uni, the produced Multi emits an NullPointerException as failure. No more items will be tested or emitted. If the function produced a failing Uni, the produced Multi emits that failure. No more items will be tested or emitted. If the function produced a Uni emitting null, the produced Multi emits a failure. No more items will be tested or emitted. If the function accepts all the items from the upstream, all the items are selected. The produced Multi completes when the upstream completes.

      This method preserves the item orders.

      Parameters:
      predicate - the function to test the items, must not be null, must not produced null
      Returns:
      the resulting Multi
    • distinct

      @CheckReturnValue public Multi<T> distinct()
      Selects all the distinct items from the upstream. This method uses Object.hashCode() to compare items.

      Do NOT call this method on unbounded upstream, as it would lead to an OutOfMemoryError.

      If the comparison throws an exception, the produced Multi fails. The produced Multi completes when the upstream sends the completion event.

      Returns:
      the resulting Multi.
      See Also:
    • distinct

      @CheckReturnValue public Multi<T> distinct(Comparator<? super T> comparator)
      Selects all the distinct items from the upstream. This method uses the given comparator to compare the items.

      Do NOT call this method on unbounded upstream, as it would lead to an OutOfMemoryError.

      If the comparison throws an exception, the produced Multi fails. The produced Multi completes when the upstream sends the completion event.

      Unlike distinct() which uses a HashSet internally, this variant uses a TreeSet initialized with the given comparator. If the comparator is null, it uses a HashSet as backend.

      Parameters:
      comparator - the comparator used to compare items. If null, it will use the item's hashCode method.
      Returns:
      the resulting Multi.
      See Also:
    • distinct

      @CheckReturnValue public <K> Multi<T> distinct(Function<T,K> keyExtractor)
      Selects all the distinct items from the upstream. This method uses the given key extractor to extract an object from each item which is then used to compare the items. This method allows for a smaller memory footprint than distinct() and distinct(Comparator) as only the extracted keys are held in memory rather than the items themselves.

      Do NOT call this method on unbounded upstream, as it would lead to an OutOfMemoryError.

      If the comparison throws an exception, the produced Multi fails. The produced Multi completes when the upstream sends the completion event.

      Parameters:
      keyExtractor - the function used to extract keys from items, must not be null, must not produce null.
      Returns:
      the resulting Multi.
      See Also: