Class UniCreate
- java.lang.Object
-
- io.smallrye.mutiny.groups.UniCreate
-
public class UniCreate extends java.lang.Object
Group methods allowing to createUni
instances from various sources.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Uni<T>
completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
<T> Uni<T>
completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)
<T,S>
Uni<T>completionStage(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,? extends java.util.concurrent.CompletionStage<? extends T>> mapper)
<T> Uni<T>
context(java.util.function.Function<Context,Uni<? extends T>> mapper)
<I,T>
Uni<T>converter(io.smallrye.mutiny.converters.UniConverter<I,T> converter, I instance)
Creates a newUni
from the passed instance with the passed converter.<T> Uni<T>
deferred(java.util.function.Supplier<Uni<? extends T>> supplier)
<T,S>
Uni<T>deferred(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,Uni<? extends T>> mapper)
<T> Uni<T>
emitter(java.util.function.Consumer<UniEmitter<? super T>> consumer)
Creates aUni
deferring the logic to the given consumer.<T,S>
Uni<T>emitter(java.util.function.Supplier<S> stateSupplier, java.util.function.BiConsumer<S,UniEmitter<? super T>> consumer)
Creates aUni
deferring the logic to the given consumer.<T> Uni<T>
failure(java.lang.Throwable failure)
Creates aUni
that emits afailure
event immediately after being subscribed to.<T> Uni<T>
failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aUni
that emits afailure
event produced using the passed supplier immediately after being subscribed to.<T> Uni<T>
future(java.util.concurrent.Future<? extends T> future)
Creates aUni
from the givenFuture
.<T> Uni<T>
future(java.util.concurrent.Future<? extends T> future, java.time.Duration timeout)
Creates aUni
from the givenFuture
.<T> Uni<T>
future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier)
Creates aUni
from the givenFuture
.<T> Uni<T>
future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier, java.time.Duration timeout)
Creates aUni
from the givenFuture
.<T> Uni<T>
item(java.util.function.Supplier<? extends T> supplier)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) value.<T,S>
Uni<T>item(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,? extends T> mapper)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) value.<T> Uni<T>
item(T item)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) item.<T> Uni<T>
multi(Multi<T> multi)
<T> Uni<T>
nothing()
<T> Uni<T>
nullItem()
Creates a newUni
that completes with anull
item.<T> Uni<T>
optional(java.util.function.Supplier<java.util.Optional<T>> supplier)
Creates a newUni
that completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()
ornull
otherwise.<T> Uni<T>
optional(java.util.Optional<T> optional)
Creates a newUni
that completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()
ornull
otherwise.<T> Uni<T>
publisher(org.reactivestreams.Publisher<? extends T> publisher)
Creates aUni
from the passedPublisher
.Uni<java.lang.Void>
voidItem()
Creates a newUni
that completes with anull
item.
-
-
-
Field Detail
-
INSTANCE
public static final UniCreate INSTANCE
-
-
Method Detail
-
converter
@CheckReturnValue public <I,T> Uni<T> converter(io.smallrye.mutiny.converters.UniConverter<I,T> converter, I instance)
Creates a newUni
from the passed instance with the passed converter.
-
completionStage
@CheckReturnValue public <T> Uni<T> completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
Creates aUni
from the givenCompletionStage
orCompletableFuture
. The producedUni
emits the item of the passedCompletionStage
. If theCompletionStage
never completes (or failed), the producedUni
would not emit theitem
orfailure
events.Cancelling the subscription on the produced
Uni
cancels the passedCompletionStage
(callingCompletableFuture.cancel(boolean)
on the future retrieved usingCompletionStage.toCompletableFuture()
.If the stage has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case, the subscriber's callbacks are called on the thread used by the passedCompletionStage
.- Type Parameters:
T
- the type of item- Parameters:
stage
- the stage, must not benull
- Returns:
- the produced
Uni
-
completionStage
@CheckReturnValue public <T,S> Uni<T> completionStage(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,? extends java.util.concurrent.CompletionStage<? extends T>> mapper)
Creates aUni
from the givenCompletionStage
orCompletableFuture
. The producedUni
emits the item of the passedCompletionStage
. If theCompletionStage
never completes (or failed), the producedUni
would not emit theitem
orfailure
events.Cancelling the subscription on the produced
Uni
cancels the passedCompletionStage
(callingCompletableFuture.cancel(boolean)
on the future retrieved usingCompletionStage.toCompletableFuture()
.If the stage has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case, the subscriber's callbacks are called on the thread used by the passedCompletionStage
.This variant of
completionStage(CompletionStage)
allows passing a state supplier. This supplier allows sharing some state between the subscribers. It is particularly useful when usingUni.repeat()
as you can pass a shared state (for example a page counter, like an AtomicInteger, if you implement pagination). The state supplier is called once, during the first subscription. Note that the mapper is called for every subscription.The state supplier should produce a container wrapping the shared state. This shared state must be thread-safe.
- Type Parameters:
T
- the type of itemS
- the type of the state- Parameters:
stateSupplier
- the state supplier, must not returnnull
, must not benull
mapper
- the taking the shared state and producing the completion stage.- Returns:
- the produced
Uni
-
completionStage
@CheckReturnValue public <T> Uni<T> completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)
Creates aUni
from the givenCompletionStage
orCompletableFuture
. The future is created by invoking the passedSupplier
lazily at subscription time.The produced
Uni
emits the item of the passedCompletionStage
. If theCompletionStage
never completes (or failed), the producedUni
would not emit an item or a failure.Cancelling the subscription on the produced
Uni
cancels the passedCompletionStage
(callingCompletableFuture.cancel(boolean)
on the future retrieved usingCompletionStage.toCompletableFuture()
.If the produced stage has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case the subscriber's callbacks are called on the thread used by the passedCompletionStage
.If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
- Returns:
- the produced
Uni
-
future
@CheckReturnValue public <T> Uni<T> future(java.util.concurrent.Future<? extends T> future)
Creates aUni
from the givenFuture
.The produced
Uni
emits the item produced by theFuture
. BecauseFuture.get()
is blocking, creating aUni
from aFuture
requires blocking a thread until the future produces a value, a failure, or the subscriber cancels. As a consequence, a thread from theInfrastructure.getDefaultExecutor()
is used, and waits until the passed future produces an outcome. If theFuture
never completes (or fails), the producedUni
will not emit any item or failure, but it would also keep the thread blocked. So, make sure yourFuture
are always completing or failing.Cancelling the subscription on the produced
Uni
cancels the passedFuture
(callingFuture.cancel(boolean)
).If the produced future has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case the callbacks of the subscriber are called on the thread used to wait the result (a thread from the Mutiny infrastructure default executor).- Type Parameters:
T
- the type of item- Parameters:
future
- the future, must not benull
- Returns:
- the produced
Uni
-
future
@CheckReturnValue public <T> Uni<T> future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier)
Creates aUni
from the givenFuture
. The future is created by invoking the passedSupplier
lazily at subscription time.The produced
Uni
emits the item produced by theFuture
supplied by the givenSupplier
. BecauseFuture.get()
is blocking, creating aUni
from aFuture
requires blocking a thread until the future produces a value, a failure, or the subscriber cancels. A thread from theInfrastructure.getDefaultExecutor()
is used, and waits until the passed future produces an outcome. If theFuture
never completes (or fails), the producedUni
will not emit an item or a failure, but it would also keep the thread blocked. So, make sure yourFuture
are always completing or failing.Cancelling the subscription on the produced
Uni
cancels the passedFuture
(callingFuture.cancel(boolean)
).If the produced future has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case the subscriber's callbacks are called on the thread used to wait for the result (so a thread from the default executor).If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
- Returns:
- the produced
Uni
-
future
@CheckReturnValue public <T> Uni<T> future(java.util.concurrent.Future<? extends T> future, java.time.Duration timeout)
Creates aUni
from the givenFuture
.The produced
Uni
emits the item produced by theFuture
. BecauseFuture.get()
is blocking, creating aUni
from aFuture
requires blocking a thread until the future produces a value, a failure, a timeout, or the subscriber cancels. As a consequence, a thread from theInfrastructure.getDefaultExecutor()
is used, and waits until the passed future produces an outcome.Cancelling the subscription on the produced
Uni
cancels the passedFuture
(callingFuture.cancel(boolean)
).If the produced future has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case the callbacks of the subscriber are called on the thread used to wait the result (a thread from the Mutiny infrastructure default executor).- Type Parameters:
T
- the type of item- Parameters:
future
- the future, must not benull
timeout
- the future timeout, must not benull
- Returns:
- the produced
Uni
-
future
@CheckReturnValue public <T> Uni<T> future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier, java.time.Duration timeout)
Creates aUni
from the givenFuture
. The future is created by invoking the passedSupplier
lazily at subscription time.The produced
Uni
emits the item produced by theFuture
supplied by the givenSupplier
. BecauseFuture.get()
is blocking, creating aUni
from aFuture
requires blocking a thread until the future produces a value, a failure, a timeout, or the subscriber cancels. A thread from theInfrastructure.getDefaultExecutor()
is used, and waits until the passed future produces an outcome.Cancelling the subscription on the produced
Uni
cancels the passedFuture
(callingFuture.cancel(boolean)
).If the produced future has already been completed (or failed), the produced
Uni
sends the item or failure immediately after subscription. If it's not the case the subscriber's callbacks are called on the thread used to wait for the result (so a thread from the default executor).If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
timeout
- the future timeout- Returns:
- the produced
Uni
-
publisher
@CheckReturnValue public <T> Uni<T> publisher(org.reactivestreams.Publisher<? extends T> publisher)
Creates aUni
from the passedPublisher
.The produced
Uni
emits the first item/value emitted by the passedPublisher
. If the publisher emits multiple values, others are dropped. If the publisher emits a failure after a value, the failure is dropped. If the publisher emits the completion signal before having emitted a value, the producedUni
emits anull
item event.When a subscriber subscribes to the produced
Uni
, it subscribes to thePublisher
and requests1
item. When the first item is received, the subscription is cancelled. Note that each Uni's subscriber would produce a new subscription.If the Uni's observer cancels its subscription, the subscription to the
Publisher
is also cancelled.- Type Parameters:
T
- the type of item- Parameters:
publisher
- the publisher, must not benull
- Returns:
- the produced
Uni
-
item
@CheckReturnValue public <T> Uni<T> item(java.util.function.Supplier<? extends T> supplier)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) value. The item is retrieved lazily at subscription time, using the passedSupplier
. Unlikedeferred(Supplier)
, the supplier produces an item and not anUni
.If the supplier produces
null
,null
is used as item event. If the supplier throws an exception, a failure event with the exception is fired. If the supplier isnull
, anIllegalArgumentException
is thrown, synchronously.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the item supplier, must not benull
, can producenull
- Returns:
- the new
Uni
-
item
@CheckReturnValue public <T,S> Uni<T> item(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,? extends T> mapper)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) value. The item is retrieved lazily at subscription time, using the passedSupplier
. Unlikedeferred(Supplier)
, the supplier produces an item and not anUni
.This variant of
item(Supplier)
allows passing a state supplier. This supplier allows sharing some state between the subscribers. It is particularly useful when usingUni.repeat()
as you can pass a shared state (for example a page counter, like an AtomicInteger, if you implement pagination). The state supplier is called once, during the first subscription. Note that the mapper is called for every subscription.The state supplier should produce a container wrapping the shared state. This shared state must be thread-safe.
- Type Parameters:
T
- the type of itemS
- the type of the state- Parameters:
stateSupplier
- the state supplier, must not returnnull
, must not benull
mapper
- the taking the shared state and producing the item.- Returns:
- the produced
Uni
-
item
@CheckReturnValue public <T> Uni<T> item(T item)
Creates a newUni
that completes immediately after being subscribed to with the specified (potentiallynull
) item.- Type Parameters:
T
- the type of item- Parameters:
item
- the item, can benull
- Returns:
- the new
Uni
-
voidItem
@CheckReturnValue public Uni<java.lang.Void> voidItem()
Creates a newUni
that completes with anull
item.- Returns:
- the new
Uni
with anull
item
-
nullItem
@CheckReturnValue public <T> Uni<T> nullItem()
Creates a newUni
that completes with anull
item.- Type Parameters:
T
- the type of item- Returns:
- the new
Uni
with anull
item
-
optional
@CheckReturnValue public <T> Uni<T> optional(java.util.Optional<T> optional)
Creates a newUni
that completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()
ornull
otherwise.- Type Parameters:
T
- the type of the produced item- Parameters:
optional
- the optional, must not benull
- Returns:
- the new
Uni
-
optional
@CheckReturnValue public <T> Uni<T> optional(java.util.function.Supplier<java.util.Optional<T>> supplier)
Creates a newUni
that completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()
ornull
otherwise. Unlikeoptional(Optional)
, the passedSupplier
is called lazily at subscription time.If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of the produced item- Parameters:
supplier
- the supplier, must not benull
, must not returnnull
- Returns:
- the new
Uni
-
emitter
@CheckReturnValue public <T> Uni<T> emitter(java.util.function.Consumer<UniEmitter<? super T>> consumer)
Creates aUni
deferring the logic to the given consumer. The consumer can be used with callback-based APIs to fire at most one item (potentiallynull
), or a failure event.Using this method, you can produce a
Uni
based on listener or callbacks APIs. You register the listener in the consumer and emits the item / failure events when the listener is invoked. Don't forget to unregister the listener on cancellation.Note that the emitter only forwards the first event, subsequent events are dropped.
If the consumer throws an exception, a failure event with the exception is fired if the first event was already fired.
- Type Parameters:
T
- the type of item- Parameters:
consumer
- callback receiving theUniEmitter
and events downstream. The callback is called for each subscriber (at subscription time). Must not benull
- Returns:
- the produced
Uni
-
emitter
@CheckReturnValue public <T,S> Uni<T> emitter(java.util.function.Supplier<S> stateSupplier, java.util.function.BiConsumer<S,UniEmitter<? super T>> consumer)
Creates aUni
deferring the logic to the given consumer. The consumer can be used with callback-based APIs to fire at most one item (potentiallynull
), or a failure event.Using this method, you can produce a
Uni
based on listener or callbacks APIs. You register the listener in the consumer and emits the item / failure events when the listener is invoked. Don't forget to unregister the listener on cancellation.Note that the emitter only forwards the first event, subsequent events are dropped.
If the consumer throws an exception, a failure event with the exception is fired if the first event was already fired. This variant of
emitter(Consumer)
allows passing a state supplier. This supplier allows sharing some state between the subscribers. It is particularly useful when usingUni.repeat()
as you can pass a shared state (for example a page counter, like an AtomicInteger, if you implement pagination). The state supplier is called once, during the first subscription. Note that the mapper is called for every subscription.The state supplier should produce a container wrapping the shared state. This shared state must be thread-safe.
- Type Parameters:
T
- the type of itemS
- the type of the state- Parameters:
stateSupplier
- the state supplier, must not returnnull
, must not benull
consumer
- callback receiving theUniEmitter
and events downstream. The callback is called for each subscriber (at subscription time). Must not benull
- Returns:
- the produced
Uni
-
deferred
@CheckReturnValue public <T> Uni<T> deferred(java.util.function.Supplier<Uni<? extends T>> supplier)
Creates aUni
thatsupplies
anUni
to subscribe to for eachUniSubscriber
. The supplier is called at subscription time.In practice, it defers the
Uni
creation at subscription time and allows each subscriber to get differentUni
. So, it does not create theUni
until asubscriber
subscribes, and creates a freshUni
for each subscriber.Unlike
item(Supplier)
, the supplier produces anUni
(and not an item).If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
- Returns:
- the produced
Uni
-
context
@Experimental("Context support is a new experimental API introduced in Mutiny 1.3.0") @CheckReturnValue public <T> Uni<T> context(java.util.function.Function<Context,Uni<? extends T>> mapper)
Creates aUni
usingFunction.apply(Object)
on the subscription-boundContext
(the mapper is called at subscription time).This method is semantically equivalent to
deferred(Supplier)
, except that it passes a context.- Type Parameters:
T
- the type of the item- Parameters:
mapper
- the mapper, must not benull
, must not producenull
- Returns:
- the produced
Uni
-
deferred
@CheckReturnValue public <T,S> Uni<T> deferred(java.util.function.Supplier<S> stateSupplier, java.util.function.Function<S,Uni<? extends T>> mapper)
Creates aUni
thatsupplies
anUni
to subscribe to for eachUniSubscriber
. The supplier is called at subscription time.In practice, it defers the
Uni
creation at subscription time and allows each subscriber to get differentUni
. So, it does not create theUni
until asubscriber
subscribes, and creates a freshUni
for each subscriber.Unlike
item(Supplier)
, the supplier produces anUni
(and not an item).If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.This variant of
deferred(Supplier)
allows passing a state supplier. This supplier allows sharing some state between the subscribers. It is particularly useful when usingUni.repeat()
as you can pass a shared state (for example a page counter, like an AtomicInteger, if you implement pagination). The state supplier is called once, during the first subscription. Note that the mapper is called for every subscription.The state supplier should produce a container wrapping the shared state. This shared state must be thread-safe.
- Type Parameters:
T
- the type of itemS
- the type of the state- Parameters:
stateSupplier
- the state supplier, must not returnnull
, must not benull
mapper
- the taking the shared state and producing the completion stage.- Returns:
- the produced
Uni
-
failure
@CheckReturnValue public <T> Uni<T> failure(java.lang.Throwable failure)
Creates aUni
that emits afailure
event immediately after being subscribed to.
-
failure
@CheckReturnValue public <T> Uni<T> failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aUni
that emits afailure
event produced using the passed supplier immediately after being subscribed to. The supplier is called at subscription time, and produces an instance ofThrowable
. If the supplier throws an exception, afailure
event is fired with this exception. If the supplier producesnull
, afailure
event is fired with aNullPointerException
.
-
nothing
@CheckReturnValue public <T> Uni<T> nothing()
- Type Parameters:
T
- the virtual type of item- Returns:
- a never completing
Uni
-
multi
@CheckReturnValue public <T> Uni<T> multi(Multi<T> multi)
Creates aUni
from the givenMulti
.When a subscriber subscribes to the returned
Uni
, it subscribes to theMulti
and requests one item. The event emitted by theMulti
are then forwarded to theUni
:- on item event, the item is fired by the produced
Uni
- on failure event, the failure is fired by the produced
Uni
- on completion event, a
null
item is fired by the producesUni
- any item or failure events received after the first event is dropped
If the subscription on the produced
Uni
is cancelled, the subscription to the passedMulti
is also cancelled.- Type Parameters:
T
- the type of item- Parameters:
multi
- the multi, must not benull
- Returns:
- the produced
Uni
- on item event, the item is fired by the produced
-
-