Class Single<T>
- Type Parameters:
T
- Type of the result of the single.
- Direct Known Subclasses:
SubscribableSingle
How to subscribe?
This class does not provide a way to subscribe using aSingleSource.Subscriber
as such calls are
ambiguous about the intent whether the subscribe is part of the same source (a.k.a an operator) or it is a terminal
subscribe. If it is required to subscribe to a source, then a source adapter
can be used to
convert to a SingleSource
.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionafterCancel
(Runnable onCancel) Invokes theonCancel
Runnable
argument afterCancellable.cancel()
is called for Subscriptions of the returnedSingle
.afterFinally
(SingleTerminalSignalConsumer<? super T> doFinally) Invokes the corresponding method onafterFinally
SingleTerminalSignalConsumer
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.afterFinally
(TerminalSignalConsumer doFinally) Invokes the corresponding method onafterFinally
TerminalSignalConsumer
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.afterFinally
(Runnable doFinally) Invokes thewhenFinally
Runnable
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.afterOnError
(Consumer<Throwable> onError) Invokes theonError
Consumer
argument afterSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.afterOnSubscribe
(Consumer<Cancellable> onSubscribe) Invokes theonSubscribe
Consumer
argument afterSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.afterOnSuccess
(Consumer<? super T> onSuccess) Invokes theonSuccess
Consumer
argument afterSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.afterSubscriber
(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) on each call to subscribe and invokes all theSingleSource.Subscriber
methods after theSingleSource.Subscriber
s of the returnedSingle
.static <T> Single<T>
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first.static <T> Single<T>
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first.static <T> Single<T>
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first.static <T> Single<T>
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first.beforeCancel
(Runnable onCancel) Invokes theonCancel
Runnable
argument beforeCancellable.cancel()
is called for Subscriptions of the returnedSingle
.beforeFinally
(SingleTerminalSignalConsumer<? super T> doFinally) Invokes the corresponding method onbeforeFinally
SingleTerminalSignalConsumer
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.beforeFinally
(TerminalSignalConsumer doFinally) Invokes the corresponding method onbeforeFinally
TerminalSignalConsumer
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.beforeFinally
(Runnable doFinally) Invokes thewhenFinally
Runnable
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.beforeOnError
(Consumer<Throwable> onError) Invokes theonError
Consumer
argument beforeSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.beforeOnSubscribe
(Consumer<Cancellable> onSubscribe) Invokes theonSubscribe
Consumer
argument beforeSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.beforeOnSuccess
(Consumer<? super T> onSuccess) Invokes theonSuccess
Consumer
argument beforeSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.beforeSubscriber
(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) on each call to subscribe and invokes all theSingleSource.Subscriber
methods before theSingleSource.Subscriber
s of the returnedSingle
.cache()
Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s.cache
(int minSubscribers) Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s.cache
(int minSubscribers, boolean cancelUpstream) Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s.cache
(int minSubscribers, boolean cancelUpstream, BiFunction<T, Throwable, Completable> terminalResubscribe) Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s.final <R> Single<R>
static <T> Single<Collection<T>>
collectUnordered
(int maxConcurrency, Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.static <T> Single<Collection<T>>
collectUnordered
(Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.static <T> Single<Collection<T>>
collectUnordered
(Iterable<? extends Single<? extends T>> singles) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.static <T> Single<Collection<T>>
collectUnordered
(Iterable<? extends Single<? extends T>> singles, int maxConcurrency) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.static <T> Single<Collection<T>>
collectUnorderedDelayError
(int maxConcurrency, Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.static <T> Single<Collection<T>>
collectUnorderedDelayError
(Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.static <T> Single<Collection<T>>
collectUnorderedDelayError
(Iterable<? extends Single<? extends T>> singles) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.static <T> Single<Collection<T>>
collectUnorderedDelayError
(Iterable<? extends Single<? extends T>> singles, int maxConcurrency) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.concat
(Completable next) Returns aSingle
that emits the result of thisSingle
afternext
Completable
terminates successfully.Deprecated.concatDeferSubscribe
(Publisher<? extends T> next) This method is likeconcat(Publisher)
subscribe to thenext
Publisher
will be deferred until demand is received (at least 2 items requested).concatPropagateCancel
(Publisher<? extends T> next) This method is likeconcat(Publisher)
exceptnext
will be subscribed to and cancelled if thisPublisher
is cancelled or terminates withSingleSource.Subscriber.onError(Throwable)
.static <T> Single<T>
Defer creation of aSingle
till it is subscribed to.static <T> Single<T>
Creates a realizedSingle
which always completes with the provided errorcause
.final <R> Single<R>
final Completable
flatMapCompletable
(Function<? super T, ? extends Completable> next) final <R> Publisher<R>
flatMapPublisher
(Function<? super T, ? extends Publisher<? extends R>> next) static <T> Single<T>
fromCallable
(Callable<T> callable) Creates aSingle
which when subscribed will invokeCallable.call()
on the passedCallable
and emit the value returned by that invocation from the returnedSingle
.static <T> Single<T>
fromFuture
(Future<? extends T> future) static <T> Single<T>
fromStage
(CompletionStage<? extends T> stage) Convert from aCompletionStage
to aSingle
.static <T> Single<T>
fromSupplier
(Supplier<T> supplier) Creates aSingle
which when subscribed will invokeSupplier.get()
on the passedSupplier
and emit the value returned by that invocation from the returnedSingle
.protected abstract void
handleSubscribe
(SingleSource.Subscriber<? super T> subscriber) Handles a subscriber to thisSingle
.final Completable
Ignores the result of thisSingle
and forwards the termination signal to the returnedCompletable
.final <R> Single<R>
liftAsync
(SingleOperator<? super T, ? extends R> operator) This method requires advanced knowledge of building operators.final <R> Single<R>
liftSync
(SingleOperator<? super T, ? extends R> operator) This method requires advanced knowledge of building operators.final <R> Single<R>
Maps the result of this single to a different type.static <T> Single<T>
never()
Creates aSingle
that never terminates.onErrorMap
(Class<E> type, Function<? super E, ? extends Throwable> mapper) Transform errors emitted on thisSingle
which matchtype
into a different error.onErrorMap
(Function<? super Throwable, ? extends Throwable> mapper) Transform errors emitted on thisSingle
into a different error.onErrorMap
(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends Throwable> mapper) Transform errors emitted on thisSingle
which matchpredicate
into a different error.onErrorResume
(Class<E> type, Function<? super E, ? extends Single<? extends T>> nextFactory) onErrorResume
(Function<? super Throwable, ? extends Single<? extends T>> nextFactory) onErrorResume
(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends Single<? extends T>> nextFactory) onErrorReturn
(Class<E> type, Function<? super E, ? extends T> itemSupplier) Transform errors emitted on thisSingle
which matchtype
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g.onErrorReturn
(Function<? super Throwable, ? extends T> itemSupplier) Transform errors emitted on thisSingle
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g.onErrorReturn
(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends T> itemSupplier) Transform errors emitted on thisSingle
which matchpredicate
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g.Creates a newSingle
that will use the passedExecutor
to invoke allSingleSource.Subscriber
methods.publishOn
(Executor executor, BooleanSupplier shouldOffload) Creates a newSingle
that may use the passedExecutor
to invoke allSingleSource.Subscriber
methods.repeat
(BiIntPredicate<? super T> shouldRepeat) repeat
(IntPredicate shouldRepeat) repeatWhen
(BiIntFunction<? super T, ? extends Completable> repeatWhen) Re-subscribes to thisSingle
when it completes and theCompletable
returned by the suppliedBiIntFunction
completes successfully.repeatWhen
(IntFunction<? extends Completable> repeatWhen) Re-subscribes to thisSingle
when it completes and theCompletable
returned by the suppliedIntFunction
completes successfully.retry
(BiIntPredicate<Throwable> shouldRetry) retryWhen
(BiIntFunction<Throwable, ? extends Completable> retryWhen) Re-subscribes to thisSingle
if an error is emitted and theCompletable
returned by the suppliedBiIntFunction
completes successfully.setContextOnSubscribe
(ContextMap context) Signifies that when the returnedSingle
is subscribed to, theAsyncContext
will be shared instead of making acopy
.final Cancellable
Subscribe to thisSingle
, emit the result to the passedConsumer
and log anySingleSource.Subscriber.onError(Throwable)
at debug level.final Cancellable
protected final void
subscribeInternal
(SingleSource.Subscriber<? super T> subscriber) A internal subscribe method similar toSingleSource.subscribe(Subscriber)
which can be used by different implementations to subscribe.subscribeOn
(Executor executor) Creates a newSingle
that will use the passedExecutor
to invoke the following methods: AllCancellable
methods. ThehandleSubscribe(SingleSource.Subscriber)
method. This method does not override precedingExecutor
s, if any, specified forthis
Single
.subscribeOn
(Executor executor, BooleanSupplier shouldOffload) Creates a newSingle
that may use the passedExecutor
to invoke the following methods: AllCancellable
methods. ThehandleSubscribe(SingleSource.Subscriber)
method. This method does not override precedingExecutor
s, if any, specified forthis
Single
.static <T> Single<T>
succeeded
(T value) Creates a realizedSingle
which always completes successfully with the providedvalue
.Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination.Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination.Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination.Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with a with aTimeoutException
if timeduration
elapses between subscribe and termination.final Completable
Ignores the result of thisSingle
and forwards the termination signal to the returnedCompletable
.final CompletionStage<T>
Convert thisSingle
to aCompletionStage
.toFuture()
Converts thisSingle
to aPublisher
.whenCancel
(Runnable onCancel) Invokes theonCancel
Runnable
argument whenCancellable.cancel()
is called for Subscriptions of the returnedSingle
.whenFinally
(SingleTerminalSignalConsumer<? super T> doFinally) Invokes the corresponding method onwhenFinally
SingleTerminalSignalConsumer
argument when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.whenFinally
(TerminalSignalConsumer doFinally) Invokes the corresponding method onwhenFinally
TerminalSignalConsumer
argument when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.whenFinally
(Runnable doFinally) Invokes thewhenFinally
Runnable
argument exactly once, when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for Subscriptions/SingleSource.Subscriber
s of the returnedSingle
.whenOnError
(Consumer<Throwable> onError) Invokes theonError
Consumer
argument whenSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.whenOnSubscribe
(Consumer<Cancellable> onSubscribe) Invokes theonSubscribe
Consumer
argument whenSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.whenOnSuccess
(Consumer<? super T> onSuccess) Invokes theonSuccess
Consumer
argument whenSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.whenSubscriber
(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) for each new subscribe and invokes methods on thatSingleSource.Subscriber
when the corresponding methods are called forSingleSource.Subscriber
s of the returnedSingle
.static <T1,
T2, T3, R>
Single<R>zip
(Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Function3<? super T1, ? super T2, ? super T3, ? extends R> zipper) static <T1,
T2, T3, T4, R>
Single<R>zip
(Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Single<? extends T4> s4, Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) static <T1,
T2, R> Single<R> zip
(Single<? extends T1> s1, Single<? extends T2> s2, BiFunction<? super T1, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
.static <R> Single<R>
static <T1,
T2, T3, R>
Single<R>zipDelayError
(Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Function3<? super T1, ? super T2, ? super T3, ? extends R> zipper) static <T1,
T2, T3, T4, R>
Single<R>zipDelayError
(Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Single<? extends T4> s4, Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) static <T1,
T2, R> Single<R> zipDelayError
(Single<? extends T1> s1, Single<? extends T2> s2, BiFunction<? super T1, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
.static <R> Single<R>
zipDelayError
(Function<? super Object[], ? extends R> zipper, Single<?>... singles) final <T2,
R> Single<R> zipWith
(Single<? extends T2> other, BiFunction<? super T, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
.final <T2,
R> Single<R> zipWithDelayError
(Single<? extends T2> other, BiFunction<? super T, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
.
-
Constructor Details
-
Single
protected Single()New instance.
-
-
Method Details
-
map
Maps the result of this single to a different type. Error, if any is forwarded to the returnedSingle
.This method provides a data transformation in sequential programming similar to:
T tResult = resultOfThisSingle(); R rResult = mapper.apply(tResult);
-
cast
Cast thisSingle
from typeSingle
to typeSingle
.This method provides a data transformation in sequential programming similar to:
T tResult = resultOfThisSingle(); R rResult = clazz.cast(tResult);
- Type Parameters:
R
- The resulting type of the cast operation.- Parameters:
clazz
- The type to cast to.- Returns:
- The cast of this
Single
to typeSingle
. Terminates with aClassCastException
if signals cannot be cast to typeSingle
. - See Also:
-
onErrorReturn
Transform errors emitted on thisSingle
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error).This method provides a data transformation in sequential programming similar to:
T result = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { return itemSupplier.apply(cause); } return result;
- Parameters:
itemSupplier
- returns the element to emit toSingleSource.Subscriber.onSuccess(Object)
.- Returns:
- A
Single
which transform errors emitted on thisSingle
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error). - See Also:
-
onErrorReturn
public final <E extends Throwable> Single<T> onErrorReturn(Class<E> type, Function<? super E, ? extends T> itemSupplier) Transform errors emitted on thisSingle
which matchtype
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error).This method provides a data transformation in sequential programming similar to:
T result = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { if (!type.isInstance(cause)) { throw cause; } return itemSupplier.apply(cause); } return result;
- Type Parameters:
E
- The type ofThrowable
to transform.- Parameters:
type
- TheThrowable
type to filter, operator will not apply for errors which don't match this type.itemSupplier
- returns the element to emit toSingleSource.Subscriber.onSuccess(Object)
.- Returns:
- A
Single
which transform errors emitted on thisSingle
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error). - See Also:
-
onErrorReturn
public final Single<T> onErrorReturn(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends T> itemSupplier) Transform errors emitted on thisSingle
which matchpredicate
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error).This method provides a data transformation in sequential programming similar to:
T result = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { if (!predicate.test(cause)) { throw cause; } return itemSupplier.apply(cause); } return result;
- Parameters:
predicate
- returnstrue
if theThrowable
should be transformed toSingleSource.Subscriber.onSuccess(Object)
signal. Returnsfalse
to propagate the error.itemSupplier
- returns the element to emit toSingleSource.Subscriber.onSuccess(Object)
.- Returns:
- A
Single
which transform errors emitted on thisSingle
intoSingleSource.Subscriber.onSuccess(Object)
signal (e.g. swallows the error). - See Also:
-
onErrorMap
Transform errors emitted on thisSingle
into a different error.This method provides a data transformation in sequential programming similar to:
T result = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { throw mapper.apply(cause); } return result;
-
onErrorMap
public final <E extends Throwable> Single<T> onErrorMap(Class<E> type, Function<? super E, ? extends Throwable> mapper) Transform errors emitted on thisSingle
which matchtype
into a different error.This method provides a data transformation in sequential programming similar to:
T result = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { if (type.isInstance(cause)) { throw mapper.apply(cause); } else { throw cause; } } return result;
- Type Parameters:
E
- The type ofThrowable
to transform.- Parameters:
type
- TheThrowable
type to filter, operator will not apply for errors which don't match this type.mapper
- returns the error used to terminate the returnedSingle
.- Returns:
- A
Single
which transform errors emitted on thisSingle
into a different error. - See Also:
-
onErrorMap
public final Single<T> onErrorMap(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends Throwable> mapper) Transform errors emitted on thisSingle
which matchpredicate
into a different error.This method provides a data transformation in sequential programming similar to:
T results = resultOfThisSingle(); try { terminalOfThisSingle(); } catch (Throwable cause) { if (predicate.test(cause)) { throw mapper.apply(cause); } else { throw cause; } } return result;
-
onErrorResume
public final Single<T> onErrorResume(Function<? super Throwable, ? extends Single<? extends T>> nextFactory) Recover from any error emitted by thisSingle
by using anotherSingle
provided by the passednextFactory
.This method provides similar capabilities to a try/catch block in sequential programming:
T result; try { result = resultOfThisSingle(); } catch (Throwable cause) { // Note that nextFactory returning a error Single is like re-throwing (nextFactory shouldn't throw). result = nextFactory.apply(cause); } return result;
-
onErrorResume
public final <E extends Throwable> Single<T> onErrorResume(Class<E> type, Function<? super E, ? extends Single<? extends T>> nextFactory) Recover from errors emitted by thisSingle
which matchtype
by using anotherSingle
provided by the passednextFactory
.This method provides similar capabilities to a try/catch block in sequential programming:
T result; try { result = resultOfThisSingle(); } catch (Throwable cause) { if (type.isInstance(cause)) { // Note that nextFactory returning a error Single is like re-throwing (nextFactory shouldn't throw). result = nextFactory.apply(cause); } else { throw cause; } } return result;
- Type Parameters:
E
- The type ofThrowable
to transform.- Parameters:
type
- TheThrowable
type to filter, operator will not apply for errors which don't match this type.nextFactory
- Returns the nextSingle
, when thisSingle
emits an error.- Returns:
- A
Single
that recovers from an error from thisSingle
by using anotherSingle
provided by the passednextFactory
. - See Also:
-
onErrorResume
public final Single<T> onErrorResume(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends Single<? extends T>> nextFactory) Recover from errors emitted by thisSingle
which matchpredicate
by using anotherSingle
provided by the passednextFactory
.This method provides similar capabilities to a try/catch block in sequential programming:
T result; try { result = resultOfThisSingle(); } catch (Throwable cause) { if (predicate.test(cause)) { // Note that nextFactory returning a error Single is like re-throwing (nextFactory shouldn't throw). result = nextFactory.apply(cause); } else { throw cause; } } return result;
- Parameters:
predicate
- returnstrue
if theThrowable
should be transformed vianextFactory
. Returnsfalse
to propagate the original error.nextFactory
- Returns the nextSingle
, when thisSingle
emits an error.- Returns:
- A
Single
that recovers from an error from thisSingle
by using anotherSingle
provided by the passednextFactory
. - See Also:
-
flatMap
Returns aSingle
that mirrors emissions from theSingle
returned bynext
. Any error emitted by thisSingle
is forwarded to the returnedSingle
.This method is similar to
map(Function)
but the result is asynchronous, and provides a data transformation in sequential programming similar to:T tResult = resultOfThisSingle(); R rResult = mapper.apply(tResult); // Asynchronous result is flatten into a value by this operator.
-
flatMapCompletable
Returns aCompletable
that mirrors emissions from theCompletable
returned bynext
. Any error emitted by thisSingle
is forwarded to the returnedCompletable
.This method is similar to
map(Function)
but the result is asynchronous with either complete/error status in sequential programming similar to:T tResult = resultOfThisSingle(); mapper.apply(tResult); // Asynchronous result is flatten into a error or completion by this operator.
- Parameters:
next
- Function to give the nextCompletable
.- Returns:
- New
Completable
that switches to theCompletable
returned bynext
after thisSingle
completes successfully.
-
flatMapPublisher
public final <R> Publisher<R> flatMapPublisher(Function<? super T, ? extends Publisher<? extends R>> next) Returns aPublisher
that mirrors emissions from thePublisher
returned bynext
. Any error emitted by thisSingle
is forwarded to the returnedPublisher
.This method is similar to
map(Function)
but the result is asynchronous, and provides a data transformation in sequential programming similar to:T tResult = resultOfThisSingle(); // Asynchronous result from mapper is flatten into collection of values. for (R rResult : mapper.apply(tResult)) { // process rResult }
-
whenOnSuccess
Invokes theonSuccess
Consumer
argument whenSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.The order in which
onSuccess
will be invoked relative toSingleSource.Subscriber.onSuccess(Object)
is undefined. If you need strict ordering seebeforeOnSuccess(Consumer)
andafterOnSuccess(Consumer)
.From a sequential programming point of view this method is roughly equivalent to the following:
T result = resultOfThisSingle(); // NOTE: The order of operations here is not guaranteed by this method! nextOperation(result); onSuccess.accept(result);
- Parameters:
onSuccess
- Invoked whenSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
whenOnError
Invokes theonError
Consumer
argument whenSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.The order in which
onError
will be invoked relative toSingleSource.Subscriber.onError(Throwable)
is undefined. If you need strict ordering seebeforeOnError(Consumer)
andafterOnError(Consumer)
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } catch (Throwable cause) { // NOTE: The order of operations here is not guaranteed by this method! nextOperation(cause); onError.accept(cause); }
- Parameters:
onError
- Invoked whenSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
whenFinally
Invokes thewhenFinally
Runnable
argument exactly once, when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
.The order in which
whenFinally
will be invoked relative to the above methods is undefined. If you need strict ordering seebeforeFinally(Runnable)
andafterFinally(Runnable)
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } finally { // NOTE: The order of operations here is not guaranteed by this method! nextOperation(); // Maybe notifying of cancellation, or termination doFinally.run(); }
- Parameters:
doFinally
- Invoked exactly once, when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
whenFinally
Invokes the corresponding method onwhenFinally
TerminalSignalConsumer
argument when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.The order in which
whenFinally
will be invoked relative to the above methods is undefined. If you need strict ordering seebeforeFinally(TerminalSignalConsumer)
andafterFinally(TerminalSignalConsumer)
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { // NOTE: The order of operations here is not guaranteed by this method! nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onError(t); return; } // NOTE: The order of operations here is not guaranteed by this method! nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onComplete();
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
whenFinally
Invokes the corresponding method onwhenFinally
SingleTerminalSignalConsumer
argument when any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.The order in which
whenFinally
will be invoked relative to the above methods is undefined. If you need strict ordering seebeforeFinally(SingleTerminalSignalConsumer)
andafterFinally(SingleTerminalSignalConsumer)
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { // NOTE: The order of operations here is not guaranteed by this method! nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onError(t); return; } // NOTE: The order of operations here is not guaranteed by this method! nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onSuccess(result);
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisSingleTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
whenCancel
Invokes theonCancel
Runnable
argument whenCancellable.cancel()
is called for Subscriptions of the returnedSingle
.The order in which
whenFinally
will be invoked relative toCancellable.cancel()
is undefined. If you need strict ordering seebeforeCancel(Runnable)
andafterCancel(Runnable)
.- Parameters:
onCancel
- Invoked whenCancellable.cancel()
is called for Subscriptions of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
timeout
Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination. The timer starts when the returnedSingle
is subscribed.In the event of timeout any
Cancellable
fromSingleSource.Subscriber.onSubscribe(Cancellable)
will becancelled
and the associatedSingleSource.Subscriber
will beterminated
.- Parameters:
duration
- The time duration which is allowed to elapse beforeSingleSource.Subscriber.onSuccess(Object)
.unit
- The units forduration
.- Returns:
- a new
Single
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses beforeSingleSource.Subscriber.onSuccess(Object)
. - See Also:
-
timeout
Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination. The timer starts when the returnedSingle
is subscribed.In the event of timeout any
Cancellable
fromSingleSource.Subscriber.onSubscribe(Cancellable)
will becancelled
and the associatedSingleSource.Subscriber
will beterminated
.- Parameters:
duration
- The time duration which is allowed to elapse beforeSingleSource.Subscriber.onSuccess(Object)
.unit
- The units forduration
.timeoutExecutor
- TheExecutor
to use for managing the timer notifications.- Returns:
- a new
Single
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses beforeSingleSource.Subscriber.onSuccess(Object)
. - See Also:
-
timeout
Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses between subscribe and termination. The timer starts when the returnedSingle
is subscribed.In the event of timeout any
Cancellable
fromSingleSource.Subscriber.onSubscribe(Cancellable)
will becancelled
and the associatedSingleSource.Subscriber
will beterminated
.SingleSource.Subscriber
will viaterminated
.- Parameters:
duration
- The time duration which is allowed to elapse beforeSingleSource.Subscriber.onSuccess(Object)
.- Returns:
- a new
Single
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses beforeSingleSource.Subscriber.onSuccess(Object)
. - See Also:
-
timeout
Creates a newSingle
that will mimic the signals of thisSingle
but will terminate with a with aTimeoutException
if timeduration
elapses between subscribe and termination. The timer starts when the returnedSingle
is subscribed.In the event of timeout any
Cancellable
fromSingleSource.Subscriber.onSubscribe(Cancellable)
will becancelled
and the associatedSingleSource.Subscriber
will beterminated
.- Parameters:
duration
- The time duration which is allowed to elapse beforeSingleSource.Subscriber.onSuccess(Object)
.timeoutExecutor
- TheExecutor
to use for managing the timer notifications.- Returns:
- a new
Single
that will mimic the signals of thisSingle
but will terminate with aTimeoutException
if timeduration
elapses beforeSingleSource.Subscriber.onSuccess(Object)
. - See Also:
-
concat
Returns aPublisher
that first emits the result of thisSingle
and then subscribes and emits result ofnext
Single
. Any error emitted by thisSingle
ornext
Single
is forwarded to the returnedPublisher
.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
Pair<T, T> p = new Pair<>(); p.first = resultOfThisSingle(); p.second = nextSingle(); return p;
-
concat
Returns aSingle
that emits the result of thisSingle
afternext
Completable
terminates successfully.next
Completable
will only be subscribed to after thisSingle
terminates successfully. Any error emitted by thisSingle
ornext
Completable
is forwarded to the returnedSingle
.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
T result = resultOfThisSingle(); nextCompletable(); // Note this either completes successfully, or throws an error. return result;
- Parameters:
next
-Completable
to concat.- Returns:
- New
Single
that emits the result of thisSingle
afternext
Completable
terminates successfully.
-
concat
Returns aPublisher
that first emits the result of thisSingle
and then subscribes and emits all elements fromnext
Publisher
. Any error emitted by thisSingle
ornext
Publisher
is forwarded to the returnedPublisher
.Note: this operator triggers subscribe to the
next
Publisher
as soon asthis
Single
completes successfully.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = new ...; results.add(resultOfThisSingle()); results.addAll(nextStream()); return results;
-
concat
Deprecated.Useconcat(Publisher)
orconcatDeferSubscribe(Publisher)
instead.Returns aPublisher
that first emits the result of thisSingle
and then subscribes and emits all elements fromnext
Publisher
. Any error emitted by thisSingle
ornext
Publisher
is forwarded to the returnedPublisher
.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = new ...; results.add(resultOfThisSingle()); results.addAll(nextStream()); return results;
- Parameters:
next
-Publisher
to concat.deferSubscribe
- iftrue
subscribe to thenext
Publisher
will be deferred until demand is received. Otherwise, it subscribes to thenext
Publisher
as soon asthis
Single
completes successfully. Choosing the deferred (true
) behavior is important if thenext
Publisher
does not or might not support multiple subscribers (non-replayable). Choosing the immediate subscribe (false
) behavior may have better performance and may be a preferable choice for replayablePublisher
(s) or when eager subscribe is beneficial.- Returns:
- New
Publisher
that first emits the result of thisSingle
and then subscribes and emits all elements fromnext
Publisher
.
-
concatDeferSubscribe
This method is likeconcat(Publisher)
subscribe to thenext
Publisher
will be deferred until demand is received (at least 2 items requested).Choosing the deferred behavior is important if the
next
Publisher
does not or might not support multiple subscribers (non-replayable). Choosing the immediate subscribe (concat(Publisher)
) behavior may have better performance and may be a preferable choice for replayablePublisher
(s) or when eager subscribe is beneficial.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = new ...; results.add(resultOfThisSingle()); results.addAll(nextStream()); return results;
-
concatPropagateCancel
This method is likeconcat(Publisher)
exceptnext
will be subscribed to and cancelled if thisPublisher
is cancelled or terminates withSingleSource.Subscriber.onError(Throwable)
.This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = new ...; results.add(resultOfThisSingle()); results.addAll(nextStream()); return results;
- Parameters:
next
-Publisher
to concat. Will be subscribed to and cancelled if thisPublisher
is cancelled or terminates withSingleSource.Subscriber.onError(Throwable)
.- Returns:
- New
Publisher
that first emits the result of thisSingle
and then subscribes and emits all elements fromnext
Publisher
. - See Also:
-
zipWith
public final <T2,R> Single<R> zipWith(Single<? extends T2> other, BiFunction<? super T, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T> f1 = ...; // this CompletableFuture<T2> other = ...; CompletableFuture.allOf(f1, other).get(); // wait for all futures to complete return zipper.apply(f1.get(), other.get());
- Type Parameters:
T2
- The type ofother
.R
- The result type of the zipper.- Parameters:
other
- The otherSingle
to zip with.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
. - See Also:
-
zipWithDelayError
public final <T2,R> Single<R> zipWithDelayError(Single<? extends T2> other, BiFunction<? super T, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
. If any of theSingle
s terminate with an error, the returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated, and then terminate with the first error.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T> f1 = ...; // this CompletableFuture<T2> other = ...; CompletableFuture.allOf(f1, other).get(); // wait for all futures to complete return zipper.apply(f1.get(), other.get());
- Type Parameters:
T2
- The type ofother
.R
- The result type of the zipper.- Parameters:
other
- The otherSingle
to zip with.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperBiFunction
to items emitted bythis
andother
. - See Also:
-
retry
Re-subscribes to thisSingle
if an error is emitted and the passedBiIntPredicate
returnstrue
.This method may result in a
This method provides a means to retry an operation under certain failure conditions and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:retryWhen((i, cause) -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
public T execute() { return execute(0); } private T execute(int attempts) { try { return resultOfThisSingle(); } catch (Throwable cause) { if (shouldRetry.apply(attempts + 1, cause)) { return execute(attempts + 1); } else { throw cause; } } }
- Parameters:
shouldRetry
-BiIntPredicate
that given the retry count and the most recentThrowable
emitted from thisSingle
determines if the operation should be retried.- Returns:
- A
Single
that emits the result from thisSingle
or re-subscribes if an error is emitted and if the passedBiIntPredicate
returnedtrue
. - See Also:
-
retryWhen
Re-subscribes to thisSingle
if an error is emitted and theCompletable
returned by the suppliedBiIntFunction
completes successfully. If the returnedCompletable
emits an error, the returnedSingle
terminates with that error.This method may result in a
This method provides a means to retry an operation under certain failure conditions in an asynchronous fashion and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:retryWhen((i, cause) -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
public T execute() { return execute(0); } private T execute(int attempts) { try { return resultOfThisSingle(); } catch (Throwable cause) { try { shouldRetry.apply(attempts + 1, cause); // Either throws or completes normally execute(attempts + 1); } catch (Throwable ignored) { throw cause; } } }
- Parameters:
retryWhen
-BiIntFunction
that given the retry count and the most recentThrowable
emitted from thisSingle
returns aCompletable
. If thisCompletable
emits an error, that error is emitted from the returnedSingle
, otherwise, originalSingle
is re-subscribed when thisCompletable
completes.- Returns:
- A
Single
that emits the result from thisSingle
or re-subscribes if an error is emitted andCompletable
returned byBiIntFunction
completes successfully. - See Also:
-
repeat
Re-subscribes to thisSingle
when it completes and the passedIntPredicate
returnstrue
.This method may result in a
This method provides a means to repeat an operation multiple times and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:repeatWhen(i -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
List<T> results = new ...; int i = 0; do { results.add(resultOfThisSingle()); } while (shouldRepeat.test(++i)); return results;
- Parameters:
shouldRepeat
-IntPredicate
that given the repetition count returnstrue
if the operation should be repeated.- Returns:
- A
Publisher
that emits all items from thisSingle
and from all re-subscriptions whenever the operation is repeated. - See Also:
-
repeat
Re-subscribes to thisSingle
when it completes and the passedIntPredicate
returnstrue
.This method may result in a
This method provides a means to repeat an operation multiple times and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:repeatWhen(i -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
List<T> results = new ...; int i = 0; T result; do { result = resultOfThisSingle(); results.add(result); } while (shouldRepeat.test(++i, result)); return results;
- Parameters:
shouldRepeat
-BiIntPredicate
that given the repetition count and value from the current iteration returnstrue
if the operation should be repeated.- Returns:
- A
Publisher
that emits all items from thisSingle
and from all re-subscriptions whenever the operation is repeated. - See Also:
-
repeatWhen
Re-subscribes to thisSingle
when it completes and theCompletable
returned by the suppliedIntFunction
completes successfully. If the returnedCompletable
emits an error, the returnedSingle
emits an error.This method may result in a
This method provides a means to repeat an operation multiple times when in an asynchronous fashion and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:repeatWhen(i -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
List<T> results = new ...; int i = 0; while (true) { results.add(resultOfThisSingle()); try { repeatWhen.apply(++i); // Either throws or completes normally } catch (Throwable cause) { break; } } return results;
- Parameters:
repeatWhen
-IntFunction
that given the repetition count returns aCompletable
. If thisCompletable
emits an error repeat is terminated, otherwise, originalSingle
is re-subscribed when thisCompletable
completes.- Returns:
- A
Publisher
that emits all items from thisSingle
and from all re-subscriptions whenever the operation is repeated. - See Also:
-
repeatWhen
Re-subscribes to thisSingle
when it completes and theCompletable
returned by the suppliedBiIntFunction
completes successfully.This method may result in a
This method provides a means to repeat an operation multiple times when in an asynchronous fashion and in sequential programming is similar to:StackOverflowError
if too many consecutive calls are made. This can be avoided by trampolining the call stack onto anExecutor
. For example:repeatWhen(i -> i % 10 == 0 ? executor.submit(() -> { }) : Completable.completed())
List<T> results = new ...; int i = 0; while (true) { T result = resultOfThisSingle(); try { repeatWhen.apply(++i, result); // Either throws or completes normally } catch (Throwable cause) { break; } } return results;
- Parameters:
repeatWhen
-BiIntFunction
that given the repetition count and value from the current iteration returns aCompletable
. If thisCompletable
emits an error repeat is terminated, otherwise, originalSingle
is re-subscribed when thisCompletable
completes.- Returns:
- A
Publisher
that emits all items from thisSingle
and from all re-subscriptions whenever the operation is repeated. - See Also:
-
cache
Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
.In sequential programming this is similar to the following:
T result = resultOfThisSingle(); List<T> multiResults = ...; // simulating multiple Subscribers for (int i = 0; i < expectedSubscribers; ++i) { multiResults.add(result); } return multiResults;
- Returns:
- a
Single
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
. - See Also:
-
cache
Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
.In sequential programming this is similar to the following:
T result = resultOfThisSingle(); List<T> multiResults = ...; // simulating multiple Subscribers for (int i = 0; i < expectedSubscribers; ++i) { multiResults.add(result); } return multiResults;
- Parameters:
minSubscribers
- The upstream subscribe operation will not happen until after this manySingleSource.Subscriber
subscribe to the return value.- Returns:
- a
Single
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
. - See Also:
-
cache
Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
.In sequential programming this is similar to the following:
T result = resultOfThisSingle(); List<T> multiResults = ...; // simulating multiple Subscribers for (int i = 0; i < expectedSubscribers; ++i) { multiResults.add(result); } return multiResults;
- Parameters:
minSubscribers
- The upstream subscribe operation will not happen until after this manySingleSource.Subscriber
subscribe to the return value.cancelUpstream
-true
if upstream should becancelled
when all downstreamSingleSource.Subscriber
s cancel.false
means that cancel will not be propagated upstream even if all downstreamSingleSource.Subscriber
s cancel, and the upstream Subscription will stay valid until termination.- Returns:
- a
Single
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
. - See Also:
-
cache
public final Single<T> cache(int minSubscribers, boolean cancelUpstream, BiFunction<T, Throwable, Completable> terminalResubscribe) Create aSingle
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
.In sequential programming this is similar to the following:
T result = resultOfThisSingle(); List<T> multiResults = ...; // simulating multiple Subscribers for (int i = 0; i < expectedSubscribers; ++i) { multiResults.add(result); } return multiResults;
- Parameters:
minSubscribers
- The upstream subscribe operation will not happen until after this manySingleSource.Subscriber
subscribe to the return value.cancelUpstream
-true
if upstream should becancelled
when all downstreamSingleSource.Subscriber
s cancel.false
means that cancel will not be propagated upstream even if all downstreamSingleSource.Subscriber
s cancel, and the upstream Subscription will stay valid until termination.terminalResubscribe
- ABiFunction
that is invoked when a terminal signal arrives from upstream, and returns aCompletable
whose termination resets the state of the returnedSingle
and allows for downstream resubscribing.- Returns:
- a
Single
that subscribes a single time upstream but allows for multiple downstreamSingleSource.Subscriber
s. The terminal signal will be cached and delivered to each downstreamSingleSource.Subscriber
. - See Also:
-
beforeOnSubscribe
Invokes theonSubscribe
Consumer
argument beforeSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.- Parameters:
onSubscribe
- Invoked beforeSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
beforeOnSuccess
Invokes theonSuccess
Consumer
argument beforeSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result = resultOfThisSingle(); onSuccess.accept(result); nextOperation(result);
- Parameters:
onSuccess
- Invoked beforeSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
beforeOnError
Invokes theonError
Consumer
argument beforeSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } catch (Throwable cause) { onError.accept(cause); nextOperation(cause); }
- Parameters:
onError
- Invoked beforeSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
beforeCancel
Invokes theonCancel
Runnable
argument beforeCancellable.cancel()
is called for Subscriptions of the returnedSingle
.- Parameters:
onCancel
- Invoked beforeCancellable.cancel()
is called for Subscriptions of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
beforeFinally
Invokes thewhenFinally
Runnable
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } finally { doFinally.run(); nextOperation(); // Maybe notifying of cancellation, or termination }
- Parameters:
doFinally
- Invoked before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
beforeFinally
Invokes the corresponding method onbeforeFinally
TerminalSignalConsumer
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { doFinally.onError(t); nextOperation(); // Maybe notifying of cancellation, or termination return; } doFinally.onComplete(); nextOperation(); // Maybe notifying of cancellation, or termination
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
beforeFinally
Invokes the corresponding method onbeforeFinally
SingleTerminalSignalConsumer
argument before any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { doFinally.onError(t); nextOperation(); // Maybe notifying of cancellation, or termination return; } doFinally.onSuccess(result); nextOperation(); // Maybe notifying of cancellation, or termination
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisSingleTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
beforeSubscriber
public final Single<T> beforeSubscriber(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) on each call to subscribe and invokes all theSingleSource.Subscriber
methods before theSingleSource.Subscriber
s of the returnedSingle
.- Parameters:
subscriberSupplier
- Creates a newSingleSource.Subscriber
on each call to subscribe and invokes all theSingleSource.Subscriber
methods before theSingleSource.Subscriber
s of the returnedSingle
.SingleSource.Subscriber
methods MUST NOT throw.- Returns:
- The new
Single
.
-
afterOnSubscribe
Invokes theonSubscribe
Consumer
argument afterSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.- Parameters:
onSubscribe
- Invoked afterSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
whenOnSubscribe
Invokes theonSubscribe
Consumer
argument whenSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
.The order in which
onSubscribe
will be invoked relative toSingleSource.Subscriber.onSubscribe(Cancellable)
is undefined. If you need strict ordering seebeforeOnSubscribe(Consumer)
andafterOnSubscribe(Consumer)
.- Parameters:
onSubscribe
- Invoked whenSingleSource.Subscriber.onSubscribe(Cancellable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
afterOnSuccess
Invokes theonSuccess
Consumer
argument afterSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result = resultOfThisSingle(); nextOperation(result); onSuccess.accept(result);
- Parameters:
onSuccess
- Invoked afterSingleSource.Subscriber.onSuccess(Object)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
afterOnError
Invokes theonError
Consumer
argument afterSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } catch (Throwable cause) { nextOperation(cause); onError.accept(cause); }
- Parameters:
onError
- Invoked afterSingleSource.Subscriber.onError(Throwable)
is called forSingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
afterCancel
Invokes theonCancel
Runnable
argument afterCancellable.cancel()
is called for Subscriptions of the returnedSingle
.- Parameters:
onCancel
- Invoked afterCancellable.cancel()
is called for Subscriptions of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
.
-
afterFinally
Invokes thewhenFinally
Runnable
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
try { T result = resultOfThisSingle(); } finally { nextOperation(); // Maybe notifying of cancellation, or termination doFinally.run(); }
- Parameters:
doFinally
- Invoked after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
Cancellable.cancel()
SingleSource.Subscriber
s of the returnedSingle
. MUST NOT throw.- Returns:
- The new
Single
. - See Also:
-
afterFinally
Invokes the corresponding method onafterFinally
TerminalSignalConsumer
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesTerminalSignalConsumer.onComplete()
SingleSource.Subscriber.onError(Throwable)
- invokesTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onError(t); return; } nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onComplete();
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
afterFinally
Invokes the corresponding method onafterFinally
SingleTerminalSignalConsumer
argument after any of the following terminal methods are called:SingleSource.Subscriber.onSuccess(Object)
- invokesSingleTerminalSignalConsumer.onSuccess(Object)
SingleSource.Subscriber.onError(Throwable)
- invokesSingleTerminalSignalConsumer.onError(Throwable)
Cancellable.cancel()
- invokesSingleTerminalSignalConsumer.cancel()
SingleSource.Subscriber
s of the returnedSingle
.From a sequential programming point of view this method is roughly equivalent to the following:
T result; try { result = resultOfThisSingle(); } catch(Throwable t) { nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onError(t); return; } nextOperation(); // Maybe notifying of cancellation, or termination doFinally.onSuccess(result);
- Parameters:
doFinally
- For each subscribe of the returnedSingle
, at most one method of thisSingleTerminalSignalConsumer
will be invoked.- Returns:
- The new
Single
. - See Also:
-
afterSubscriber
public final Single<T> afterSubscriber(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) on each call to subscribe and invokes all theSingleSource.Subscriber
methods after theSingleSource.Subscriber
s of the returnedSingle
.- Parameters:
subscriberSupplier
- Creates a newSingleSource.Subscriber
on each call to subscribe and invokes all theSingleSource.Subscriber
methods after theSingleSource.Subscriber
s of the returnedSingle
.SingleSource.Subscriber
methods MUST NOT throw.- Returns:
- The new
Single
.
-
whenSubscriber
public final Single<T> whenSubscriber(Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier) Creates a newSingleSource.Subscriber
(via thesubscriberSupplier
argument) for each new subscribe and invokes methods on thatSingleSource.Subscriber
when the corresponding methods are called forSingleSource.Subscriber
s of the returnedSingle
.- Parameters:
subscriberSupplier
- Creates a newSingleSource.Subscriber
for each new subscribe and invokes methods on thatSingleSource.Subscriber
when the corresponding methods are called forSingleSource.Subscriber
s of the returnedSingle
.SingleSource.Subscriber
methods MUST NOT throw.- Returns:
- The new
Single
.
-
publishOn
Creates a newSingle
that will use the passedExecutor
to invoke allSingleSource.Subscriber
methods. This method does not override precedingExecutor
s, if any, specified forthis
Single
. Only subsequent operations, if any, added in this execution chain will use thisExecutor
.Note: unlike
publishOn(io.servicetalk.concurrent.Executor, BooleanSupplier)
, current operator always enforces offloading to the passedExecutor
.- Parameters:
executor
-Executor
to use.- Returns:
- A new
Single
that will use the passedExecutor
to invoke allSingleSource.Subscriber
methods. - See Also:
-
publishOn
Creates a newSingle
that may use the passedExecutor
to invoke allSingleSource.Subscriber
methods. This method does not override precedingExecutor
s, if any, specified forthis
Single
. Only subsequent operations, if any, added in this execution chain will use thisExecutor
.Note: unlike
publishOn(io.servicetalk.concurrent.Executor)
, current operator may skip offloading to the passedExecutor
, depending on the result of theBooleanSupplier
hint.- Parameters:
executor
-Executor
to use.shouldOffload
- Provides a hint whether offloading to the executor can be omitted or not. Offloading may still occur even iffalse
is returned in order to preserve signal ordering.- Returns:
- A new
Single
that may use the passedExecutor
to invoke allSingleSource.Subscriber
methods. - See Also:
-
subscribeOn
Creates a newSingle
that will use the passedExecutor
to invoke the following methods:- All
Cancellable
methods. - The
handleSubscribe(SingleSource.Subscriber)
method.
Executor
s, if any, specified forthis
Single
. Only subsequent operations, if any, added in this execution chain will use thisExecutor
.Note: unlike
subscribeOn(io.servicetalk.concurrent.Executor, BooleanSupplier)
, current operator always enforces offloading to the passedExecutor
.- Parameters:
executor
-Executor
to use.- Returns:
- A new
Single
that will use the passedExecutor
to invoke all methods ofCancellable
andhandleSubscribe(SingleSource.Subscriber)
. - See Also:
- All
-
subscribeOn
Creates a newSingle
that may use the passedExecutor
to invoke the following methods:- All
Cancellable
methods. - The
handleSubscribe(SingleSource.Subscriber)
method.
Executor
s, if any, specified forthis
Single
. Only subsequent operations, if any, added in this execution chain will use thisExecutor
.Note: unlike
subscribeOn(io.servicetalk.concurrent.Executor)
, current operator may skip offloading to the passedExecutor
, depending on the result of theBooleanSupplier
hint.- Parameters:
executor
-Executor
to use.shouldOffload
- Provides a hint whether offloading to the executor can be omitted or not. Offloading may still occur even iffalse
is returned in order to preserve signal ordering.- Returns:
- A new
Single
that may use the passedExecutor
to invoke all methods ofCancellable
andhandleSubscribe(SingleSource.Subscriber)
. - See Also:
- All
-
setContextOnSubscribe
Specify theContextMap
to use forAsyncContext
when the returnedSingle
is subscribed to.This operator only impacts behavior if the returned
Single
is subscribed directly after this operator, that means this must be the "last operator" in the chain for this to have an impact.- Parameters:
context
- TheContextMap
to use forAsyncContext
when subscribed.- Returns:
- A
Single
that will use theContextMap
forAsyncContext
when subscribed.
-
liftSync
This method requires advanced knowledge of building operators. Before using this method please attempt to compose existing operator(s) to satisfy your use case.Returns a
Single
which will wrap theSingleSource.Subscriber
using the providedoperator
argument before subscribing to thisSingle
.
TheSingle<X> pub = ...; pub.map(..) // A .liftSync(original -> modified) .afterFinally(..) // B
original -> modified
"operator" MUST be "synchronous" in that it does not interact with the originalSingleSource.Subscriber
from outside the modifiedSingleSource.Subscriber
orCancellable
threads. That is to say this operator will not impact theExecutor
constraints already in place between A and B above. If you need asynchronous behavior, or are unsure, seeliftAsync(SingleOperator)
.- Type Parameters:
R
- Type of the items emitted by the returnedSingle
.- Parameters:
operator
- The custom operator logic. The input is the "original"SingleSource.Subscriber
to thisSingle
and the return is the "modified"SingleSource.Subscriber
that provides custom operator business logic.- Returns:
- a
Single
which when subscribed, theoperator
argument will be used to wrap theSingleSource.Subscriber
before subscribing to thisSingle
. - See Also:
-
liftAsync
This method requires advanced knowledge of building operators. Before using this method please attempt to compose existing operator(s) to satisfy your use case.Returns a
Single
which will wrap theSingleSource.Subscriber
using the providedoperator
argument before subscribing to thisSingle
.
ThePublisher<X> pub = ...; pub.map(..) // Aw .liftAsync(original -> modified) .afterFinally(..) // B
original -> modified
"operator" MAY be "asynchronous" in that it may interact with the originalSingleSource.Subscriber
from outside the modifiedSingleSource.Subscriber
orCancellable
threads. More specifically:- all of the
SingleSource.Subscriber
invocations going "downstream" (i.e. from A to B above) MAY be offloaded via anExecutor
- all of the
Cancellable
invocations going "upstream" (i.e. from B to A above) MAY be offloaded via anExecutor
- Type Parameters:
R
- Type of the items emitted by the returnedSingle
.- Parameters:
operator
- The custom operator logic. The input is the "original"SingleSource.Subscriber
to thisSingle
and the return is the "modified"SingleSource.Subscriber
that provides custom operator business logic.- Returns:
- a
Single
which when subscribed, theoperator
argument will be used to wrap theSingleSource.Subscriber
before subscribing to thisSingle
. - See Also:
- all of the
-
ambWith
Creates a newSingle
that terminates with the result (either success or error) of either thisSingle
or the passedother
Single
, whichever terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. After the first source terminates, only the non-terminated source will be cancelled.From a sequential programming point of view this method is roughly equivalent to the following:
for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator will pick the first result from either of the futures. return ft.get(); }
- Parameters:
other
-Single
to subscribe to and race with thisSingle
to propagate to the return value.- Returns:
- A new
Single
that terminates with the result (either success or error) of either thisSingle
or the passedother
Single
, whichever terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. - See Also:
-
toPublisher
Converts thisSingle
to aPublisher
.- Returns:
- A
Publisher
that emits at most a single item which is emitted by thisSingle
.
-
toCompletable
Ignores the result of thisSingle
and forwards the termination signal to the returnedCompletable
.- Returns:
- A
Completable
that mirrors the terminal signal from thisSingle
.
-
ignoreElement
Ignores the result of thisSingle
and forwards the termination signal to the returnedCompletable
.- Returns:
- A
Completable
that mirrors the terminal signal from thisSingle
.
-
toCompletionStage
Convert thisSingle
to aCompletionStage
.- Returns:
- A
CompletionStage
that mirrors the terminal signal from thisSingle
.
-
toFuture
-
subscribeInternal
A internal subscribe method similar toSingleSource.subscribe(Subscriber)
which can be used by different implementations to subscribe.- Parameters:
subscriber
-SingleSource.Subscriber
to subscribe for the result.
-
subscribe
Subscribe to thisSingle
, emit the result to the passedConsumer
and log anySingleSource.Subscriber.onError(Throwable)
at debug level.- Parameters:
resultConsumer
-Consumer
to accept the result of thisSingle
if it succeeds.- Returns:
Cancellable
used to invokeCancellable.cancel()
on the parameter ofSingleSource.Subscriber.onSubscribe(Cancellable)
for thisSingle
.
-
subscribe
public final Cancellable subscribe(Consumer<? super T> resultConsumer, Consumer<? super Throwable> errorConsumer) - Parameters:
resultConsumer
-Consumer
to accept the result of thisSingle
if it succeeds.errorConsumer
-Consumer
to accept the error of thisSingle
if it fails.- Returns:
Cancellable
used to invokeCancellable.cancel()
on the parameter ofSingleSource.Subscriber.onSubscribe(Cancellable)
for thisSingle
.
-
handleSubscribe
Handles a subscriber to thisSingle
.- Parameters:
subscriber
- the subscriber.
-
succeeded
Creates a realizedSingle
which always completes successfully with the providedvalue
. -
fromCallable
Creates aSingle
which when subscribed will invokeCallable.call()
on the passedCallable
and emit the value returned by that invocation from the returnedSingle
. Any error emitted by theCallable
will terminate the returnedSingle
with the same error.Blocking inside
Callable.call()
will in turn block the subscribe call to the returnedSingle
. If this behavior is undesirable then the returnedSingle
should be offloaded usingsubscribeOn(io.servicetalk.concurrent.Executor)
which offloads the subscribe call. -
fromSupplier
Creates aSingle
which when subscribed will invokeSupplier.get()
on the passedSupplier
and emit the value returned by that invocation from the returnedSingle
. Any error emitted by theSupplier
will terminate the returnedSingle
with the same error.Blocking inside
Supplier.get()
will in turn block the subscribe call to the returnedSingle
. If this behavior is undesirable then the returnedSingle
should be offloaded usingsubscribeOn(io.servicetalk.concurrent.Executor)
which offloads the subscribe call. -
failed
Creates a realizedSingle
which always completes with the provided errorcause
. -
never
Creates aSingle
that never terminates. -
defer
Defer creation of aSingle
till it is subscribed to.- Type Parameters:
T
- Type of theSingle
.- Parameters:
singleSupplier
-Supplier
to create a newSingle
every time the returnedSingle
is subscribed.- Returns:
- A new
Single
that creates a newSingle
usingsingleSupplier
every time it is subscribed and forwards all items and terminal events from the newly createdSingle
to itsSingleSource.Subscriber
.
-
fromFuture
Convert from aFuture
to aSingle
viaFuture.get()
.Note that because
Future
only presents blocking APIs to extract the result, so the process of getting the results will block. The caller of subscribe is responsible for offloading if necessary, and also offloading ifCancellable.cancel()
will be called and this operation may block.To apply a timeout see
timeout(long, TimeUnit)
and related methods. -
collectUnordered
public static <T> Single<Collection<T>> collectUnordered(Iterable<? extends Single<? extends T>> singles) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.This will actively subscribe to a limited number of
Single
s concurrently, in order to alter the defaults,collectUnordered(Iterable, int)
should be used.If any of the
Single
s terminate with an error, returnedSingle
will immediately terminate with that error. In such a case, any in progressSingle
s will be cancelled. In order to delay error termination usecollectUnorderedDelayError(Iterable)
.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. result.add(ft.get()); } return result;
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Iterable
ofSingle
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnordered
@SafeVarargs public static <T> Single<Collection<T>> collectUnordered(Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.This will actively subscribe to a limited number of
Single
s concurrently, in order to alter the defaults,collectUnordered(int, Single[])
should be used.If any of the
Single
s terminate with an error, returnedSingle
will immediately terminate with that error. In such a case, any in progressSingle
s will be cancelled. In order to delay error termination usecollectUnorderedDelayError(Single[])
.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. result.add(ft.get()); } return result;
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnordered
public static <T> Single<Collection<T>> collectUnordered(Iterable<? extends Single<? extends T>> singles, int maxConcurrency) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.If any of the
Single
s terminate with an error, returnedSingle
will immediately terminate with that error. In such a case, any in progressSingle
s will be cancelled. In order to delay error termination usecollectUnorderedDelayError(Iterable, int)
.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. result.add(ft.get()); } return result;
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Iterable
ofSingle
s, results of which are to be collected.maxConcurrency
- Maximum number ofSingle
s that will be active at any point in time.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnordered
@SafeVarargs public static <T> Single<Collection<T>> collectUnordered(int maxConcurrency, Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.If any of the
Single
s terminate with an error, returnedSingle
will immediately terminate with that error. In such a case, any in progressSingle
s will be cancelled. In order to delay error termination usecollectUnorderedDelayError(int, Single[])
.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. result.add(ft.get()); } return result;
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
maxConcurrency
- Maximum number ofSingle
s that will be active at any point in time.singles
-Single
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnorderedDelayError
public static <T> Single<Collection<T>> collectUnorderedDelayError(Iterable<? extends Single<? extends T>> singles) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.This will actively subscribe to a limited number of
Single
s concurrently, in order to alter the defaults,collectUnorderedDelayError(Iterable, int)
.If any of the
Single
s terminate with an error, returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated. If it is expected for the returnedSingle
to terminate on the first failingSingle
,collectUnordered(Iterable)
should be used.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe List<Throwable> errors = ...; // assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. try { result.add(ft.get()); } catch(Throwable t) { errors.add(t); } } if (errors.isEmpty()) { return rResults; } createAndThrowACompositeException(errors);
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Iterable
ofSingle
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnorderedDelayError
@SafeVarargs public static <T> Single<Collection<T>> collectUnorderedDelayError(Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.This will actively subscribe to a limited number of
Single
s concurrently, in order to alter the defaults,collectUnordered(int, Single[])
.If any of the
Single
s terminate with an error, returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated. If it is expected for the returnedSingle
to terminate on the first failingSingle
,collectUnordered(Single[])
should be used.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe List<Throwable> errors = ...; // assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. try { result.add(ft.get()); } catch(Throwable t) { errors.add(t); } } if (errors.isEmpty()) { return rResults; } createAndThrowACompositeException(errors);
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnorderedDelayError
public static <T> Single<Collection<T>> collectUnorderedDelayError(Iterable<? extends Single<? extends T>> singles, int maxConcurrency) Asynchronously collects results of individualSingle
s returned by the passedIterable
into a singleCollection
.If any of the
Single
s terminate with an error, returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated. If it is expected for the returnedSingle
to terminate on the first failingSingle
,collectUnordered(Iterable, int)
should be used.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe List<Throwable> errors = ...; // assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. try { result.add(ft.get()); } catch(Throwable t) { errors.add(t); } } if (errors.isEmpty()) { return rResults; } createAndThrowACompositeException(errors);
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Iterable
ofSingle
s, results of which are to be collected.maxConcurrency
- Maximum number ofSingle
s that will be active at any point in time.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
collectUnorderedDelayError
@SafeVarargs public static <T> Single<Collection<T>> collectUnorderedDelayError(int maxConcurrency, Single<? extends T>... singles) Asynchronously collects results of the passedSingle
s into a singleCollection
.If any of the
Single
s terminate with an error, returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated. If it is expected for the returnedSingle
to terminate on the first failingSingle
,collectUnordered(Iterable, int)
should be used.From a sequential programming point of view this method is roughly equivalent to the following:
List<T> result = ...;// assume this is thread safe List<Throwable> errors = ...; // assume this is thread safe for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator does not provide any ordering guarantees for the results. try { result.add(ft.get()); } catch(Throwable t) { errors.add(t); } } if (errors.isEmpty()) { return rResults; } createAndThrowACompositeException(errors);
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
maxConcurrency
- Maximum number ofSingle
s that will be active at any point in time.singles
-Single
s, results of which are to be collected.- Returns:
- A
Single
producing aCollection
of all values produced by the individualSingle
s. There is no guarantee of the order of the values in the producedCollection
as compared to the order ofSingle
s passed to this method.
-
fromStage
Convert from aCompletionStage
to aSingle
.A best effort is made to propagate
Cancellable.cancel()
to theCompletionStage
. Cancellation forCompletionStage
implementations will result in exceptional completion and invoke user callbacks. If there is any blocking code involved in the cancellation process (including invoking user callbacks) you should investigate if using anExecutor
is appropriate.- Type Parameters:
T
- The data type theCompletionStage
provides when complete.- Parameters:
stage
- TheCompletionStage
to convert.- Returns:
- A
Single
that derives results fromCompletionStage
.
-
amb
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. After the first source terminates, only the non-terminated sources will be cancelled.From a sequential programming point of view this method is roughly equivalent to the following:
for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator will pick the first result from any of the futures. return ft.get(); }
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s to subscribe to and race to propagate to the return value.- Returns:
- A new
Single
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. - See Also:
-
amb
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. After the first source terminates, only the non-terminated sources will be cancelled.From a sequential programming point of view this method is roughly equivalent to the following:
for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator will pick the first result from any of the futures. return ft.get(); }
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s to subscribe to and race to propagate to the return value.- Returns:
- A new
Single
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. - See Also:
-
anyOf
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. After the first source terminates, only the non-terminated sources will be cancelled.From a sequential programming point of view this method is roughly equivalent to the following:
for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator will pick the first result from any of the futures. return ft.get(); }
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s to subscribe to and race to propagate to the return value.- Returns:
- A new
Single
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. - See Also:
-
anyOf
Creates a newSingle
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. Therefore, the result is said to be ambiguous relative to which source it originated from. After the first source terminates, only the non-terminated sources will be cancelled.From a sequential programming point of view this method is roughly equivalent to the following:
for (Future<T> ft: futures) { // Provided Futures (analogous to the Singles here) // This is an approximation, this operator will pick the first result from any of the futures. return ft.get(); }
- Type Parameters:
T
- Type of the result of the individualSingle
s- Parameters:
singles
-Single
s to subscribe to and race to propagate to the return value.- Returns:
- A new
Single
that terminates with the result (either success or error) of whichever amongst the passedsingles
that terminates first. - See Also:
-
zip
public static <T1,T2, Single<R> zipR> (Single<? extends T1> s1, Single<? extends T2> s2, BiFunction<? super T1, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture.allOf(f1, f2).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
. - See Also:
-
zipDelayError
public static <T1,T2, Single<R> zipDelayErrorR> (Single<? extends T1> s1, Single<? extends T2> s2, BiFunction<? super T1, ? super T2, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
. If any of theSingle
s terminate with an error, the returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated, and then terminate with the first error.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture.allOf(f1, f2).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperBiFunction
to items emitted bys1
ands2
. - See Also:
-
zip
public static <T1,T2, Single<R> zipT3, R> (Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Function3<? super T1, ? super T2, ? super T3, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperFunction3
to items emitted bys1
,s2
, ands3
.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture<T3> f3 = ...; // s3 CompletableFuture.allOf(f1, f2, f3).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get(), f3.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.T3
- The type for the thirdSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.s3
- The thirdSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperFunction3
to items emitted bys1
,s2
, ands3
. - See Also:
-
zipDelayError
public static <T1,T2, Single<R> zipDelayErrorT3, R> (Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Function3<? super T1, ? super T2, ? super T3, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperFunction3
to items emitted bys1
,s2
, ands3
. If any of theSingle
s terminate with an error, the returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated, and then terminate with the first error.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture<T3> f3 = ...; // s3 CompletableFuture.allOf(f1, f2, f3).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get(), f3.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.T3
- The type for the thirdSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.s3
- The thirdSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperFunction3
to items emitted bys1
,s2
, ands3
. - See Also:
-
zip
public static <T1,T2, Single<R> zipT3, T4, R> (Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Single<? extends T4> s4, Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperFunction4
to items emitted bys1
,s2
,s3
, ands4
.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture<T3> f3 = ...; // s3 CompletableFuture<T4> f4 = ...; // s3 CompletableFuture.allOf(f1, f2, f3, f4).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get(), f3.get(), f4.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.T3
- The type for the thirdSingle
.T4
- The type for the fourthSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.s3
- The thirdSingle
to zip.s4
- The fourthSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperFunction4
to items emitted bys1
,s2
,s3
, ands4
. - See Also:
-
zipDelayError
public static <T1,T2, Single<R> zipDelayErrorT3, T4, R> (Single<? extends T1> s1, Single<? extends T2> s2, Single<? extends T3> s3, Single<? extends T4> s4, Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) Create a newSingle
that emits the results of a specified zipperFunction4
to items emitted bys1
,s2
,s3
, ands4
. If any of theSingle
s terminate with an error, the returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated, and then terminate with the first error.From a sequential programming point of view this method is roughly equivalent to the following:
CompletableFuture<T1> f1 = ...; // s1 CompletableFuture<T2> f2 = ...; // s2 CompletableFuture<T3> f3 = ...; // s3 CompletableFuture<T4> f4 = ...; // s3 CompletableFuture.allOf(f1, f2, f3, f4).get(); // wait for all futures to complete return zipper.apply(f1.get(), f2.get(), f3.get(), f4.get());
- Type Parameters:
T1
- The type for the firstSingle
.T2
- The type for the secondSingle
.T3
- The type for the thirdSingle
.T4
- The type for the fourthSingle
.R
- The result type of the zipper.- Parameters:
s1
- The firstSingle
to zip.s2
- The secondSingle
to zip.s3
- The thirdSingle
to zip.s4
- The fourthSingle
to zip.zipper
- Used to combine the completed results for each item fromsingles
.- Returns:
- a new
Single
that emits the results of a specified zipperFunction4
to items emitted bys1
,s2
,s3
, ands4
. - See Also:
-
zip
public static <R> Single<R> zip(Function<? super Object[], ? extends R> zipper, Single<?>... singles) Create a newSingle
that emits the results of a specified zipperFunction
to items emitted bysingles
.From a sequential programming point of view this method is roughly equivalent to the following:
Function<? super CompletableFuture<?>[], ? extends R> zipper = ...; CompletableFuture<?>[] futures = ...; // Provided Futures (analogous to the Singles here) CompletableFuture.allOf(futures).get(); // wait for all futures to complete return zipper.apply(futures);
- Type Parameters:
R
- The result type of the zipper.- Parameters:
zipper
- Used to combine the completed results for each item fromsingles
.singles
- The collection ofSingle
s that when complete provides the results to "zip" (aka combine) together.- Returns:
- a new
Single
that emits the results of a specified zipperFunction
to items emitted bysingles
. - See Also:
-
zipDelayError
public static <R> Single<R> zipDelayError(Function<? super Object[], ? extends R> zipper, Single<?>... singles) Create a newSingle
that emits the results of a specified zipperFunction
to items emitted bysingles
. If any of theSingle
s terminate with an error, the returnedSingle
will wait for termination till all the otherSingle
s have been subscribed and terminated, and then terminate with the first error.From a sequential programming point of view this method is roughly equivalent to the following:
Function<? super CompletableFuture<?>[], ? extends R> zipper = ...; CompletableFuture<?>[] futures = ...; // Provided Futures (analogous to the Singles here) CompletableFuture.allOf(futures).get(); // wait for all futures to complete return zipper.apply(futures);
- Type Parameters:
R
- The result type of the zipper.- Parameters:
zipper
- Used to combine the completed results for each item fromsingles
.singles
- The collection ofSingle
s that when complete provides the results to "zip" (aka combine) together.- Returns:
- a new
Single
that emits the results of a specified zipperFunction
to items emitted bysingles
. - See Also:
-
concat(Publisher)
orconcatDeferSubscribe(Publisher)
instead.