T
- Type of the result of the single.public abstract class Single<T>
extends java.lang.Object
SingleSource.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
.Modifier | Constructor and Description |
---|---|
protected |
Single()
New instance.
|
Modifier and Type | Method and Description |
---|---|
Single<T> |
afterCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument after Cancellable.cancel() is
called for Subscriptions of the returned Single . |
Single<T> |
afterFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally 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 returned Single . |
Single<T> |
afterOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument after
SingleSource.Subscriber.onError(Throwable) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
afterOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
Invokes the
onSubscribe Consumer argument after
SingleSource.Subscriber.onSubscribe(Cancellable) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
afterOnSuccess(java.util.function.Consumer<? super T> onSuccess)
Invokes the
onSuccess Consumer argument after
SingleSource.Subscriber.onSuccess(Object) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
afterSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
Creates a new
SingleSource.Subscriber (via the subscriberSupplier argument) on each call to subscribe and
invokes all the SingleSource.Subscriber methods after the SingleSource.Subscriber s of the returned
Single . |
Single<T> |
beforeCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument before Cancellable.cancel() is
called for Subscriptions of the returned Single . |
Single<T> |
beforeFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally 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 returned Single . |
Single<T> |
beforeOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument before
SingleSource.Subscriber.onError(Throwable) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
beforeOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
Invokes the
onSubscribe Consumer argument before
SingleSource.Subscriber.onSubscribe(Cancellable) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
beforeOnSuccess(java.util.function.Consumer<? super T> onSuccess)
Invokes the
onSuccess Consumer argument before
SingleSource.Subscriber.onSuccess(Object) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
beforeSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
Creates a new
SingleSource.Subscriber (via the subscriberSupplier argument) on each call to subscribe and
invokes all the SingleSource.Subscriber methods before the SingleSource.Subscriber s of the returned
Single . |
static <T> Single<java.util.Collection<T>> |
collectUnordered(int maxConcurrency,
Single<? extends T>... singles)
Asynchronously collects results of the passed
Single s into a single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnordered(java.lang.Iterable<? extends Single<? extends T>> singles)
Asynchronously collects results of individual
Single s returned by the passed Iterable into a
single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnordered(java.lang.Iterable<? extends Single<? extends T>> singles,
int maxConcurrency)
Asynchronously collects results of individual
Single s returned by the passed Iterable into a
single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnordered(Single<? extends T>... singles)
Asynchronously collects results of the passed
Single s into a single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnorderedDelayError(int maxConcurrency,
Single<? extends T>... singles)
Asynchronously collects results of the passed
Single s into a single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnorderedDelayError(java.lang.Iterable<? extends Single<? extends T>> singles)
Asynchronously collects results of individual
Single s returned by the passed Iterable into a
single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnorderedDelayError(java.lang.Iterable<? extends Single<? extends T>> singles,
int maxConcurrency)
Asynchronously collects results of individual
Single s returned by the passed Iterable into a
single Collection . |
static <T> Single<java.util.Collection<T>> |
collectUnorderedDelayError(Single<? extends T>... singles)
Asynchronously collects results of the passed
Single s into a single Collection . |
Single<T> |
concat(Completable next)
Returns a
Single that emits the result of this Single after next Completable
terminates successfully. |
Publisher<T> |
concat(Publisher<? extends T> next)
|
Publisher<T> |
concat(Single<? extends T> next)
|
static <T> Single<T> |
defer(java.util.function.Supplier<? extends Single<? extends T>> singleSupplier)
Defer creation of a
Single till it is subscribed to. |
static <T> Single<T> |
failed(java.lang.Throwable cause)
Creates a realized
Single which always completes with the provided error cause . |
<R> Single<R> |
flatMap(java.util.function.Function<? super T,? extends Single<? extends R>> next)
|
Completable |
flatMapCompletable(java.util.function.Function<? super T,? extends Completable> next)
|
<R> Publisher<R> |
flatMapPublisher(java.util.function.Function<? super T,? extends Publisher<? extends R>> next)
|
static <T> Single<T> |
fromCallable(java.util.concurrent.Callable<T> callable)
|
static <T> Single<T> |
fromFuture(java.util.concurrent.Future<? extends T> future)
|
static <T> Single<T> |
fromStage(java.util.concurrent.CompletionStage<? extends T> stage)
Convert from a
CompletionStage to a Single . |
static <T> Single<T> |
fromSupplier(java.util.function.Supplier<T> supplier)
|
protected abstract void |
handleSubscribe(SingleSource.Subscriber<? super T> subscriber)
Handles a subscriber to this
Single . |
Single<T> |
idleTimeout(java.time.Duration duration)
|
Single<T> |
idleTimeout(java.time.Duration duration,
Executor timeoutExecutor)
|
Single<T> |
idleTimeout(long duration,
java.util.concurrent.TimeUnit unit)
|
Single<T> |
idleTimeout(long duration,
java.util.concurrent.TimeUnit unit,
Executor timeoutExecutor)
|
Completable |
ignoreElement()
Ignores the result of this
Single and forwards the termination signal to the returned
Completable . |
<R> Single<R> |
liftAsync(SingleOperator<? super T,? extends R> operator)
This method requires advanced knowledge of building operators.
|
<R> Single<R> |
liftSync(SingleOperator<? super T,? extends R> operator)
This method requires advanced knowledge of building operators.
|
<R> Single<R> |
map(java.util.function.Function<? super T,? extends R> mapper)
Maps the result of this single to a different type.
|
static <T> Single<T> |
never()
Creates a
Single that never terminates. |
Single<T> |
publishAndSubscribeOn(Executor executor)
Creates a new
Single that will use the passed Executor to invoke the following methods:
All SingleSource.Subscriber methods.
All Cancellable methods.
The handleSubscribe(SingleSource.Subscriber) method.
This method does not override preceding Executor s, if any, specified for this
Single . |
Single<T> |
publishAndSubscribeOnOverride(Executor executor)
Creates a new
Single that will use the passed Executor to invoke the following methods:
All SingleSource.Subscriber methods.
All Cancellable methods.
The handleSubscribe(SingleSource.Subscriber) method.
This method overrides preceding Executor s, if any, specified for this Single . |
Single<T> |
publishOn(Executor executor)
Creates a new
Single that will use the passed Executor to invoke all SingleSource.Subscriber methods. |
Single<T> |
publishOnOverride(Executor executor)
Creates a new
Single that will use the passed Executor to invoke all SingleSource.Subscriber methods. |
Single<T> |
recoverWith(java.util.function.Function<java.lang.Throwable,? extends Single<? extends T>> nextFactory)
|
Publisher<T> |
repeat(java.util.function.IntPredicate shouldRepeat)
|
Publisher<T> |
repeatWhen(java.util.function.IntFunction<? extends Completable> repeatWhen)
Re-subscribes to this
Single when it completes and the Completable returned by the supplied
IntFunction completes successfully. |
Single<T> |
retry(BiIntPredicate<java.lang.Throwable> shouldRetry)
|
Single<T> |
retryWhen(BiIntFunction<java.lang.Throwable,? extends Completable> retryWhen)
Re-subscribes to this
Single if an error is emitted and the Completable returned by the supplied
BiIntFunction completes successfully. |
Cancellable |
subscribe(java.util.function.Consumer<? super T> resultConsumer)
Subscribe to this
Single , emits the result to the passed Consumer and log any
SingleSource.Subscriber.onError(Throwable) . |
protected void |
subscribeInternal(SingleSource.Subscriber<? super T> subscriber)
A internal subscribe method similar to
SingleSource.subscribe(Subscriber) which can be used by
different implementations to subscribe. |
Single<T> |
subscribeOn(Executor executor)
Creates a new
Single that will use the passed Executor to invoke the following methods:
All Cancellable methods.
The handleSubscribe(SingleSource.Subscriber) method.
This method does not override preceding Executor s, if any, specified for this
Single . |
Single<T> |
subscribeOnOverride(Executor executor)
Creates a new
Single that will use the passed Executor to invoke the following methods:
All Cancellable methods.
The handleSubscribe(SingleSource.Subscriber) method.
This method overrides preceding Executor s, if any, specified for this Single . |
Single<T> |
subscribeShareContext()
Signifies that when the returned
Single is subscribed to, the AsyncContext will be shared
instead of making a copy . |
static <T> Single<T> |
succeeded(T value)
Creates a realized
Single which always completes successfully with the provided value . |
Completable |
toCompletable()
Ignores the result of this
Single and forwards the termination signal to the returned
Completable . |
java.util.concurrent.CompletionStage<T> |
toCompletionStage()
Convert this
Single to a CompletionStage . |
java.util.concurrent.Future<T> |
toFuture()
Convert this
Single to a Future . |
Publisher<T> |
toPublisher()
Converts this
Single to a Publisher . |
Single<T> |
whenCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument when Cancellable.cancel() is called for
Subscriptions of the returned Single . |
Single<T> |
whenFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally 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 returned Single . |
Single<T> |
whenOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument when SingleSource.Subscriber.onError(Throwable) is called for
SingleSource.Subscriber s of the returned Single . |
Single<T> |
whenOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
Invokes the
onSubscribe Consumer argument when
SingleSource.Subscriber.onSubscribe(Cancellable) is called for SingleSource.Subscriber s of the returned Single . |
Single<T> |
whenOnSuccess(java.util.function.Consumer<? super T> onSuccess)
Invokes the
onSuccess Consumer argument when SingleSource.Subscriber.onSuccess(Object) is called for
SingleSource.Subscriber s of the returned Single . |
Single<T> |
whenSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
Creates a new
SingleSource.Subscriber (via the subscriberSupplier argument) for each new subscribe and
invokes methods on that SingleSource.Subscriber when the corresponding methods are called for SingleSource.Subscriber s of
the returned Single . |
public final <R> Single<R> map(java.util.function.Function<? super T,? extends R> mapper)
Single
.
This method provides a data transformation in sequential programming similar to:
T tResult = resultOfThisSingle();
R rResult = mapper.apply(tResult);
R
- Type of the returned Single
.mapper
- To convert this result to other.Single
that will now have the result of type R
.public final Single<T> recoverWith(java.util.function.Function<java.lang.Throwable,? extends Single<? extends T>> nextFactory)
Single
by using another Single
provided by the
passed nextFactory
.
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;
public final <R> Single<R> flatMap(java.util.function.Function<? super T,? extends Single<? extends R>> next)
Single
that mirrors emissions from the Single
returned by next
.
Any error emitted by this Single
is forwarded to the returned Single
.
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.
public final Completable flatMapCompletable(java.util.function.Function<? super T,? extends Completable> next)
Completable
that mirrors emissions from the Completable
returned by next
.
Any error emitted by this Single
is forwarded to the returned Completable
.
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.
next
- Function to give the next Completable
.Completable
that switches to the Completable
returned by next
after this
Single
completes successfully.public final <R> Publisher<R> flatMapPublisher(java.util.function.Function<? super T,? extends Publisher<? extends R>> next)
Publisher
that mirrors emissions from the Publisher
returned by next
.
Any error emitted by this Single
is forwarded to the returned Publisher
.
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
}
public final Single<T> whenOnSuccess(java.util.function.Consumer<? super T> onSuccess)
onSuccess
Consumer
argument when SingleSource.Subscriber.onSuccess(Object)
is called for
SingleSource.Subscriber
s of the returned Single
.
The order in which onSuccess
will be invoked relative to SingleSource.Subscriber.onSuccess(Object)
is
undefined. If you need strict ordering see beforeOnSuccess(Consumer)
and
afterOnSuccess(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);
onSuccess
- Invoked when SingleSource.Subscriber.onSuccess(Object)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.beforeOnSuccess(Consumer)
,
afterOnSuccess(Consumer)
public final Single<T> whenOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument when SingleSource.Subscriber.onError(Throwable)
is called for
SingleSource.Subscriber
s of the returned Single
.
The order in which onError
will be invoked relative to SingleSource.Subscriber.onError(Throwable)
is
undefined. If you need strict ordering see beforeOnError(Consumer)
and
afterOnError(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);
}
onError
- Invoked when SingleSource.Subscriber.onError(Throwable)
is called for SingleSource.Subscriber
s of the
returned Single
. MUST NOT throw.Single
.beforeOnError(Consumer)
,
afterOnError(Consumer)
public final Single<T> whenFinally(java.lang.Runnable doFinally)
whenFinally
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 returned Single
.
The order in which whenFinally
will be invoked relative to the above methods is undefined. If you need
strict ordering see beforeFinally(Runnable)
and afterFinally(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
whenFinally.run();
}
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 returned Single
. MUST NOT throw.Single
.afterFinally(Runnable)
,
beforeFinally(Runnable)
public final Single<T> whenCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument when Cancellable.cancel()
is called for
Subscriptions of the returned Single
.
The order in which whenFinally
will be invoked relative to Cancellable.cancel()
is undefined. If
you need strict ordering see beforeCancel(Runnable)
and afterCancel(Runnable)
.
onCancel
- Invoked when Cancellable.cancel()
is called for Subscriptions of the
returned Single
. MUST NOT throw.Single
.beforeCancel(Runnable)
,
afterCancel(Runnable)
public final Single<T> idleTimeout(long duration, java.util.concurrent.TimeUnit unit)
Single
that will mimic the signals of this Single
but will terminate with a
with a TimeoutException
if time duration
elapses between subscribe and
termination. The timer starts when the returned Single
is subscribed.
In the event of timeout any Cancellable
from SingleSource.Subscriber.onSubscribe(Cancellable)
will be
cancelled
and the associated SingleSource.Subscriber
will be
terminated
.
duration
- The time duration which is allowed to elapse before SingleSource.Subscriber.onSuccess(Object)
.unit
- The units for duration
.Single
that will mimic the signals of this Single
but will terminate with a
TimeoutException
if time duration
elapses before SingleSource.Subscriber.onSuccess(Object)
.public final Single<T> idleTimeout(long duration, java.util.concurrent.TimeUnit unit, Executor timeoutExecutor)
Single
that will mimic the signals of this Single
but will terminate with a
with a TimeoutException
if time duration
elapses between subscribe and
termination. The timer starts when the returned Single
is subscribed.
In the event of timeout any Cancellable
from SingleSource.Subscriber.onSubscribe(Cancellable)
will be
cancelled
and the associated SingleSource.Subscriber
will be
terminated
.
duration
- The time duration which is allowed to elapse before SingleSource.Subscriber.onSuccess(Object)
.unit
- The units for duration
.timeoutExecutor
- The Executor
to use for managing the timer notifications.Single
that will mimic the signals of this Single
but will terminate with a
TimeoutException
if time duration
elapses before SingleSource.Subscriber.onSuccess(Object)
.public final Single<T> idleTimeout(java.time.Duration duration)
Single
that will mimic the signals of this Single
but will terminate with a
with a TimeoutException
if time duration
elapses between subscribe and
termination. The timer starts when the returned Single
is subscribed.
In the event of timeout any Cancellable
from SingleSource.Subscriber.onSubscribe(Cancellable)
will be
cancelled
and the associated SingleSource.Subscriber
will be
terminated
.
SingleSource.Subscriber
will via terminated
.
duration
- The time duration which is allowed to elapse before SingleSource.Subscriber.onSuccess(Object)
.Single
that will mimic the signals of this Single
but will terminate with a
TimeoutException
if time duration
elapses before SingleSource.Subscriber.onSuccess(Object)
.public final Single<T> idleTimeout(java.time.Duration duration, Executor timeoutExecutor)
Single
that will mimic the signals of this Single
but will terminate with a
with a TimeoutException
if time duration
elapses between subscribe and termination.
The timer starts when the returned Single
is subscribed.
In the event of timeout any Cancellable
from SingleSource.Subscriber.onSubscribe(Cancellable)
will be
cancelled
and the associated SingleSource.Subscriber
will be
terminated
.
duration
- The time duration which is allowed to elapse before SingleSource.Subscriber.onSuccess(Object)
.timeoutExecutor
- The Executor
to use for managing the timer notifications.Single
that will mimic the signals of this Single
but will terminate with a
TimeoutException
if time duration
elapses before SingleSource.Subscriber.onSuccess(Object)
.public final Publisher<T> concat(Single<? extends T> next)
Publisher
that first emits the result of this Single
and then subscribes and emits
result of next
Single
. Any error emitted by this Single
or next
Single
is
forwarded to the returned Publisher
.
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;
public final Single<T> concat(Completable next)
Single
that emits the result of this Single
after next
Completable
terminates successfully.
next
Completable
will only be subscribed to after this Single
terminates successfully.
Any error emitted by this Single
or next
Completable
is forwarded to the returned
Single
.
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;
next
- Completable
to concat.Single
that emits the result of this Single
after next
Completable
terminates successfully.public final Publisher<T> concat(Publisher<? extends T> next)
Publisher
that first emits the result of this Single
and then subscribes and emits all
elements from next
Publisher
. Any error emitted by this Single
or next
Publisher
is forwarded to the returned Publisher
.
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;
public final Single<T> retry(BiIntPredicate<java.lang.Throwable> shouldRetry)
Single
if an error is emitted and the passed BiIntPredicate
returns
true
.
This method provides a means to retry an operation under certain failure conditions and in sequential programming is similar to:
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;
}
}
}
shouldRetry
- BiIntPredicate
that given the retry count and the most recent Throwable
emitted from this
Single
determines if the operation should be retried.Single
that emits the result from this Single
or re-subscribes if an error is emitted
and if the passed BiIntPredicate
returned true
.public final Single<T> retryWhen(BiIntFunction<java.lang.Throwable,? extends Completable> retryWhen)
Single
if an error is emitted and the Completable
returned by the supplied
BiIntFunction
completes successfully. If the returned Completable
emits an error, the returned
Single
terminates with that error.
This method provides a means to retry an operation under certain failure conditions in an asynchronous fashion and in sequential programming is similar to:
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;
}
}
}
retryWhen
- BiIntFunction
that given the retry count and the most recent Throwable
emitted
from this Single
returns a Completable
. If this Completable
emits an error, that error is
emitted from the returned Single
, otherwise, original Single
is re-subscribed when this
Completable
completes.Single
that emits the result from this Single
or re-subscribes if an error is emitted
and Completable
returned by BiIntFunction
completes successfully.public final Publisher<T> repeat(java.util.function.IntPredicate shouldRepeat)
Single
when it completes and the passed IntPredicate
returns true
.
This method provides a means to repeat an operation multiple times and in sequential programming is similar to:
List<T> results = new ...;
int i = 0;
do {
results.add(resultOfThisSingle());
} while (shouldRepeat.test(++i));
return results;
shouldRepeat
- IntPredicate
that given the repeat count determines if the operation should be
repeated.Publisher
that emits all items from this Single
and from all re-subscriptions whenever
the operation is repeated.public final Publisher<T> repeatWhen(java.util.function.IntFunction<? extends Completable> repeatWhen)
Single
when it completes and the Completable
returned by the supplied
IntFunction
completes successfully. If the returned Completable
emits an error, the returned
Single
emits an error.
This method provides a means to repeat an operation multiple times when in an asynchronous fashion and in sequential programming is similar to:
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;
repeatWhen
- IntFunction
that given the repeat count returns a Completable
.
If this Completable
emits an error repeat is terminated, otherwise, original Single
is
re-subscribed when this Completable
completes.Publisher
that emits all items from this Single
and from all re-subscriptions whenever
the operation is repeated.public final Single<T> beforeOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
onSubscribe
Consumer
argument before
SingleSource.Subscriber.onSubscribe(Cancellable)
is called for SingleSource.Subscriber
s of the returned Single
.onSubscribe
- Invoked before SingleSource.Subscriber.onSubscribe(Cancellable)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> beforeOnSuccess(java.util.function.Consumer<? super T> onSuccess)
onSuccess
Consumer
argument before
SingleSource.Subscriber.onSuccess(Object)
is called for SingleSource.Subscriber
s of the returned Single
.
From a sequential programming point of view this method is roughly equivalent to the following:
T result = resultOfThisSingle();
onSuccess.accept(result);
nextOperation(result);
onSuccess
- Invoked before SingleSource.Subscriber.onSuccess(Object)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> beforeOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument before
SingleSource.Subscriber.onError(Throwable)
is called for SingleSource.Subscriber
s of the returned Single
.
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);
}
onError
- Invoked before SingleSource.Subscriber.onError(Throwable)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> beforeCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument before Cancellable.cancel()
is
called for Subscriptions of the returned Single
.onCancel
- Invoked before Cancellable.cancel()
is called for Subscriptions of the
returned Single
. MUST NOT throw.Single
.public final Single<T> beforeFinally(java.lang.Runnable doFinally)
whenFinally
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 returned Single
.
From a sequential programming point of view this method is roughly equivalent to the following:
try {
T result = resultOfThisSingle();
} finally {
whenFinally.run();
nextOperation(); // Maybe notifying of cancellation, or termination
}
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 returned Single
. MUST NOT throw.Single
.public final Single<T> beforeSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
SingleSource.Subscriber
(via the subscriberSupplier
argument) on each call to subscribe and
invokes all the SingleSource.Subscriber
methods before the SingleSource.Subscriber
s of the returned
Single
.subscriberSupplier
- Creates a new SingleSource.Subscriber
on each call to subscribe and invokes all the
SingleSource.Subscriber
methods before the SingleSource.Subscriber
s of the returned Single
.
SingleSource.Subscriber
methods MUST NOT throw.Single
.public final Single<T> afterOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
onSubscribe
Consumer
argument after
SingleSource.Subscriber.onSubscribe(Cancellable)
is called for SingleSource.Subscriber
s of the returned Single
.onSubscribe
- Invoked after SingleSource.Subscriber.onSubscribe(Cancellable)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> whenOnSubscribe(java.util.function.Consumer<Cancellable> onSubscribe)
onSubscribe
Consumer
argument when
SingleSource.Subscriber.onSubscribe(Cancellable)
is called for SingleSource.Subscriber
s of the returned Single
.
The order in which onSubscribe
will be invoked relative to
SingleSource.Subscriber.onSubscribe(Cancellable)
is undefined. If you need strict ordering see
beforeOnSubscribe(Consumer)
and afterOnSubscribe(Consumer)
.
onSubscribe
- Invoked when SingleSource.Subscriber.onSubscribe(Cancellable)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.beforeOnSubscribe(Consumer)
,
afterOnSubscribe(Consumer)
public final Single<T> afterOnSuccess(java.util.function.Consumer<? super T> onSuccess)
onSuccess
Consumer
argument after
SingleSource.Subscriber.onSuccess(Object)
is called for SingleSource.Subscriber
s of the returned Single
.
From a sequential programming point of view this method is roughly equivalent to the following:
T result = resultOfThisSingle();
nextOperation(result);
onSuccess.accept(result);
onSuccess
- Invoked after SingleSource.Subscriber.onSuccess(Object)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> afterOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument after
SingleSource.Subscriber.onError(Throwable)
is called for SingleSource.Subscriber
s of the returned Single
.
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);
}
onError
- Invoked after SingleSource.Subscriber.onError(Throwable)
is called for
SingleSource.Subscriber
s of the returned Single
. MUST NOT throw.Single
.public final Single<T> afterCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument after Cancellable.cancel()
is
called for Subscriptions of the returned Single
.onCancel
- Invoked after Cancellable.cancel()
is called for Subscriptions of the
returned Single
. MUST NOT throw.Single
.public final Single<T> afterFinally(java.lang.Runnable doFinally)
whenFinally
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 returned Single
.
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
whenFinally.run();
}
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 returned Single
. MUST NOT throw.Single
.public final Single<T> afterSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
SingleSource.Subscriber
(via the subscriberSupplier
argument) on each call to subscribe and
invokes all the SingleSource.Subscriber
methods after the SingleSource.Subscriber
s of the returned
Single
.subscriberSupplier
- Creates a new SingleSource.Subscriber
on each call to subscribe and invokes all the
SingleSource.Subscriber
methods after the SingleSource.Subscriber
s of the returned Single
.
SingleSource.Subscriber
methods MUST NOT throw.Single
.public final Single<T> whenSubscriber(java.util.function.Supplier<? extends SingleSource.Subscriber<? super T>> subscriberSupplier)
SingleSource.Subscriber
(via the subscriberSupplier
argument) for each new subscribe and
invokes methods on that SingleSource.Subscriber
when the corresponding methods are called for SingleSource.Subscriber
s of
the returned Single
.subscriberSupplier
- Creates a new SingleSource.Subscriber
for each new subscribe and invokes methods on that
SingleSource.Subscriber
when the corresponding methods are called for SingleSource.Subscriber
s of the returned
Single
. SingleSource.Subscriber
methods MUST NOT throw.Single
.public final Single<T> publishOn(Executor executor)
Single
that will use the passed Executor
to invoke all SingleSource.Subscriber
methods.
This method does not override preceding Executor
s, if any, specified for this
Single
. Only subsequent operations, if any, added in this execution chain will use this
Executor
. If such an override is required, publishOnOverride(Executor)
can be used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods on the
SingleSource.Subscriber
.public final Single<T> publishOnOverride(Executor executor)
Single
that will use the passed Executor
to invoke all SingleSource.Subscriber
methods.
This method overrides preceding Executor
s, if any, specified for this
Single
.
That is to say preceding and subsequent operations for this execution chain will use this Executor
for
invoking all SingleSource.Subscriber
methods. If such an override is not required, publishOn(Executor)
can be
used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods of
SingleSource.Subscriber
, Cancellable
and handleSubscribe(SingleSource.Subscriber)
both for the
returned Single
as well as this
Single
.public final Single<T> subscribeOn(Executor executor)
Single
that will use the passed Executor
to invoke the following methods:
Cancellable
methods.handleSubscribe(SingleSource.Subscriber)
method.Executor
s, if any, specified for this
Single
. Only subsequent operations, if any, added in this execution chain will use this
Executor
. If such an override is required, subscribeOnOverride(Executor)
can be used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods of
Cancellable
and handleSubscribe(SingleSource.Subscriber)
.public final Single<T> subscribeOnOverride(Executor executor)
Single
that will use the passed Executor
to invoke the following methods:
Cancellable
methods.handleSubscribe(SingleSource.Subscriber)
method.Executor
s, if any, specified for this
Single
.
That is to say preceding and subsequent operations for this execution chain will use this Executor
for
invoking the above specified methods.
If such an override is not required, subscribeOn(Executor)
can be used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods of
Cancellable
and handleSubscribe(SingleSource.Subscriber)
both for the returned
Single
as well as this
Single
.public final Single<T> publishAndSubscribeOn(Executor executor)
Single
that will use the passed Executor
to invoke the following methods:
SingleSource.Subscriber
methods.Cancellable
methods.handleSubscribe(SingleSource.Subscriber)
method.Executor
s, if any, specified for this
Single
. Only subsequent operations, if any, added in this execution chain will use this
Executor
. If such an override is required, publishAndSubscribeOnOverride(Executor)
can be used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods
SingleSource.Subscriber
, Cancellable
and handleSubscribe(SingleSource.Subscriber)
.public final Single<T> publishAndSubscribeOnOverride(Executor executor)
Single
that will use the passed Executor
to invoke the following methods:
SingleSource.Subscriber
methods.Cancellable
methods.handleSubscribe(SingleSource.Subscriber)
method.Executor
s, if any, specified for this
Single
.
That is to say preceding and subsequent operations for this execution chain will use this Executor
.
If such an override is not required, publishAndSubscribeOn(Executor)
can be used.executor
- Executor
to use.Single
that will use the passed Executor
to invoke all methods of
SingleSource.Subscriber
, Cancellable
and handleSubscribe(SingleSource.Subscriber)
both for the
returned Single
as well as this
Single
.public final Single<T> subscribeShareContext()
Single
is subscribed to, the AsyncContext
will be shared
instead of making a copy
.
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.
Single
that will share the AsyncContext
instead of making a
copy
when subscribed to.public final <R> Single<R> liftSync(SingleOperator<? super T,? extends R> operator)
Returns a Single
which will wrap the SingleSource.Subscriber
using the provided operator
argument before subscribing to this Single
.
Single<X> pub = ...;
pub.map(..) // A
.liftSync(original -> modified)
.afterFinally(..) // B
The original -> modified
"operator" MUST be "synchronous" in that it does not interact
with the original SingleSource.Subscriber
from outside the modified SingleSource.Subscriber
or Cancellable
threads. That is to say this operator will not impact the Executor
constraints already in place between
A and B above. If you need asynchronous behavior, or are unsure, see
liftAsync(SingleOperator)
.R
- Type of the items emitted by the returned Single
.operator
- The custom operator logic. The input is the "original" SingleSource.Subscriber
to this
Single
and the return is the "modified" SingleSource.Subscriber
that provides custom operator business
logic.Single
which when subscribed, the operator
argument will be used to wrap the
SingleSource.Subscriber
before subscribing to this Single
.liftAsync(SingleOperator)
public final <R> Single<R> liftAsync(SingleOperator<? super T,? extends R> operator)
Returns a Single
which will wrap the SingleSource.Subscriber
using the provided operator
argument before subscribing to this Single
.
Publisher<X> pub = ...;
pub.map(..) // Aw
.liftAsync(original -> modified)
.afterFinally(..) // B
The original -> modified
"operator" MAY be "asynchronous" in that it may interact with the original
SingleSource.Subscriber
from outside the modified SingleSource.Subscriber
or Cancellable
threads. More
specifically:
SingleSource.Subscriber
invocations going "downstream" (i.e. from A to B above) MAY be
offloaded via an Executor
Cancellable
invocations going "upstream" (i.e. from B to A above) MAY be
offloaded via an Executor
R
- Type of the items emitted by the returned Single
.operator
- The custom operator logic. The input is the "original" SingleSource.Subscriber
to this
Single
and the return is the "modified" SingleSource.Subscriber
that provides custom operator business
logic.Single
which when subscribed, the operator
argument will be used to wrap the
SingleSource.Subscriber
before subscribing to this Single
.liftSync(SingleOperator)
public final Publisher<T> toPublisher()
Single
to a Publisher
.Publisher
that emits at most a single item which is emitted by this Single
.public final Completable toCompletable()
Single
and forwards the termination signal to the returned
Completable
.Completable
that mirrors the terminal signal from this Single
.public final Completable ignoreElement()
Single
and forwards the termination signal to the returned
Completable
.Completable
that mirrors the terminal signal from this Single
.public final java.util.concurrent.CompletionStage<T> toCompletionStage()
Single
to a CompletionStage
.CompletionStage
that mirrors the terminal signal from this Single
.public final java.util.concurrent.Future<T> toFuture()
Single
to a Future
.Future
that mirrors the terminal signal from this Single
.protected final void subscribeInternal(SingleSource.Subscriber<? super T> subscriber)
SingleSource.subscribe(Subscriber)
which can be used by
different implementations to subscribe.subscriber
- SingleSource.Subscriber
to subscribe for the result.public final Cancellable subscribe(java.util.function.Consumer<? super T> resultConsumer)
Single
, emits the result to the passed Consumer
and log any
SingleSource.Subscriber.onError(Throwable)
.resultConsumer
- Consumer
to accept the result of this Single
.Cancellable
used to invoke Cancellable.cancel()
on the parameter of
SingleSource.Subscriber.onSubscribe(Cancellable)
for this Single
.protected abstract void handleSubscribe(SingleSource.Subscriber<? super T> subscriber)
Single
.subscriber
- the subscriber.public static <T> Single<T> succeeded(@Nullable T value)
Single
which always completes successfully with the provided value
.public static <T> Single<T> fromCallable(java.util.concurrent.Callable<T> callable)
Single
which when subscribed will invoke Callable.call()
on the passed
Callable
and emit the value returned by that invocation from the returned Single
. Any error
emitted by the Callable
will terminate the returned Single
with the same error.
Blocking inside Callable.call()
will in turn block the subscribe call to the returned Single
. If
this behavior is undesirable then the returned Single
should be offloaded using one of the operators that
offloads the subscribe call (eg: subscribeOn(Executor)
, publishAndSubscribeOn(Executor)
).
public static <T> Single<T> fromSupplier(java.util.function.Supplier<T> supplier)
Single
which when subscribed will invoke Supplier.get()
on the passed
Supplier
and emit the value returned by that invocation from the returned Single
. Any error
emitted by the Supplier
will terminate the returned Single
with the same error.
Blocking inside Supplier.get()
will in turn block the subscribe call to the returned Single
. If
this behavior is undesirable then the returned Single
should be offloaded using one of the operators that
offloads the subscribe call (eg: subscribeOn(Executor)
, publishAndSubscribeOn(Executor)
).
public static <T> Single<T> failed(java.lang.Throwable cause)
Single
which always completes with the provided error cause
.public static <T> Single<T> defer(java.util.function.Supplier<? extends Single<? extends T>> singleSupplier)
Single
till it is subscribed to.T
- Type of the Single
.singleSupplier
- Supplier
to create a new Single
every time the returned Single
is
subscribed.Single
that creates a new Single
using singleSupplier
every time
it is subscribed and forwards all items and terminal events from the newly created Single
to its
SingleSource.Subscriber
.public static <T> Single<T> fromFuture(java.util.concurrent.Future<? extends T> future)
Future
to a Single
via Future.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 if
Cancellable.cancel()
will be called and this operation may block.
To apply a timeout see idleTimeout(long, TimeUnit)
and related methods.
T
- The data type the Future
provides when complete.future
- The Future
to convert.Single
that derives results from Future
.idleTimeout(long, TimeUnit)
public static <T> Single<java.util.Collection<T>> collectUnordered(java.lang.Iterable<? extends Single<? extends T>> singles)
Single
s returned by the passed Iterable
into a
single Collection
.
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, returned Single
will immediately terminate with
that error. In such a case, any in progress Single
s will be cancelled. In order to delay error
termination use collectUnorderedDelayError(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;
T
- Type of the result of the individual Single
ssingles
- Iterable
of Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.@SafeVarargs public static <T> Single<java.util.Collection<T>> collectUnordered(Single<? extends T>... singles)
Single
s into a single Collection
.
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, returned Single
will immediately terminate with
that error. In such a case, any in progress Single
s will be cancelled. In order to delay error
termination use collectUnorderedDelayError(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;
T
- Type of the result of the individual Single
ssingles
- Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.public static <T> Single<java.util.Collection<T>> collectUnordered(java.lang.Iterable<? extends Single<? extends T>> singles, int maxConcurrency)
Single
s returned by the passed Iterable
into a
single Collection
.
If any of the Single
s terminate with an error, returned Single
will immediately terminate with
that error. In such a case, any in progress Single
s will be cancelled. In order to delay error
termination use collectUnorderedDelayError(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;
T
- Type of the result of the individual Single
ssingles
- Iterable
of Single
s, results of which are to be collected.maxConcurrency
- Maximum number of Single
s that will be active at any point in time.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.@SafeVarargs public static <T> Single<java.util.Collection<T>> collectUnordered(int maxConcurrency, Single<? extends T>... singles)
Single
s into a single Collection
.
If any of the Single
s terminate with an error, returned Single
will immediately terminate with
that error. In such a case, any in progress Single
s will be cancelled. In order to delay error
termination use collectUnorderedDelayError(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;
T
- Type of the result of the individual Single
smaxConcurrency
- Maximum number of Single
s that will be active at any point in time.singles
- Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.public static <T> Single<java.util.Collection<T>> collectUnorderedDelayError(java.lang.Iterable<? extends Single<? extends T>> singles)
Single
s returned by the passed Iterable
into a
single Collection
.
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, returned Single
will wait for termination till all
the other Single
s have been subscribed and terminated. If it is expected for the returned Single
to terminate on the first failing Single
, 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);
T
- Type of the result of the individual Single
ssingles
- Iterable
of Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.@SafeVarargs public static <T> Single<java.util.Collection<T>> collectUnorderedDelayError(Single<? extends T>... singles)
Single
s into a single Collection
.
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, returned Single
will wait for termination till all
the other Single
s have been subscribed and terminated. If it is expected for the returned Single
to terminate on the first failing Single
, 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);
T
- Type of the result of the individual Single
ssingles
- Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.public static <T> Single<java.util.Collection<T>> collectUnorderedDelayError(java.lang.Iterable<? extends Single<? extends T>> singles, int maxConcurrency)
Single
s returned by the passed Iterable
into a
single Collection
.
If any of the Single
s terminate with an error, returned Single
will wait for termination till all
the other Single
s have been subscribed and terminated. If it is expected for the returned Single
to terminate on the first failing Single
, 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);
T
- Type of the result of the individual Single
ssingles
- Iterable
of Single
s, results of which are to be collected.maxConcurrency
- Maximum number of Single
s that will be active at any point in time.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.@SafeVarargs public static <T> Single<java.util.Collection<T>> collectUnorderedDelayError(int maxConcurrency, Single<? extends T>... singles)
Single
s into a single Collection
.
If any of the Single
s terminate with an error, returned Single
will wait for termination till all
the other Single
s have been subscribed and terminated. If it is expected for the returned Single
to terminate on the first failing Single
, 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);
T
- Type of the result of the individual Single
smaxConcurrency
- Maximum number of Single
s that will be active at any point in time.singles
- Single
s, results of which are to be collected.Single
producing a Collection
of all values produced by the individual Single
s.
There is no guarantee of the order of the values in the produced Collection
as compared to the order of
Single
s passed to this method.public static <T> Single<T> fromStage(java.util.concurrent.CompletionStage<? extends T> stage)
CompletionStage
to a Single
.
A best effort is made to propagate Cancellable.cancel()
to the CompletionStage
. Cancellation for
CompletionStage
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 an Executor
is appropriate.
T
- The data type the CompletionStage
provides when complete.stage
- The CompletionStage
to convert.Single
that derives results from CompletionStage
.