- java.lang.Object
-
- io.smallrye.mutiny.groups.UniCreate
-
public class UniCreate extends java.lang.ObjectGroup methods allowing to createUniinstances 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 newUnifrom 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 aUnideferring 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 aUnideferring the logic to the given consumer.<T> Uni<T>failure(java.lang.Throwable failure)Creates aUnithat emits afailureevent immediately after being subscribed to.<T> Uni<T>failure(java.util.function.Supplier<java.lang.Throwable> supplier)Creates aUnithat emits afailureevent produced using the passed supplier immediately after being subscribed to.<T> Uni<T>future(java.util.concurrent.Future<? extends T> future)Creates aUnifrom the givenFuture.<T> Uni<T>future(java.util.concurrent.Future<? extends T> future, java.time.Duration timeout)Creates aUnifrom the givenFuture.<T> Uni<T>future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier)Creates aUnifrom the givenFuture.<T> Uni<T>future(java.util.function.Supplier<java.util.concurrent.Future<? extends T>> supplier, java.time.Duration timeout)Creates aUnifrom the givenFuture.<T> Uni<T>item(java.util.function.Supplier<? extends T> supplier)Creates a newUnithat 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 newUnithat completes immediately after being subscribed to with the specified (potentiallynull) value.<T> Uni<T>item(T item)Creates a newUnithat 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 newUnithat completes with anullitem.<T> Uni<T>optional(java.util.function.Supplier<java.util.Optional<T>> supplier)Creates a newUnithat completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()ornullotherwise.<T> Uni<T>optional(java.util.Optional<T> optional)Creates a newUnithat completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()ornullotherwise.<T> Uni<T>publisher(java.util.concurrent.Flow.Publisher<? extends T> publisher)Creates aUnifrom the passedFlow.Publisher.Uni<java.lang.Void>voidItem()Creates a newUnithat completes with anullitem.
-
-
-
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 newUnifrom the passed instance with the passed converter.
-
completionStage
@CheckReturnValue public <T> Uni<T> completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
Creates aUnifrom the givenCompletionStageorCompletableFuture. The producedUniemits the item of the passedCompletionStage. If theCompletionStagenever completes (or failed), the producedUniwould not emit theitemorfailureevents.Cancelling the subscription on the produced
Unicancels the passedCompletionStage(callingCompletableFuture.cancel(boolean)on the future retrieved usingCompletionStage.toCompletableFuture().If the stage has already been completed (or failed), the produced
Unisends 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 aUnifrom the givenCompletionStageorCompletableFuture. The producedUniemits the item of the passedCompletionStage. If theCompletionStagenever completes (or failed), the producedUniwould not emit theitemorfailureevents.Cancelling the subscription on the produced
Unicancels the passedCompletionStage(callingCompletableFuture.cancel(boolean)on the future retrieved usingCompletionStage.toCompletableFuture().If the stage has already been completed (or failed), the produced
Unisends 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 benullmapper- 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 aUnifrom the givenCompletionStageorCompletableFuture. The future is created by invoking the passedSupplierlazily at subscription time.The produced
Uniemits the item of the passedCompletionStage. If theCompletionStagenever completes (or failed), the producedUniwould not emit an item or a failure.Cancelling the subscription on the produced
Unicancels the passedCompletionStage(callingCompletableFuture.cancel(boolean)on the future retrieved usingCompletionStage.toCompletableFuture().If the produced stage has already been completed (or failed), the produced
Unisends 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 aNullPointerExceptionis 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 aUnifrom the givenFuture.The produced
Uniemits the item produced by theFuture. BecauseFuture.get()is blocking, creating aUnifrom aFuturerequires 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 theFuturenever completes (or fails), the producedUniwill not emit any item or failure, but it would also keep the thread blocked. So, make sure yourFutureare always completing or failing.Cancelling the subscription on the produced
Unicancels the passedFuture(callingFuture.cancel(boolean)).If the produced future has already been completed (or failed), the produced
Unisends 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 aUnifrom the givenFuture. The future is created by invoking the passedSupplierlazily at subscription time.The produced
Uniemits the item produced by theFuturesupplied by the givenSupplier. BecauseFuture.get()is blocking, creating aUnifrom aFuturerequires 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 theFuturenever completes (or fails), the producedUniwill not emit an item or a failure, but it would also keep the thread blocked. So, make sure yourFutureare always completing or failing.Cancelling the subscription on the produced
Unicancels the passedFuture(callingFuture.cancel(boolean)).If the produced future has already been completed (or failed), the produced
Unisends 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 aNullPointerExceptionis 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 aUnifrom the givenFuture.The produced
Uniemits the item produced by theFuture. BecauseFuture.get()is blocking, creating aUnifrom aFuturerequires 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
Unicancels the passedFuture(callingFuture.cancel(boolean)).If the produced future has already been completed (or failed), the produced
Unisends 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 benulltimeout- 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 aUnifrom the givenFuture. The future is created by invoking the passedSupplierlazily at subscription time.The produced
Uniemits the item produced by theFuturesupplied by the givenSupplier. BecauseFuture.get()is blocking, creating aUnifrom aFuturerequires 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
Unicancels the passedFuture(callingFuture.cancel(boolean)).If the produced future has already been completed (or failed), the produced
Unisends 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 aNullPointerExceptionis fired.- Type Parameters:
T- the type of item- Parameters:
supplier- the supplier, must not benull, must not producenulltimeout- the future timeout- Returns:
- the produced
Uni
-
publisher
@CheckReturnValue public <T> Uni<T> publisher(java.util.concurrent.Flow.Publisher<? extends T> publisher)
Creates aUnifrom the passedFlow.Publisher.The produced
Uniemits the first item/value emitted by the passedFlow.Publisher. 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 producedUniemits anullitem event.When a subscriber subscribes to the produced
Uni, it subscribes to theFlow.Publisherand requests1item. 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
Flow.Publisheris 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 newUnithat 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,nullis used as item event. If the supplier throws an exception, a failure event with the exception is fired. If the supplier isnull, anIllegalArgumentExceptionis 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 newUnithat 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 benullmapper- the taking the shared state and producing the item.- Returns:
- the produced
Uni
-
item
@CheckReturnValue public <T> Uni<T> item(T item)
Creates a newUnithat 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 newUnithat completes with anullitem.- Returns:
- the new
Uniwith anullitem
-
nullItem
@CheckReturnValue public <T> Uni<T> nullItem()
Creates a newUnithat completes with anullitem.- Type Parameters:
T- the type of item- Returns:
- the new
Uniwith anullitem
-
optional
@CheckReturnValue public <T> Uni<T> optional(java.util.Optional<T> optional)
Creates a newUnithat completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()ornullotherwise.- 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 newUnithat completes immediately after being subscribed to with the item based on the value contained in the given optional ifOptional.isPresent()ornullotherwise. Unlikeoptional(Optional), the passedSupplieris 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 aNullPointerExceptionis 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 aUnideferring 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
Unibased 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 theUniEmitterand 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 aUnideferring 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
Unibased 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 benullconsumer- callback receiving theUniEmitterand 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 aUnithatsuppliesanUnito subscribe to for eachUniSubscriber. The supplier is called at subscription time.In practice, it defers the
Unicreation at subscription time and allows each subscriber to get differentUni. So, it does not create theUniuntil asubscribersubscribes, and creates a freshUnifor 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 aNullPointerExceptionis fired.- Type Parameters:
T- the type of item- Parameters:
supplier- the supplier, must not benull, must not producenull- Returns:
- the produced
Uni
-
context
@CheckReturnValue public <T> Uni<T> context(java.util.function.Function<Context,Uni<? extends T>> mapper)
Creates aUniusingFunction.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 aUnithatsuppliesanUnito subscribe to for eachUniSubscriber. The supplier is called at subscription time.In practice, it defers the
Unicreation at subscription time and allows each subscriber to get differentUni. So, it does not create theUniuntil asubscribersubscribes, and creates a freshUnifor 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 aNullPointerExceptionis 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 benullmapper- 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 aUnithat emits afailureevent immediately after being subscribed to.
-
failure
@CheckReturnValue public <T> Uni<T> failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aUnithat emits afailureevent 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, afailureevent is fired with this exception. If the supplier producesnull, afailureevent 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 aUnifrom the givenMulti.When a subscriber subscribes to the returned
Uni, it subscribes to theMultiand requests one item. The event emitted by theMultiare 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
nullitem is fired by the producesUni - any item or failure events received after the first event is dropped
If the subscription on the produced
Uniis cancelled, the subscription to the passedMultiis 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
-
-