Class MultiSelect<T>

  • Type Parameters:
    T - the type of item

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

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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Multi<T> distinct()
      Selects all the distinct items from the upstream.
      Multi<T> distinct​(java.util.Comparator<? super T> comparator)
      Selects all the distinct items from the upstream.
      Multi<T> first()
      Select the first item from the Multi.
      Multi<T> first​(long n)
      Selects the first n items from the Multi.
      Multi<T> first​(java.time.Duration duration)
      Selects the first items for the given duration.
      Multi<T> first​(java.util.function.Predicate<? super T> predicate)
      Selects the first items while the given predicate returns true.
      Multi<T> last()
      Select the last item from the Multi.
      Multi<T> last​(int n)
      Selects the last n items from the Multi.
      Multi<T> when​(java.util.function.Function<? super T,​Uni<java.lang.Boolean>> predicate)
      Selects the items where the given function produced a Uni emitting true.
      Multi<T> where​(java.util.function.Predicate<? super T> predicate)
      Selects the items where the given predicate returns true.
      Multi<T> where​(java.util.function.Predicate<? super T> predicate, int limit)
      Like when(Function), but select at most limit items.
      • Methods inherited from class java.lang.Object

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

      • MultiSelect

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

      • first

        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

        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

        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

        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

        public Multi<T> first​(java.util.function.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

        public Multi<T> first​(java.time.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

        public Multi<T> where​(java.util.function.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:
        when(Function)
      • where

        public Multi<T> where​(java.util.function.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(Function)
      • when

        public Multi<T> when​(java.util.function.Function<? super T,​Uni<java.lang.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

        public Multi<T> distinct()
        Selects all the distinct items from the upstream. This methods 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:
        MultiSkip.repetitions(), distinct(Comparator)
      • distinct

        public Multi<T> distinct​(java.util.Comparator<? super T> comparator)
        Selects all the distinct items from the upstream. This methods 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 uses the item's hashCode method.
        Returns:
        the resulting Multi.
        See Also:
        MultiSkip.repetitions()