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 java.lang.ObjectCollects / aggregates items from the upstream and send the resulting collection / structure when the upstream completes. The resulting structure is emitted through aUni. IMPORTANT: Do not use on unbounded streams, as it would lead toOutOfMemoryError.
-
-
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()<K> Uni<java.util.Map<K,T>>asMap(java.util.function.Function<? super T,? extends K> keyMapper)<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)<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, java.util.function.BinaryOperator<V> mergeFunction)<K> Uni<java.util.Map<K,java.util.Collection<T>>>asMultiMap(java.util.function.Function<? super T,? extends K> keyMapper)<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)Uni<T>first()<X> Uni<X>in(java.util.function.Supplier<X> supplier, java.util.function.BiConsumer<X,T> accumulator)Uni<T>last()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 aUniemitting an item with the object computed by the givenCollector.
-
-
-
Method Detail
-
first
@CheckReturnValue public Uni<T> first()
Creates aUnireceiving the first item emitted by the upstreamMulti. If theMultiis empty, the producedUnifiresnullas item when theMultiemits the completion event. If theMultiemits a failure before having emitted an item, the producedUnipropagates the failure.- Returns:
- the produced uni
-
last
@CheckReturnValue public Uni<T> last()
Creates aUnireceiving the last item emitted by the upstreamMulti. The last item is item fired just before the completion event.If the
Multiis empty, the producedUnifiresnullas item when theMultiemits the completion event. If theMultiemits a failure, the producedUnipropagates the failure.- Returns:
- the produced uni
-
with
@CheckReturnValue public <X,A> Uni<X> with(java.util.stream.Collector<? super T,A,? extends X> collector)
Creates aUniemitting an item with the object computed by the givenCollector. The collector behaves the same way as on a Java stream.
-
in
@CheckReturnValue public <X> Uni<X> in(java.util.function.Supplier<X> supplier, java.util.function.BiConsumer<X,T> accumulator)
Produces a newUniemitting a container with all items emitted by thisMulti.It produces the container instance using the passed
Supplier(at subscription time) and then call theaccumulatorbi-consumer for each item emitted by theMulti.The collected item will be emitted when this
Multifires the completion event.If the
Multipropagates a failure, the producesUnipropagates the same failure, even if some items have been collected. If theMultiis 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 benull. Must not producenullaccumulator- a consumer called on every item with the container instance and the item. It should add the item into the container. Must not benull- Returns:
- a
Uniemitting the collected container as item when thisMulticompletes
-
asMap
@CheckReturnValue public <K> Uni<java.util.Map<K,T>> asMap(java.util.function.Function<? super T,? extends K> keyMapper)
Produces anUniemitting aMapofkey -> itemfor each item emitted by thisMulti. The collected map is emitted by the producedUniwhen theMultifires the completion event.The key is extracted from each item by applying the
keyMapperfunction. 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 thisMulti- Parameters:
keyMapper- aFunctionto map item to a key for theMap. Must not benull, must not producenull- Returns:
- a
Uniemitting an item with the collectedMap. The uni emits the item when thisMulticompletes
-
asMap
@CheckReturnValue 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 anUniemitting aMapofkey -> mapped itemfor each item emitted by thisMulti. The collected map is emitted by the producedUniwhen theMultifires the completion event.The key is extracted from each item by applying the
keyMapperfunction. In case of conflict, the associated value will be the most recently emitted item. The value is computed by applying thevalueMapperfunction.- Type Parameters:
K- the type of the key extracted from each item emitted by thisMultiV- the type of the value extracted from each item emitted by thisMulti- Parameters:
keyMapper- aFunctionto map item to a key for theMap. Must not benull, must not producenullvalueMapper- aFunctionto map item to a value for theMap. Must not benull, must not producenull- Returns:
- a
Uniemitting an item with the collectedMap. The uni emits the item when thisMulticompletes
-
asMap
@CheckReturnValue 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, java.util.function.BinaryOperator<V> mergeFunction)
Produces anUniemitting aMapofkey -> mapped itemfor each item emitted by thisMulti. The collected map is emitted by the producedUniwhen theMultifires the completion event.The key is extracted from each item by applying the
keyMapperfunction. In case of conflictmergeFunctionis used to choose which item should be emitted. The value is computed by applying thevalueMapperfunction.- Type Parameters:
K- the type of the key extracted from each item emitted by thisMultiV- the type of the value extracted from each item emitted by thisMulti- Parameters:
keyMapper- aFunctionto map item to a key for theMap. Must not benull, must not producenullvalueMapper- aFunctionto map item to a value for theMap. Must not benull, must not producenullmergeFunction- aBinaryOperatorused to resolve collisions between values associated with the same key. Must not benull. In case it returns null the owner key will be removed from theMap- Returns:
- a
Uniemitting an item with the collectedMap. The uni emits the item when thisMulticompletes
-
asMultiMap
@CheckReturnValue 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 anUniemitting aMapofkey -> Collection of mapped valuesfor each item emitted by thisMulti. The collected map is emitted by the producedUniwhen theMultifires the completion event.The key is extracted from each item by applying the
keyMapperfunction. The value is a collection containing all the values mapped to the specific key. The value is computed by applying thevalueMapperfunction.- Type Parameters:
K- the type of the key extracted from each item emitted by thisMultiV- the type of the value extracted from each item emitted by thisMulti- Parameters:
keyMapper- aFunctionto map item to a key for theMap. Must not benull, must not producenullvalueMapper- aFunctionto map item to a value for theMap. Must not benull, must not producenull- Returns:
- a
Uniemitting an item with the collectedMap. The uni emits the item when thisMulticompletes
-
asMultiMap
@CheckReturnValue public <K> Uni<java.util.Map<K,java.util.Collection<T>>> asMultiMap(java.util.function.Function<? super T,? extends K> keyMapper)
Produces anUniemitting aMapofkey -> Collection of itemsfor each item emitted by thisMulti. The collected map is emitted by the producedUniwhen theMultifires the completion event.The key is extracted from each item by applying the
keyMapperfunction. 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 thisMulti- Parameters:
keyMapper- aFunctionto map item to a key for theMap.Must not benull, must not producenull- Returns:
- a
Uniemitting an item with the collectedMap. The uni emits the item when thisMulticompletes
-
where
@CheckReturnValue 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 toupstream.select().when(predicate).collect(). For each item, it calls the predicate. If the predicate returnstrue, 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 benull.- Returns:
- the object to configure the item collection.
-
when
@CheckReturnValue 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. Unlikewhere(Predicate), the predicate returns aUni<Boolean>, which support asynchronous tests. This method is equivalent toupstream.select().where(predicate).collect(). For each item, it calls the predicate. If the predicate emits the itemtrue, 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 benull.- Returns:
- the object to configure the item collection.
-
-