Class MultiCreate
- java.lang.Object
-
- io.smallrye.mutiny.groups.MultiCreate
-
public class MultiCreate extends java.lang.ObjectGroup methods allowing to createMultiinstances from various sources.
-
-
Field Summary
Fields Modifier and Type Field Description static MultiCreateINSTANCE
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Multi<T>completionStage(java.util.concurrent.CompletionStage<? extends T> stage)<T> Multi<T>completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)<I,T>
Multi<T>converter(MultiConverter<I,T> converter, I instance)Creates a newUnifrom the passed instance with the passed converter.<T> Multi<T>deferred(java.util.function.Supplier<Multi<? extends T>> supplier)<T> Multi<T>emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer)Likeemitter(Consumer, BackPressureStrategy)with theBackPressureStrategy.BUFFERstrategy.<T> Multi<T>emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, BackPressureStrategy strategy)Creates aMultideferring the logic to the given consumer.<T> Multi<T>empty()Creates aMultithat fires the completion event without having emitted any items.<T> Multi<T>failure(java.lang.Throwable failure)Creates aMultithat emits afailureevent immediately after being subscribed to.<T> Multi<T>failure(java.util.function.Supplier<java.lang.Throwable> supplier)Creates aMultithat emits afailureevent produced using the passed supplier immediately after being subscribed to.<S,T>
Multi<T>generator(java.util.function.Supplier<S> initialStateSupplier, java.util.function.BiFunction<S,GeneratorEmitter<? super T>,S> generator)Creates aMultifrom on some initial state and a generator function.<T> Multi<T>item(java.util.function.Supplier<? extends T> supplier)Creates a newMultithat emits an item immediately after being subscribed to with the specified single (potentiallynull) value.<T> Multi<T>item(T item)Creates a newMultithat emits an item immediately after being subscribed to with the specified single item.<T> Multi<T>items(java.util.function.Supplier<? extends java.util.stream.Stream<? extends T>> supplier)Creates a newMultithat emits the items immediately after being subscribed to.<T> Multi<T>items(java.util.stream.Stream<T> items)Creates a newMultithat emits the items from the passedStreamindividually after being subscribed to (according to the subscriber's request).<T> Multi<T>items(T... items)Creates a newMultithat emits the items individually after being subscribed to (according to the subscriber's request).<T> Multi<T>iterable(java.lang.Iterable<T> iterable)Creates a newMultithat emits the items individually after being subscribed to (according to the subscriber's request).<T> Multi<T>nothing()Creates aMultithat will never fire any events.<T> Multi<T>optional(java.util.function.Supplier<java.util.Optional<T>> supplier)Creates a newMultithat emits an item immediately after being subscribed to with the value contained in the optional supplied bysupplier.<T> Multi<T>optional(java.util.Optional<T> optional)Creates a newMultithat emits an item immediately after being subscribed to with the value contained in the given optional ifOptional.isPresent()or empty otherwise.<T> Multi<T>publisher(org.reactivestreams.Publisher<T> publisher)Creates aMultifrom the passedPublisher.Multi<java.lang.Integer>range(int startInclusive, int endExclusive)<R,I>
MultiResource<R,I>resource(java.util.function.Supplier<? extends R> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)Creates aMultifrom a resource, generated by a supplier function called for each individualSubscriber, while streaming the items from aPublisher/Multicreated from the resource.<T> Multi<T>safePublisher(org.reactivestreams.Publisher<T> publisher)Creates aMultifrom the passedPublisher.MultiTimePeriodticks()Creates aMultithat emitslongitems (ticks) starting with 0 and incrementing at specified time intervals.<T> Multi<T>uni(Uni<T> uni)
-
-
-
Field Detail
-
INSTANCE
public static final MultiCreate INSTANCE
-
-
Method Detail
-
converter
public <I,T> Multi<T> converter(MultiConverter<I,T> converter, I instance)
Creates a newUnifrom the passed instance with the passed converter.
-
completionStage
public <T> Multi<T> completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
Creates aMultifrom the givenCompletionStageorCompletableFuture. The producedMultiemits the item of the passedCompletionStageand then fires the completion event. If theCompletionStagenever completes (or fails), the producedMultiwould not emit anyitemorfailureevents.Cancelling the subscription on the produced
Multicancels the passedCompletionStage(callingCompletableFuture.cancel(boolean)on the future retrieved usingCompletionStage.toCompletableFuture().If the stage has already been completed (or failed), the produced
Multisends 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 completion stage redeemsnull, it fires the completion event without any item.- Type Parameters:
T- the type of item- Parameters:
stage- the stage, must not benull- Returns:
- the produced
Multi
-
completionStage
public <T> Multi<T> completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)
Creates aMultifrom the givenCompletionStageorCompletableFuture. The future is created by invoking the passedSupplierlazily at subscription time.The produced
Multiemits the item of the passedCompletionStagefollowed by the completion event. If theCompletionStagenever completes (or failed), the producedMultiwould not emit an item or a failure.Cancelling the subscription on the produced
Multicancels the passedCompletionStage(callingCompletableFuture.cancel(boolean)on the future retrieved usingCompletionStage.toCompletableFuture().If the produced stage has already been completed (or failed), the produced
Multisends the item or failure immediately after subscription. In the case of item, the event is followed by the completion event. If the produced stage is not yet completed, the subscriber's callbacks are called on the thread used by the passedCompletionStage.If the produced completion stage redeems
null, it fires the completion event without any 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
Multi
-
safePublisher
public <T> Multi<T> safePublisher(org.reactivestreams.Publisher<T> publisher)
Creates aMultifrom the passedPublisher.It is assumed that the
Publisheris fully compliant with the Reactive Streams protocol and passes the TCK. If this is not the case usepublisher(Publisher)instead.When a subscriber subscribes to the produced
Multi, it subscribes to thePublisherand delegate the requests. Note that each Multi's subscriber would produce a new subscription.If the Multi's observer cancels its subscription, the subscription to the
Publisheris also cancelled.- Type Parameters:
T- the type of item- Parameters:
publisher- the publisher, must not benull- Returns:
- the produced
Multi - See Also:
publisher(Publisher)
-
publisher
public <T> Multi<T> publisher(org.reactivestreams.Publisher<T> publisher)
Creates aMultifrom the passedPublisher.The
Publisheris not assumed to be fully compliant with the Reactive Streams TCK, hence it is wrapped around a subscriber that enforces the Reactive Streams protocol. If you know thePublisheris safe then you should usesafePublisher(Publisher)instead.When a subscriber subscribes to the produced
Multi, it subscribes to thePublisherand delegate the requests. Note that each Multi's subscriber would produce a new subscription.If the Multi's observer cancels its subscription, the subscription to the
Publisheris also cancelled.- Type Parameters:
T- the type of item- Parameters:
publisher- the publisher, must not benull- Returns:
- the produced
Multi - See Also:
safePublisher(Publisher)
-
uni
public <T> Multi<T> uni(Uni<T> uni)
Creates an never ofMultifrom the givenUni.When a subscriber subscribes to the returned
Multiand request an item, it subscribes to the givenUniand the events from thisUniare propagated to theMulti:- if the
Uniemits a non-nullitem - this item is propagated to theMultiand followed with the completion event - if the
Uniemits anullitem - theMultifires the completion event - if the
Uniemits a failure, this failure event is propagated by theMulti
It's important to note that the subscription to the
Unihappens when the subscriber to the producedMultirequests values, and not at subscription time. - if the
-
item
public <T> Multi<T> item(java.util.function.Supplier<? extends T> supplier)
Creates a newMultithat emits an item immediately after being subscribed to with the specified single (potentiallynull) value. The value is retrieved lazily at subscription time, using the passedSupplier. Unlikedeferred(Supplier), the supplier produces an item and not aMulti.If the supplier produces
null, the producedMultifires the completion event. If the supplier produces a non-nullitem, the producedMultifires an item event followed with the completion event. If the supplier throws an exception, a failure event with the exception is fired.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
supplier- the item supplier, must not benull, can producenull- Returns:
- the new
Multi
-
items
public <T> Multi<T> items(java.util.function.Supplier<? extends java.util.stream.Stream<? extends T>> supplier)
Creates a newMultithat emits the items immediately after being subscribed to. The individual items come from theStreamsupplied by the givenSupplier. This supplier is called at subscription time.If the supplier produces
null, the producedMultifires a failure event. If the supplier produces an empty stream, the producedMultifires a completion event. For each item from the supplied stream, an item event is fired. When all the items have been emitted, the completion event is fired. If the supplier throws an exception, a failure event with the exception is fired. The stream is consumed sequentially.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
supplier- the item supplier, must not benull, must not producenull- Returns:
- the new
Multi
-
item
public <T> Multi<T> item(T item)
-
items
@SafeVarargs public final <T> Multi<T> items(T... items)
Creates a newMultithat emits the items individually after being subscribed to (according to the subscriber's request).If
itemsisnull, anIllegalArgumentExceptionis thrown at call time. If one of the item fromitemsisnull, a failure event is fired (with anIllegalArgumentException). When all the items have been emitted, the completion event is fired.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
items- the items, must not benull, must not containnull- Returns:
- the new
Multi
-
iterable
public <T> Multi<T> iterable(java.lang.Iterable<T> iterable)
Creates a newMultithat emits the items individually after being subscribed to (according to the subscriber's request).If
iterableisnull, anIllegalArgumentExceptionis thrown at call time. If one of the item fromiterableisnull, a failure event is fired (with anIllegalArgumentException). When all the items have been emitted, the completion event is fired.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
iterable- the iterable of items, must not benull, must not containnull- Returns:
- the new
Multi
-
items
public <T> Multi<T> items(java.util.stream.Stream<T> items)
Creates a newMultithat emits the items from the passedStreamindividually after being subscribed to (according to the subscriber's request).If
itemsisnull, anIllegalArgumentExceptionis thrown at call time. If one of the item from the stream isnull, a failure event is fired (with anIllegalArgumentException). When all the items have been emitted, the completion event is fired. The stream is consumed sequentially.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
items- the items, must not benull, must not containnull- Returns:
- the new
Multi
-
optional
public <T> Multi<T> optional(java.util.Optional<T> optional)
Creates a newMultithat emits an item immediately after being subscribed to with the value contained in the given optional ifOptional.isPresent()or empty otherwise.
-
optional
public <T> Multi<T> optional(java.util.function.Supplier<java.util.Optional<T>> supplier)
Creates a newMultithat emits an item immediately after being subscribed to with the value contained in the optional supplied bysupplier.If the optional is empty, an empty
Multiis produced. Otherwise the contained value is emitted as item, followed with the completion event. 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
Multi
-
emitter
public <T> Multi<T> emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer)
Likeemitter(Consumer, BackPressureStrategy)with theBackPressureStrategy.BUFFERstrategy. Note that to create hot streams, you should use aBroadcastProcessor.- Type Parameters:
T- the type of item emitted by the produced Multi- Parameters:
consumer- the consumer receiving the emitter, must not benull- Returns:
- the produced
Multi
-
emitter
public <T> Multi<T> emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, BackPressureStrategy strategy)
Creates aMultideferring the logic to the given consumer. The consumer can be used with callback-based APIs to fire items (non-null), failure or completion events.Emitting
nullvalue is not supported. Emitting values after having fired a failure or the completion event is a no-op. So subsequent item events are dropped.Using this method, you can produce a
Multibased on listener or callbacks APIs. You register the listener in the consumer and emits the items / failure / completion events when the listener is invoked. Don't forget to unregister the listener on cancellation.If the consumer throws an exception, a failure event with the exception is fired. Note that to create hot streams, you should use a
BroadcastProcessor.- Type Parameters:
T- the type of items emitted by the emitter. Must not benull- Parameters:
consumer- callback receiving theMultiEmitterand events downstream. The callback is called for each subscriber (at subscription time). Must not benullstrategy- the back pressure strategy to apply when the downstream subscriber cannot keep up with the items emitted by the emitter.- Returns:
- the produced
Multi
-
deferred
public <T> Multi<T> deferred(java.util.function.Supplier<Multi<? extends T>> supplier)
Creates aMultithatsuppliesanMultito subscribe to for eachSubscriber. The supplier is called at subscription time.In practice, it defers the
Multicreation at subscription time and allows each subscriber to get differentMulti. So, it does not create theMultiuntil ansubscribersubscribes, and creates a freshMultifor each subscriber.Unlike
item(Supplier), the supplier produces anMulti(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
Multi
-
failure
public <T> Multi<T> failure(java.lang.Throwable failure)
Creates aMultithat emits afailureevent immediately after being subscribed to.
-
failure
public <T> Multi<T> failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aMultithat emits afailureevent produced using the passed supplier immediately after being subscribed to. The supplier is called at subscription time, and produces an never ofThrowable. If the supplier throws an exception, afailureevent is fired with this exception. If the supplier producesnull, afailureevent is fired with aNullPointerException.
-
nothing
public <T> Multi<T> nothing()
Creates aMultithat will never fire any events.- Type Parameters:
T- the virtual type of item- Returns:
- a never emitting
Multi
-
empty
public <T> Multi<T> empty()
Creates aMultithat fires the completion event without having emitted any items. An emptyMultidoes not fires a failure event either.- Type Parameters:
T- the virtual type of item- Returns:
- an empty
Multi
-
ticks
public MultiTimePeriod ticks()
Creates aMultithat emitslongitems (ticks) starting with 0 and incrementing at specified time intervals.Be aware that if the subscriber does not request enough items in time, a back pressure failure is fired. The produced
Multinever completes until cancellation by the subscriber.The callbacks are invoked on the executor passed in
MultiTimePeriod.onExecutor(ScheduledExecutorService).- Returns:
- the object to configure the time period (initial delay, executor, interval)
-
range
public Multi<java.lang.Integer> range(int startInclusive, int endExclusive)
Creates aMultiemitting the sequence of integer fromstartInclusivetoendExclusive. Once all the integers have been emitted, the completion event is fired.- Parameters:
startInclusive- the start integer (inclusive)endExclusive- the end integer (exclusive)- Returns:
- the
Multiemitting the items
-
resource
public <R,I> MultiResource<R,I> resource(java.util.function.Supplier<? extends R> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)
Creates aMultifrom a resource, generated by a supplier function called for each individualSubscriber, while streaming the items from aPublisher/Multicreated from the resource.This method gets a resource and creates a
Publisherfrom this resource (by calling thestreamSupplierfunction). The subscriber receives the items from thisPublisher. When the stream completes, fails or when the subscriber cancels the subscription, a finalizer is called to close the resource. This cleanup process can be either synchronous and asynchronous, as well as distinct for each type of event. This method can be seen as a reactive version of the "try/finally" construct.- Type Parameters:
R- the type of the resource.I- the type of items emitted by the stream produced by thestreamSupplier.- Parameters:
resourceSupplier- a supplier called for each subscriber to generate the resource, must not benull.streamSupplier- a function returning the stream for the given resource instance, must not benull.- Returns:
- an object to configure the finalizers.
-
generator
public <S,T> Multi<T> generator(java.util.function.Supplier<S> initialStateSupplier, java.util.function.BiFunction<S,GeneratorEmitter<? super T>,S> generator)
Creates aMultifrom on some initial state and a generator function.The generator function accepts the current state and a
GeneratorEmitterto emit items, failures and completion events. The function shall return the new state which will be used for the next item generation, if any. The state can benull, but emitted items cannot benull. A failure is propagated downstream if the function throws an exception.Items are being generated based on subscription requests. Requesting
Long.MAX_VALUEitems can possibly make for an infinite stream unless the generator function callsGeneratorEmitter.complete()at some point.- Type Parameters:
S- the state typeT- the items type- Parameters:
initialStateSupplier- a supplier for the initial state, must not benullbut can supplynullgenerator- the generator function, returns the new state for the next item generation- Returns:
- a new
Multi
-
-