- 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.Object
Collects / 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 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()
<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<java.util.Set<T>>
asSet()
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 aUni
emitting an item with the object computed by the givenCollector
.
-
-
-
Method Detail
-
first
@CheckReturnValue public Uni<T> first()
Creates aUni
receiving the first item emitted by the upstreamMulti
. If theMulti
is empty, the producedUni
firesnull
as item when theMulti
emits the completion event. If theMulti
emits a failure before having emitted an item, the producedUni
propagates the failure.- Returns:
- the produced uni
-
last
@CheckReturnValue public Uni<T> last()
Creates aUni
receiving the last item emitted by the upstreamMulti
. The last item is item fired just before the completion event.If the
Multi
is empty, the producedUni
firesnull
as item when theMulti
emits the completion event. If theMulti
emits a failure, the producedUni
propagates 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 aUni
emitting 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 newUni
emitting a container with all items emitted by thisMulti
.It produces the container instance using the passed
Supplier
(at subscription time) and then call theaccumulator
bi-consumer for each item emitted by theMulti
.The collected item will be emitted when this
Multi
fires the completion event.If the
Multi
propagates a failure, the producesUni
propagates the same failure, even if some items have been collected. If theMulti
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 benull
. Must not producenull
accumulator
- 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
Uni
emitting the collected container as item when thisMulti
completes
-
asMap
@CheckReturnValue public <K> Uni<java.util.Map<K,T>> asMap(java.util.function.Function<? super T,? extends K> keyMapper)
Produces anUni
emitting aMap
ofkey -> item
for each item emitted by thisMulti
. The collected map is emitted by the producedUni
when theMulti
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 thisMulti
- Parameters:
keyMapper
- aFunction
to map item to a key for theMap
. Must not benull
, must not producenull
- Returns:
- a
Uni
emitting an item with the collectedMap
. The uni emits the item when thisMulti
completes
-
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 anUni
emitting aMap
ofkey -> mapped item
for each item emitted by thisMulti
. The collected map is emitted by the producedUni
when theMulti
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 thevalueMapper
function.- Type Parameters:
K
- the type of the key extracted from each item emitted by thisMulti
V
- the type of the value extracted from each item emitted by thisMulti
- Parameters:
keyMapper
- aFunction
to map item to a key for theMap
. Must not benull
, must not producenull
valueMapper
- aFunction
to map item to a value for theMap
. Must not benull
, must not producenull
- Returns:
- a
Uni
emitting an item with the collectedMap
. The uni emits the item when thisMulti
completes
-
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 anUni
emitting aMap
ofkey -> mapped item
for each item emitted by thisMulti
. The collected map is emitted by the producedUni
when theMulti
fires the completion event.The key is extracted from each item by applying the
keyMapper
function. In case of conflictmergeFunction
is used to choose which item should be emitted. The value is computed by applying thevalueMapper
function.- Type Parameters:
K
- the type of the key extracted from each item emitted by thisMulti
V
- the type of the value extracted from each item emitted by thisMulti
- Parameters:
keyMapper
- aFunction
to map item to a key for theMap
. Must not benull
, must not producenull
valueMapper
- aFunction
to map item to a value for theMap
. Must not benull
, must not producenull
mergeFunction
- aBinaryOperator
used 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
Uni
emitting an item with the collectedMap
. The uni emits the item when thisMulti
completes
-
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 anUni
emitting aMap
ofkey -> Collection of mapped values
for each item emitted by thisMulti
. The collected map is emitted by the producedUni
when theMulti
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 thevalueMapper
function.- Type Parameters:
K
- the type of the key extracted from each item emitted by thisMulti
V
- the type of the value extracted from each item emitted by thisMulti
- Parameters:
keyMapper
- aFunction
to map item to a key for theMap
. Must not benull
, must not producenull
valueMapper
- aFunction
to map item to a value for theMap
. Must not benull
, must not producenull
- Returns:
- a
Uni
emitting an item with the collectedMap
. The uni emits the item when thisMulti
completes
-
asMultiMap
@CheckReturnValue public <K> Uni<java.util.Map<K,java.util.Collection<T>>> asMultiMap(java.util.function.Function<? super T,? extends K> keyMapper)
Produces anUni
emitting aMap
ofkey -> Collection of items
for each item emitted by thisMulti
. The collected map is emitted by the producedUni
when theMulti
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 thisMulti
- Parameters:
keyMapper
- aFunction
to map item to a key for theMap
.Must not benull
, must not producenull
- Returns:
- a
Uni
emitting an item with the collectedMap
. The uni emits the item when thisMulti
completes
-
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 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 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 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 benull
.- Returns:
- the object to configure the item collection.
-
-