T
- Type of items emitted.public abstract class Publisher<T>
extends java.lang.Object
PublisherSource.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 PublisherSource
.Modifier | Constructor and Description |
---|---|
protected |
Publisher()
New instance.
|
Modifier and Type | Method and Description |
---|---|
Publisher<T> |
afterCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument after Cancellable.cancel() is
called for PublisherSource.Subscription s of the returned Publisher . |
Publisher<T> |
afterFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally Runnable argument after any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for PublisherSource.Subscription s/PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
afterOnComplete(java.lang.Runnable onComplete)
Invokes the
onComplete Runnable argument after PublisherSource.Subscriber.onComplete()
is called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
afterOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument after
PublisherSource.Subscriber.onError(Throwable) is called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
afterOnNext(java.util.function.Consumer<? super T> onNext)
Invokes the
onNext Consumer argument after PublisherSource.Subscriber.onNext(Object) is
called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
afterOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
Invokes the
onSubscribe Consumer argument after
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription) is called for PublisherSource.Subscriber s of the returned
Publisher . |
Publisher<T> |
afterRequest(java.util.function.LongConsumer onRequest)
Invokes the
onRequest LongConsumer argument after
PublisherSource.Subscription.request(long) is called for PublisherSource.Subscription s of the returned Publisher . |
Publisher<T> |
afterSubscriber(java.util.function.Supplier<? extends PublisherSource.Subscriber<? super T>> subscriberSupplier)
Creates a new
PublisherSource.Subscriber (via the subscriberSupplier argument) for each new subscribe and
invokes all the PublisherSource.Subscriber methods after the PublisherSource.Subscriber s of the returned
Publisher . |
Publisher<T> |
afterSubscription(java.util.function.Supplier<? extends PublisherSource.Subscription> subscriptionSupplier)
Creates a new
PublisherSource.Subscription (via the subscriptionSupplier argument) for each new subscribe and
invokes all the PublisherSource.Subscription methods after the PublisherSource.Subscription s of the returned
Publisher . |
Publisher<T> |
beforeCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument before Cancellable.cancel() is
called for PublisherSource.Subscription s of the returned Publisher . |
Publisher<T> |
beforeFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally Runnable argument before any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for PublisherSource.Subscription s/PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
beforeOnComplete(java.lang.Runnable onComplete)
Invokes the
onComplete Runnable argument before PublisherSource.Subscriber.onComplete()
is called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
beforeOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument before
PublisherSource.Subscriber.onError(Throwable) is called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
beforeOnNext(java.util.function.Consumer<? super T> onNext)
Invokes the
onNext Consumer argument before PublisherSource.Subscriber.onNext(Object) is
called for PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
beforeOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
Invokes the
onSubscribe Consumer argument before
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription) is called for PublisherSource.Subscriber s of the returned
Publisher . |
Publisher<T> |
beforeRequest(java.util.function.LongConsumer onRequest)
Invokes the
onRequest LongConsumer argument before
PublisherSource.Subscription.request(long) is called for PublisherSource.Subscription s of the returned Publisher . |
Publisher<T> |
beforeSubscriber(java.util.function.Supplier<? extends PublisherSource.Subscriber<? super T>> subscriberSupplier)
Creates a new
PublisherSource.Subscriber (via the subscriberSupplier argument) on each call to subscribe and
invokes all the PublisherSource.Subscriber methods before the PublisherSource.Subscriber s of the returned
Publisher . |
Publisher<T> |
beforeSubscription(java.util.function.Supplier<? extends PublisherSource.Subscription> subscriptionSupplier)
Creates a new
PublisherSource.Subscription (via the subscriptionSupplier argument) on each call to
subscribe and invokes all the PublisherSource.Subscription methods before the PublisherSource.Subscription s of
the returned Publisher . |
<R> Single<R> |
collect(java.util.function.Supplier<? extends R> resultFactory,
java.util.function.BiFunction<? super R,? super T,R> collector)
Collects all items emitted by this
Publisher into a single item. |
Publisher<T> |
concat(Completable next)
|
Publisher<T> |
concat(Publisher<? extends T> next)
|
Publisher<T> |
concat(Single<? extends T> next)
|
static <T> Publisher<T> |
defer(java.util.function.Supplier<? extends Publisher<? extends T>> publisherSupplier)
Defers creation of a
Publisher till it is subscribed. |
static <T> Publisher<T> |
empty()
Creates a new
Publisher that completes when subscribed without emitting any item to its
PublisherSource.Subscriber . |
static <T> Publisher<T> |
failed(java.lang.Throwable cause)
Creates a new
Publisher that terminates its PublisherSource.Subscriber with an error without emitting any item to
it. |
Publisher<T> |
filter(java.util.function.Predicate<? super T> predicate)
Filters items emitted by this
Publisher . |
Single<T> |
firstOrElse(java.util.function.Supplier<T> defaultValueSupplier)
|
Single<T> |
firstOrError()
Ensures that this
Publisher emits exactly a single PublisherSource.Subscriber.onNext(Object) to its
PublisherSource.Subscriber . |
Completable |
flatMapCompletable(java.util.function.Function<? super T,? extends Completable> mapper)
Turns every item emitted by this
Publisher into a Completable and terminate the returned
Completable when all the intermediate Completable s have terminated successfully or any one of
them has terminated with a failure. |
Completable |
flatMapCompletable(java.util.function.Function<? super T,? extends Completable> mapper,
int maxConcurrency)
Turns every item emitted by this
Publisher into a Completable and terminate the returned
Completable when all the intermediate Completable s have terminated successfully or any one of
them has terminated with a failure. |
Completable |
flatMapCompletableDelayError(java.util.function.Function<? super T,? extends Completable> mapper)
Turns every item emitted by this
Publisher into a Completable and terminate the returned
Completable when all the intermediate Completable s have terminated. |
Completable |
flatMapCompletableDelayError(java.util.function.Function<? super T,? extends Completable> mapper,
int maxConcurrency)
Turns every item emitted by this
Publisher into a Completable and terminate the returned
Completable when all the intermediate Completable s have terminated.If any Completable
returned by mapper , terminates with an error, the returned Completable will not immediately
terminate. |
<R> Publisher<R> |
flatMapConcatIterable(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> mapper)
|
<R> Publisher<R> |
flatMapMergeSingle(java.util.function.Function<? super T,? extends Single<? extends R>> mapper)
|
<R> Publisher<R> |
flatMapMergeSingle(java.util.function.Function<? super T,? extends Single<? extends R>> mapper,
int maxConcurrency)
|
<R> Publisher<R> |
flatMapMergeSingleDelayError(java.util.function.Function<? super T,? extends Single<? extends R>> mapper)
|
<R> Publisher<R> |
flatMapMergeSingleDelayError(java.util.function.Function<? super T,? extends Single<? extends R>> mapper,
int maxConcurrency)
|
Cancellable |
forEach(java.util.function.Consumer<? super T> forEach)
|
static <T> Publisher<T> |
from(T... values)
Creates a new
Publisher that emits all values to its PublisherSource.Subscriber and then
PublisherSource.Subscriber.onComplete() . |
static <T> Publisher<T> |
from(T value)
Creates a new
Publisher that emits value to its PublisherSource.Subscriber and then
PublisherSource.Subscriber.onComplete() . |
static <T> Publisher<T> |
fromBlockingIterable(BlockingIterable<? extends T> iterable,
java.util.function.LongSupplier timeoutSupplier,
java.util.concurrent.TimeUnit unit)
Create a new
Publisher that when subscribed will get a BlockingIterator via
BlockingIterable.iterator() and emit all values to the PublisherSource.Subscriber and then
PublisherSource.Subscriber.onComplete() . |
static Publisher<byte[]> |
fromInputStream(java.io.InputStream stream)
Create a new
Publisher that when subscribed will emit all data from the InputStream to the
PublisherSource.Subscriber and then PublisherSource.Subscriber.onComplete() . |
static <T> Publisher<T> |
fromIterable(java.lang.Iterable<? extends T> iterable)
Create a new
Publisher that when subscribed will get an Iterator via Iterable.iterator()
and emit all values to the PublisherSource.Subscriber and then PublisherSource.Subscriber.onComplete() . |
<Key> Publisher<GroupedPublisher<Key,T>> |
groupBy(java.util.function.Function<? super T,? extends Key> keySelector,
int groupMaxQueueSize)
Splits items from this
Publisher into dynamically generated GroupedPublisher s. |
<Key> Publisher<GroupedPublisher<Key,T>> |
groupBy(java.util.function.Function<? super T,? extends Key> keySelector,
int groupMaxQueueSize,
int expectedGroupCountHint)
Splits items from this
Publisher into dynamically generated GroupedPublisher s. |
<Key> Publisher<GroupedPublisher<Key,T>> |
groupToMany(java.util.function.Function<? super T,? extends java.util.Iterator<? extends Key>> keySelector,
int groupMaxQueueSize)
The semantics are identical to
groupBy(Function, int) except that the keySelector can map each
data to multiple keys. |
<Key> Publisher<GroupedPublisher<Key,T>> |
groupToMany(java.util.function.Function<? super T,? extends java.util.Iterator<? extends Key>> keySelector,
int groupMaxQueueSize,
int expectedGroupCountHint)
The semantics are identical to
groupBy(Function, int) except that the keySelector can map each
data to multiple keys. |
protected abstract void |
handleSubscribe(PublisherSource.Subscriber<? super T> subscriber)
Handles a subscriber to this
Publisher . |
Publisher<T> |
idleTimeout(java.time.Duration duration)
Creates a new
Publisher that will mimic the signals of this Publisher but will terminate with a
TimeoutException if time duration elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. |
Publisher<T> |
idleTimeout(java.time.Duration duration,
Executor timeoutExecutor)
Creates a new
Publisher that will mimic the signals of this Publisher but will terminate with a
TimeoutException if time duration elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. |
Publisher<T> |
idleTimeout(long duration,
java.util.concurrent.TimeUnit unit)
Creates a new
Publisher that will mimic the signals of this Publisher but will terminate with a
TimeoutException if time duration elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. |
Publisher<T> |
idleTimeout(long duration,
java.util.concurrent.TimeUnit unit,
Executor timeoutExecutor)
Creates a new
Publisher that will mimic the signals of this Publisher but will terminate with a
TimeoutException if time duration elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. |
Completable |
ignoreElements()
Ignores all elements emitted by this
Publisher and forwards the termination signal to the returned
Completable . |
<R> Publisher<R> |
liftAsync(PublisherOperator<? super T,? extends R> operator)
This method requires advanced knowledge of building operators.
|
<R> Publisher<R> |
liftSync(PublisherOperator<? super T,? extends R> operator)
This method requires advanced knowledge of building operators.
|
<R> Single<R> |
liftSyncToSingle(PublisherToSingleOperator<? super T,? extends R> operator)
This method requires advanced knowledge of building operators.
|
<R> Publisher<R> |
map(java.util.function.Function<? super T,? extends R> mapper)
Transforms elements emitted by this
Publisher into a different type. |
Publisher<T> |
multicastToExactly(int expectedSubscribers)
Create a
Publisher that multicasts all the signals to exactly expectedSubscribers . |
Publisher<T> |
multicastToExactly(int expectedSubscribers,
int maxQueueSize)
Create a
Publisher that allows exactly expectedSubscribers subscribes. |
static <T> Publisher<T> |
never()
Creates a new
Publisher that never emits any item to its PublisherSource.Subscriber and never call any terminal
methods on it. |
Publisher<T> |
publishAndSubscribeOn(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke the following methods:
All PublisherSource.Subscriber methods.
All PublisherSource.Subscription methods.
The handleSubscribe(PublisherSource.Subscriber) method.
This method does not override preceding Executor s, if any, specified for this
Publisher . |
Publisher<T> |
publishAndSubscribeOnOverride(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke the following methods:
All PublisherSource.Subscriber methods.
All PublisherSource.Subscription methods.
The handleSubscribe(PublisherSource.Subscriber) method.
This method overrides preceding Executor s, if any, specified for this Publisher . |
Publisher<T> |
publishOn(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke all PublisherSource.Subscriber
methods. |
Publisher<T> |
publishOnOverride(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke all PublisherSource.Subscriber
methods. |
Publisher<T> |
recoverWith(java.util.function.Function<java.lang.Throwable,? extends Publisher<? 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
Publisher when it completes and the Completable returned by the supplied
IntFunction completes successfully. |
Publisher<T> |
retry(BiIntPredicate<java.lang.Throwable> shouldRetry)
|
Publisher<T> |
retryWhen(BiIntFunction<java.lang.Throwable,? extends Completable> retryWhen)
Re-subscribes to this
Publisher if an error is emitted and the Completable returned by the
supplied BiIntFunction completes successfully. |
protected void |
subscribeInternal(PublisherSource.Subscriber<? super T> subscriber)
A internal subscribe method similar to
PublisherSource.subscribe(Subscriber) which can be used by
different implementations to subscribe. |
Publisher<T> |
subscribeOn(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke the following methods:
All PublisherSource.Subscription methods.
The handleSubscribe(PublisherSource.Subscriber) method.
This method does not override preceding Executor s, if any, specified for this
Publisher . |
Publisher<T> |
subscribeOnOverride(Executor executor)
Creates a new
Publisher that will use the passed Executor to invoke the following methods:
All PublisherSource.Subscription methods.
The handleSubscribe(PublisherSource.Subscriber) method.
This method overrides preceding Executor s, if any, specified for this Publisher . |
Publisher<T> |
subscribeShareContext()
Signifies that when the returned
Publisher is subscribed to, the AsyncContext will be shared
instead of making a copy . |
Publisher<T> |
takeAtMost(long numElements)
|
Publisher<T> |
takeUntil(Completable until)
Takes elements until
Completable is terminated successfully or with failure. |
Publisher<T> |
takeWhile(java.util.function.Predicate<? super T> predicate)
Takes elements while
Predicate is true and then cancel PublisherSource.Subscription of this
Publisher once it returns false . |
java.util.concurrent.CompletionStage<java.util.Collection<T>> |
toCompletionStage()
|
<R> java.util.concurrent.CompletionStage<R> |
toCompletionStage(java.util.function.Supplier<? extends R> resultFactory,
java.util.function.BiFunction<? super R,? super T,R> reducer)
|
java.util.concurrent.Future<java.util.Collection<T>> |
toFuture()
|
<R> java.util.concurrent.Future<R> |
toFuture(java.util.function.Supplier<? extends R> resultFactory,
java.util.function.BiFunction<? super R,? super T,R> reducer)
|
java.io.InputStream |
toInputStream(java.util.function.Function<? super T,byte[]> serializer)
Subscribes to
this Publisher and converts all signals received by the PublisherSource.Subscriber to the
returned InputStream following the below rules:
PublisherSource.Subscription received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription) is used to
request more data when required. |
java.io.InputStream |
toInputStream(java.util.function.Function<? super T,byte[]> serializer,
int queueCapacity)
Subscribes to
this Publisher and converts all signals received by the PublisherSource.Subscriber to the
returned InputStream following the below rules:
PublisherSource.Subscription received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription) is used to
request more data when required. |
BlockingIterable<T> |
toIterable()
|
BlockingIterable<T> |
toIterable(int queueCapacityHint)
|
Publisher<T> |
whenCancel(java.lang.Runnable onCancel)
Invokes the
onCancel Runnable argument when Cancellable.cancel() is called for
Subscriptions of the returned Publisher . |
Publisher<T> |
whenFinally(java.lang.Runnable doFinally)
Invokes the
whenFinally Runnable argument exactly once, when any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
for PublisherSource.Subscription s/PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
whenOnComplete(java.lang.Runnable onComplete)
Invokes the
onComplete Runnable argument when PublisherSource.Subscriber.onComplete() is called for
PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
whenOnError(java.util.function.Consumer<java.lang.Throwable> onError)
Invokes the
onError Consumer argument when PublisherSource.Subscriber.onError(Throwable) is called for
PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
whenOnNext(java.util.function.Consumer<? super T> onNext)
Invokes the
onNext Consumer argument when PublisherSource.Subscriber.onNext(Object) is called for
PublisherSource.Subscriber s of the returned Publisher . |
Publisher<T> |
whenOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
Invokes the
onSubscribe Consumer argument when
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription) is called for PublisherSource.Subscriber s of the returned
Publisher . |
Publisher<T> |
whenRequest(java.util.function.LongConsumer onRequest)
Invokes the
onRequest LongConsumer argument when PublisherSource.Subscription.request(long) is called for
PublisherSource.Subscription s of the returned Publisher . |
public final <R> Publisher<R> map(java.util.function.Function<? super T,? extends R> mapper)
Publisher
into a different type.
This method provides a data transformation in sequential programming similar to:
List<R> results = ...;
for (T t : resultOfThisPublisher()) {
results.add(mapper.apply(t));
}
return results;
R
- Type of the items emitted by the returned Publisher
.mapper
- Function to transform each item emitted by this Publisher
.Publisher
that transforms elements emitted by this Publisher
into a different type.public final Publisher<T> filter(java.util.function.Predicate<? super T> predicate)
Publisher
.
This method provides a data transformation in sequential programming similar to:
List<T> results = ...;
for (T t : resultOfThisPublisher()) {
if (predicate.test(t)) {
results.add(t);
}
}
return results;
predicate
- for the filter.Publisher
that only emits the items that pass the predicate
.public final Publisher<T> recoverWith(java.util.function.Function<java.lang.Throwable,? extends Publisher<? extends T>> nextFactory)
Publisher
by using another Publisher
provided by the
passed nextFactory
.
This method provides similar capabilities to a try/catch block in sequential programming:
List<T> results;
try {
results = resultOfThisPublisher();
} catch (Throwable cause) {
// Note that nextFactory returning a error Publisher is like re-throwing (nextFactory shouldn't throw).
results = nextFactory.apply(cause);
}
return results;
nextFactory
- Returns the next Publisher
, when this Publisher
emits an error.Publisher
that recovers from an error from this Publisher
by using another
Publisher
provided by the passed nextFactory
.public final <R> Publisher<R> flatMapMergeSingle(java.util.function.Function<? super T,? extends Single<? extends R>> mapper)
Publisher
into a Single
and emits the items emitted by each of
those Single
s.
To control the amount of concurrent processing done by this operator see
flatMapMergeSingle(Function, int)
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
ExecutorService e = ...;
List<Future<R>> futures = ...; // assume this is thread safe
for (T t : resultOfThisPublisher()) {
// Note that flatMap process results in parallel.
futures.add(e.submit(() -> {
return mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
}));
}
List<R> results = new ArrayList<>(futures.size());
// This is an approximation, this operator does not provide any ordering guarantees for the results.
for (Future<R> future : futures) {
R r = future.get(); // Throws if the processing for this item failed.
results.add(r);
}
return results;
R
- Type of items emitted by the returned Publisher
.mapper
- Function to convert each item emitted by this Publisher
into a Single
.Publisher
that emits all items emitted by each single produced by mapper
.flatMapMergeSingle(Function, int)
public final <R> Publisher<R> flatMapMergeSingle(java.util.function.Function<? super T,? extends Single<? extends R>> mapper, int maxConcurrency)
Publisher
into a Single
and emits the items emitted by each of
those Single
s.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
ExecutorService e = ...;
List<Future<R>> futures = ...; // assume this is thread safe
for (T t : resultOfThisPublisher()) {
// Note that flatMap process results in parallel.
futures.add(e.submit(() -> {
return mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
}));
}
List<R> results = new ArrayList<>(futures.size());
// This is an approximation, this operator does not provide any ordering guarantees for the results.
for (Future<R> future : futures) {
R r = future.get(); // Throws if the processing for this item failed.
results.add(r);
}
return results;
R
- Type of items emitted by the returned Publisher
.mapper
- Function to convert each item emitted by this Publisher
into a Single
.maxConcurrency
- Maximum active Single
s at any time.
Even if the number of items requested by a PublisherSource.Subscriber
is more than this number, this will never request
more than this number at any point.Publisher
that emits all items emitted by each single produced by mapper
.public final <R> Publisher<R> flatMapMergeSingleDelayError(java.util.function.Function<? super T,? extends Single<? extends R>> mapper)
Publisher
into a Single
and emits the items emitted by each of
those Single
s. This is the same as flatMapMergeSingle(Function, int)
just that if any
Single
returned by mapper
, terminates with an error, the returned Publisher
will not
immediately terminate. Instead, it will wait for this Publisher
and all Single
s to terminate and
then terminate the returned Publisher
with all errors emitted by the Single
s produced by the
mapper
.
To control the amount of concurrent processing done by this operator see
flatMapMergeSingleDelayError(Function, int)
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
Executor e = ...;
List<T> tResults = resultOfThisPublisher();
List<R> rResults = ...; // assume this is thread safe
List<Throwable> errors = ...; // assume this is thread safe
CountDownLatch latch = new CountDownLatch(tResults.size());
for (T t : tResults) {
// Note that flatMap process results in parallel.
e.execute(() -> {
try {
R r = mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
rResults.add(r);
} catch (Throwable cause) {
errors.add(cause); // Asynchronous error is flatten into an error by this operator.
} finally {
latch.countdown();
}
});
}
latch.await();
if (errors.isEmpty()) {
return rResults;
}
createAndThrowACompositeException(errors);
R
- Type of items emitted by the returned Publisher
.mapper
- Function to convert each item emitted by this Publisher
into a Single
.Publisher
that emits all items emitted by each single produced by mapper
.flatMapMergeSingleDelayError(Function, int)
public final <R> Publisher<R> flatMapMergeSingleDelayError(java.util.function.Function<? super T,? extends Single<? extends R>> mapper, int maxConcurrency)
Publisher
into a Single
and emits the items emitted by each of
those Single
s. This is the same as flatMapMergeSingle(Function, int)
just that if any
Single
returned by mapper
, terminates with an error, the returned Publisher
will not
immediately terminate. Instead, it will wait for this Publisher
and all Single
s to terminate and
then terminate the returned Publisher
with all errors emitted by the Single
s produced by the
mapper
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
Executor e = ...;
List<T> tResults = resultOfThisPublisher();
List<R> rResults = ...; // assume this is thread safe
List<Throwable> errors = ...; // assume this is thread safe
CountDownLatch latch = new CountDownLatch(tResults.size());
for (T t : tResults) {
// Note that flatMap process results in parallel.
e.execute(() -> {
try {
R r = mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
rResults.add(r);
} catch (Throwable cause) {
errors.add(cause); // Asynchronous error is flatten into an error by this operator.
} finally {
latch.countdown();
}
});
}
latch.await();
if (errors.isEmpty()) {
return rResults;
}
createAndThrowACompositeException(errors);
R
- Type of items emitted by the returned Publisher
.mapper
- Function to convert each item emitted by this Publisher
into a Single
.maxConcurrency
- Maximum active Single
s at any time.
Even if the number of items requested by a PublisherSource.Subscriber
is more than this number,
this will never request more than this number at any point.Publisher
that emits all items emitted by each single produced by mapper
.public final Completable flatMapCompletable(java.util.function.Function<? super T,? extends Completable> mapper)
Publisher
into a Completable
and terminate the returned
Completable
when all the intermediate Completable
s have terminated successfully or any one of
them has terminated with a failure.
If the returned Completable
should wait for the termination of all remaining Completable
s when
any one of them terminates with a failure, flatMapCompletableDelayError(Function)
should be used.
To control the amount of concurrent processing done by this operator see
flatMapCompletable(Function, int)
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
ExecutorService e = ...;
List<Future<Void>> futures = ...; // assume this is thread safe
for (T t : resultOfThisPublisher()) {
// Note that flatMap process results in parallel.
futures.add(e.submit(() -> {
return mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
}));
}
// This is an approximation, this operator does not provide any ordering guarantees for the results.
for (Future<R> future : futures) {
future.get(); // Throws if the processing for this item failed.
}
mapper
- Function to convert each item emitted by this Publisher
into a Completable
.Completable
that terminates successfully if all the intermediate Completable
s have
terminated successfully or any one of them has terminated with a failure.flatMapCompletable(Function, int)
,
flatMapCompletableDelayError(Function)
public final Completable flatMapCompletable(java.util.function.Function<? super T,? extends Completable> mapper, int maxConcurrency)
Publisher
into a Completable
and terminate the returned
Completable
when all the intermediate Completable
s have terminated successfully or any one of
them has terminated with a failure.
If the returned Completable
should wait for the termination of all remaining Completable
s when
any one of them terminates with a failure, flatMapCompletableDelayError(Function)
should be used.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
ExecutorService e = ...;
List<Future<Void>> futures = ...; // assume this is thread safe
for (T t : resultOfThisPublisher()) {
// Note that flatMap process results in parallel.
futures.add(e.submit(() -> {
return mapper.apply(t); // Asynchronous result is flatten into a value by this operator.
}));
}
// This is an approximation, this operator does not provide any ordering guarantees for the results.
for (Future<R> future : futures) {
future.get(); // Throws if the processing for this item failed.
}
mapper
- Function to convert each item emitted by this Publisher
into a Completable
.maxConcurrency
- Maximum active Completable
s at any time.Completable
that terminates successfully if all the intermediate Completable
s have
terminated successfully or any one of them has terminated with a failure.flatMapCompletable(Function)
,
flatMapCompletableDelayError(Function, int)
public final Completable flatMapCompletableDelayError(java.util.function.Function<? super T,? extends Completable> mapper)
Publisher
into a Completable
and terminate the returned
Completable
when all the intermediate Completable
s have terminated. If any Completable
returned by mapper
, terminates with an error, the returned Completable
will not immediately
terminate. Instead, it will wait for this Publisher
and all Completable
s to terminate and then
terminate the returned Completable
with all errors emitted by the Completable
s produced by the
mapper
.
To control the amount of concurrent processing done by this operator see
flatMapCompletableDelayError(Function, int)
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
Executor e = ...;
List<Throwable> errors = ...; // assume this is thread safe
CountDownLatch latch = new CountDownLatch(tResults.size());
for (T t : tResults) {
// Note that flatMap process results in parallel.
e.execute(() -> {
try {
mapper.apply(t); // Asynchronous result is flattened by this operator.
} catch (Throwable cause) {
errors.add(cause); // Asynchronous error is flatten into an error by this operator.
} finally {
latch.countdown();
}
});
}
latch.await();
if (!errors.isEmpty()) {
createAndThrowACompositeException(errors);
}
mapper
- Function to convert each item emitted by this Publisher
into a Completable
.Completable
that terminates successfully if all the intermediate Completable
s have
terminated successfully or any one of them has terminated with a failure.flatMapMergeSingleDelayError(Function, int)
public final Completable flatMapCompletableDelayError(java.util.function.Function<? super T,? extends Completable> mapper, int maxConcurrency)
Publisher
into a Completable
and terminate the returned
Completable
when all the intermediate Completable
s have terminated.If any Completable
returned by mapper
, terminates with an error, the returned Completable
will not immediately
terminate. Instead, it will wait for this Publisher
and all Completable
s to terminate and then
terminate the returned Completable
with all errors emitted by the Completable
s produced by the
mapper
.
This method is similar to map(Function)
but the result is asynchronous, and provides a data
transformation in sequential programming similar to:
Executor e = ...;
List<Throwable> errors = ...; // assume this is thread safe
CountDownLatch latch = new CountDownLatch(tResults.size());
for (T t : tResults) {
// Note that flatMap process results in parallel.
e.execute(() -> {
try {
mapper.apply(t); // Asynchronous result is flattened by this operator.
} catch (Throwable cause) {
errors.add(cause); // Asynchronous error is flatten into an error by this operator.
} finally {
latch.countdown();
}
});
}
latch.await();
if (!errors.isEmpty()) {
createAndThrowACompositeException(errors);
}
mapper
- Function to convert each item emitted by this Publisher
into a Completable
.maxConcurrency
- Maximum active Completable
s at any time.Completable
that terminates successfully if all the intermediate Completable
s have
terminated successfully or any one of them has terminated with a failure.flatMapMergeSingleDelayError(Function, int)
public final <R> Publisher<R> flatMapConcatIterable(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> mapper)
Publisher
that flattens each element returned by the Iterable.iterator()
from
mapper
.
The mapper Function
will only be called when the previously returned Iterator
has returned
false
from Iterator.hasNext()
.
This method provides similar capabilities as expanding each result into a collection and concatenating each collection in sequential programming:
List<R> results = ...;
for (T t : resultOfThisPublisher()) {
Iterable<? extends R> itr = mapper.apply(t);
itr.forEach(results::add);
}
return results;
R
- The elements returned by the Iterable
.mapper
- A Function
that returns an Iterable
for each element.Publisher
that flattens each element returned by the Iterable.iterator()
from
mapper
. The results will be sequential for each Iterator
, and overall for all calls to
Iterable.iterator()
public final Publisher<T> whenOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
onSubscribe
Consumer
argument when
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for PublisherSource.Subscriber
s of the returned
Publisher
.
The order in which onSubscribe
will be invoked relative to
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is undefined. If you need strict ordering see
beforeOnSubscribe(Consumer)
and afterOnSubscribe(Consumer)
.
onSubscribe
- Invoked when PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.beforeOnNext(Consumer)
,
afterOnNext(Consumer)
public final Publisher<T> whenOnNext(java.util.function.Consumer<? super T> onNext)
onNext
Consumer
argument when PublisherSource.Subscriber.onNext(Object)
is called for
PublisherSource.Subscriber
s of the returned Publisher
.
The order in which onNext
will be invoked relative to PublisherSource.Subscriber.onNext(Object)
is undefined. If
you need strict ordering see beforeOnNext(Consumer)
and afterOnNext(Consumer)
.
From a sequential programming point of view this method is roughly equivalent to the following:
for (T t: resultOfThisPublisher()) {
// NOTE: The order of operations here is not guaranteed by this method!
processNext(t);
onNext.accept(t);
}
onNext
- Invoked when PublisherSource.Subscriber.onNext(Object)
is called for PublisherSource.Subscriber
s of the returned
Publisher
. MUST NOT throw.Publisher
.beforeOnNext(Consumer)
,
afterOnNext(Consumer)
public final Publisher<T> whenOnComplete(java.lang.Runnable onComplete)
onComplete
Runnable
argument when PublisherSource.Subscriber.onComplete()
is called for
PublisherSource.Subscriber
s of the returned Publisher
.
The order in which onComplete
will be invoked relative to PublisherSource.Subscriber.onComplete()
is undefined.
If you need strict ordering see beforeOnComplete(Runnable)
and afterOnComplete(Runnable)
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
// NOTE: The order of operations here is not guaranteed by this method!
onSuccess.accept(results);
nextOperation(results);
onComplete
- Invoked when PublisherSource.Subscriber.onComplete()
is called for PublisherSource.Subscriber
s of the
returned Publisher
. MUST NOT throw.Publisher
.beforeOnComplete(Runnable)
,
afterOnComplete(Runnable)
public final Publisher<T> whenOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument when PublisherSource.Subscriber.onError(Throwable)
is called for
PublisherSource.Subscriber
s of the returned Publisher
.
The order in which onError
will be invoked relative to PublisherSource.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 {
List<T> results = resultOfThisPublisher();
} catch (Throwable cause) {
// NOTE: The order of operations here is not guaranteed by this method!
nextOperation(cause);
onError.accept(cause);
}
onError
- Invoked before PublisherSource.Subscriber.onError(Throwable)
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.beforeOnError(Consumer)
,
afterOnError(Consumer)
public final Publisher<T> whenFinally(java.lang.Runnable doFinally)
whenFinally
Runnable
argument exactly once, when any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
.
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 {
List<T> results = resultOfThisPublisher();
} 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:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.afterFinally(Runnable)
,
beforeFinally(Runnable)
public final Publisher<T> whenRequest(java.util.function.LongConsumer onRequest)
onRequest
LongConsumer
argument when PublisherSource.Subscription.request(long)
is called for
PublisherSource.Subscription
s of the returned Publisher
.onRequest
- Invoked when PublisherSource.Subscription.request(long)
is called for PublisherSource.Subscription
s of the
returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> whenCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument when Cancellable.cancel()
is called for
Subscriptions of the returned Publisher
.
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
Publisher
. MUST NOT throw.Publisher
.beforeCancel(Runnable)
,
afterCancel(Runnable)
public final Publisher<T> idleTimeout(long duration, java.util.concurrent.TimeUnit unit)
Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. The timer starts when the returned Publisher
is subscribed.
In the event of timeout any PublisherSource.Subscription
from
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
will be cancelled
and
the associated PublisherSource.Subscriber
will be terminated
.
duration
- The time duration which is allowed to elapse between PublisherSource.Subscriber.onNext(Object)
calls.unit
- The units for duration
.Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between PublisherSource.Subscriber.onNext(Object)
calls.idleTimeout(long, TimeUnit, Executor)
public final Publisher<T> idleTimeout(java.time.Duration duration)
Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. The timer starts when the returned Publisher
is subscribed.
In the event of timeout any PublisherSource.Subscription
from
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
will be cancelled
and the associated PublisherSource.Subscriber
will be terminated
.
duration
- The time duration which is allowed to elapse between PublisherSource.Subscriber.onNext(Object)
calls.Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between PublisherSource.Subscriber.onNext(Object)
calls.idleTimeout(long, TimeUnit, Executor)
public final Publisher<T> idleTimeout(long duration, java.util.concurrent.TimeUnit unit, Executor timeoutExecutor)
Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. The timer starts when the returned Publisher
is subscribed.
In the event of timeout any PublisherSource.Subscription
from
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
will be cancelled
and
the associated PublisherSource.Subscriber
will be terminated
.
duration
- The time duration which is allowed to elapse between PublisherSource.Subscriber.onNext(Object)
calls.unit
- The units for duration
.timeoutExecutor
- The Executor
to use for managing the timer notifications.Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between PublisherSource.Subscriber.onNext(Object)
calls.public final Publisher<T> idleTimeout(java.time.Duration duration, Executor timeoutExecutor)
Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between adjacent PublisherSource.Subscriber.onNext(Object)
calls. The timer starts when the returned Publisher
is subscribed.
In the event of timeout any PublisherSource.Subscription
from
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
will be cancelled
and
the associated PublisherSource.Subscriber
will be terminated
.
duration
- The time duration which is allowed to elapse between PublisherSource.Subscriber.onNext(Object)
calls.timeoutExecutor
- The Executor
to use for managing the timer notifications.Publisher
that will mimic the signals of this Publisher
but will terminate with a
TimeoutException
if time duration
elapses between PublisherSource.Subscriber.onNext(Object)
calls.public final Publisher<T> concat(Publisher<? extends T> next)
next
Publisher
after this
Publisher
terminates
successfully.
This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = resultOfThisPublisher();
results.addAll(resultOfPublisher(next));
return results;
public final Publisher<T> concat(Single<? extends T> next)
next
Single
after this
Publisher
terminates
successfully.
This method provides a means to sequence the execution of two asynchronous sources and in sequential programming is similar to:
List<T> results = resultOfThisPublisher();
results.add(resultOfSingle(next));
return results;
public final Publisher<T> concat(Completable next)
next
Completable
after this
Publisher
terminates
successfully. Any error from this
Publisher
and next
Completable
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 = resultOfThisPublisher();
resultOfCompletable(next);
return results;
next
- Completable
to wait for completion after this
Publisher
terminates
successfully.Publisher
that emits all items from this Publisher
and then awaits successful
completion of next
Completable
.public final Publisher<T> retry(BiIntPredicate<java.lang.Throwable> shouldRetry)
Publisher
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 List<T> execute() {
List<T> results = ...;
return execute(0, results);
}
private List<T> execute(int attempts, List<T> results) {
try {
Iterator<T> itr = resultOfThisPublisher();
while (itr.hasNext()) {
T t = itr.next(); // Any iteration with the Iterator may throw
results.add(t);
}
return results;
} catch (Throwable cause) {
if (shouldRetry.apply(attempts + 1, cause)) {
return execute(attempts + 1, results);
} else {
throw cause;
}
}
}
shouldRetry
- BiIntPredicate
that given the retry count and the most recent Throwable
emitted from this
Publisher
determines if the operation should be retried.Publisher
that emits all items from this Publisher
and re-subscribes if an error is
emitted if the passed BiIntPredicate
returned true
.public final Publisher<T> retryWhen(BiIntFunction<java.lang.Throwable,? extends Completable> retryWhen)
Publisher
if an error is emitted and the Completable
returned by the
supplied BiIntFunction
completes successfully. If the returned Completable
emits an error, the
returned Publisher
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 List<T> execute() {
List<T> results = ...;
return execute(0, results);
}
private List<T> execute(int attempts, List<T> results) {
try {
Iterator<T> itr = resultOfThisPublisher();
while (itr.hasNext()) {
T t = itr.next(); // Any iteration with the Iterator may throw
results.add(t);
}
return results;
} catch (Throwable cause) {
try {
shouldRetry.apply(attempts + 1, cause); // Either throws or completes normally
execute(attempts + 1, results);
} catch (Throwable ignored) {
throw cause;
}
}
}
retryWhen
- BiIntFunction
that given the retry count and the most recent Throwable
emitted
from this Publisher
returns a Completable
. If this Completable
emits an error, that error
is emitted from the returned Publisher
, otherwise, original Publisher
is re-subscribed when this
Completable
completes.Publisher
that emits all items from this Publisher
and re-subscribes if an error is
emitted and Completable
returned by BiIntFunction
completes successfully.public final Publisher<T> repeat(java.util.function.IntPredicate shouldRepeat)
Publisher
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.addAll(resultOfThisPublisher());
} 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 Publisher
and re-subscribes when it completes
if the passed IntPredicate
returns true
.public final Publisher<T> repeatWhen(java.util.function.IntFunction<? extends Completable> repeatWhen)
Publisher
when it completes and the Completable
returned by the supplied
IntFunction
completes successfully. If the returned Completable
emits an error, the returned
Publisher
is completed.
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.addAll(resultOfThisPublisher());
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 Publisher
is
re-subscribed when this Completable
completes.Publisher
that emits all items from this Publisher
and re-subscribes if an error is
emitted and Completable
returned by IntFunction
completes successfully.public final Publisher<T> takeAtMost(long numElements)
numElements
elements from this
Publisher
.
If no terminal event is received before receiving numElements
elements, PublisherSource.Subscription
for the
PublisherSource.Subscriber
is cancelled.
This method provides a means to take a limited number of results from this Publisher
and in sequential
programming is similar to:
List<T> results = ...;
int i = 0;
for (T t : resultOfThisPublisher()) {
if (++i > numElements) {
break;
}
results.add(t);
}
return results;
numElements
- Number of elements to take.Publisher
that emits at most numElements
elements from this
Publisher
.public final Publisher<T> takeWhile(java.util.function.Predicate<? super T> predicate)
Predicate
is true
and then cancel PublisherSource.Subscription
of this
Publisher
once it returns false
.
This method provides a means to take a limited number of results from this Publisher
and in sequential
programming is similar to:
List<T> results = ...;
for (T t : resultOfThisPublisher()) {
if (!predicate.test(result)) {
break;
}
results.add(t);
}
return results;
predicate
- Predicate
that is checked before emitting any item to a PublisherSource.Subscriber
.
If this predicate returns true
, that item is emitted, else PublisherSource.Subscription
is cancelled.Publisher
that only emits the items as long as the Predicate.test(Object)
method
returns true
.public final Publisher<T> takeUntil(Completable until)
Completable
is terminated successfully or with failure.
This method provides a means to take a limited number of results from this Publisher
and in sequential
programming is similar to:
List<T> results = ...;
for (T t : resultOfThisPublisher()) {
if (isCompleted(until)) {
break;
}
takeResults.add(t);
}
return results;
until
- Completable
, termination of which, terminates the returned Publisher
.Publisher
that only emits the items till until
Completable
is completed.public final <Key> Publisher<GroupedPublisher<Key,T>> groupBy(java.util.function.Function<? super T,? extends Key> keySelector, int groupMaxQueueSize)
Publisher
into dynamically generated GroupedPublisher
s.
Item to group association is done by keySelector
Function
. If the selector selects a key which is
previously seen and its associated PublisherSource.Subscriber
has not yet cancelled its PublisherSource.Subscription
, this item
is sent to that PublisherSource.Subscriber
. Otherwise a new GroupedPublisher
is created and emitted from the
returned Publisher
.
PublisherSource.Subscriber
s (for multiple GroupedPublisher
s) request items individually from this
Publisher
. Since, there is no way for a PublisherSource.Subscriber
to only request elements for its group,
elements requested by one group may end up producing items for a different group, which has not yet requested
enough. This will cause items to be queued per group which can not be emitted due to lack of demand. This queue
size can be controlled with the maxQueuePerGroup
argument.
PublisherSource.Subscriber
of the returned Publisher
cancels its PublisherSource.Subscription
, then all active
GroupedPublisher
s will be terminated with an error and the PublisherSource.Subscription
to this Publisher
will be cancelled.
PublisherSource.Subscriber
s of individual GroupedPublisher
s can cancel their PublisherSource.Subscription
s at any point.
If any new item is emitted for the cancelled GroupedPublisher
, a new GroupedPublisher
will be
emitted from the returned Publisher
. Any queued items for a cancelled PublisherSource.Subscriber
for a
GroupedPublisher
will be discarded and hence will not be emitted if the same GroupedPublisher
is
emitted again.
In sequential programming this is similar to the following:
Map<Key, List<T>> results = ...;
for (T t : resultOfThisPublisher()) {
Key k = keySelector.apply(result);
List<T> v = results.get(k);
if (v == null) {
v = // new List
results.put(k, v);
}
v.add(result);
}
return results;
Key
- Type of GroupedPublisher
keys.keySelector
- Function
to assign an item emitted by this Publisher
to a
GroupedPublisher
.groupMaxQueueSize
- Maximum number of new groups that will be queued due to the PublisherSource.Subscriber
of the
Publisher
returned from this method not requesting enough via PublisherSource.Subscription.request(long)
.Publisher
that emits GroupedPublisher
s for new key
s as emitted by
keySelector
Function
.public final <Key> Publisher<GroupedPublisher<Key,T>> groupBy(java.util.function.Function<? super T,? extends Key> keySelector, int groupMaxQueueSize, int expectedGroupCountHint)
Publisher
into dynamically generated GroupedPublisher
s. Item to group
association is done by keySelector
Function
. If the selector selects a key which is previously
seen and its associated PublisherSource.Subscriber
has not yet cancelled its PublisherSource.Subscription
, this item is sent to
that PublisherSource.Subscriber
. Otherwise a new GroupedPublisher
is created and emitted from the returned
Publisher
.
PublisherSource.Subscriber
s (for multiple GroupedPublisher
s) request items individually from this
Publisher
. Since, there is no way for a PublisherSource.Subscriber
to only request elements for its group,
elements requested by one group may end up producing items for a different group, which has not yet requested
enough. This will cause items to be queued per group which can not be emitted due to lack of demand. This queue
size can be controlled with the maxQueuePerGroup
argument.
PublisherSource.Subscriber
of the returned Publisher
cancels its PublisherSource.Subscription
, then all active
GroupedPublisher
s will be terminated with an error and the PublisherSource.Subscription
to this Publisher
will be cancelled.
PublisherSource.Subscriber
s of individual GroupedPublisher
s can cancel their PublisherSource.Subscription
s at any point.
If any new item is emitted for the cancelled GroupedPublisher
, a new GroupedPublisher
will be
emitted from the returned Publisher
. Any queued items for a cancelled PublisherSource.Subscriber
for a
GroupedPublisher
will be discarded and hence will not be emitted if the same GroupedPublisher
is
emitted again.
In sequential programming this is similar to the following:
Map<Key, List<T>> results = ...;
for (T t : resultOfThisPublisher()) {
Key k = keySelector.apply(result);
List<T> v = results.get(k);
if (v == null) {
v = // new List
results.put(k, v);
}
v.add(result);
}
return results;
Key
- Type of GroupedPublisher
keys.keySelector
- Function
to assign an item emitted by this Publisher
to a
GroupedPublisher
.groupMaxQueueSize
- Maximum number of new groups that will be queued due to the PublisherSource.Subscriber
of the
Publisher
returned from this method not requesting enough via PublisherSource.Subscription.request(long)
.expectedGroupCountHint
- Expected number of groups that would be emitted by this
Publisher
.
This is just a hint for internal data structures and does not have to be precise.Publisher
that emits GroupedPublisher
s for new key
s as emitted by
keySelector
Function
.public final <Key> Publisher<GroupedPublisher<Key,T>> groupToMany(java.util.function.Function<? super T,? extends java.util.Iterator<? extends Key>> keySelector, int groupMaxQueueSize)
groupBy(Function, int)
except that the keySelector
can map each
data to multiple keys.
In sequential programming this is similar to the following:
Map<Key, List<T>> results = ...;
for (T t : resultOfThisPublisher()) {
Iterator<Key> keys = keySelector.apply(result);
for (Key key : keys) {
List<T> v = results.get(key);
if (v == null) {
v = // new List
results.put(key, v);
}
v.add(result);
}
}
return results;
Key
- Type of GroupedPublisher
keys.keySelector
- Function
to assign an item emitted by this Publisher
to multiple
GroupedPublisher
s.groupMaxQueueSize
- Maximum number of new groups that will be queued due to the PublisherSource.Subscriber
of the
Publisher
returned from this method not requesting enough via PublisherSource.Subscription.request(long)
.Publisher
that emits GroupedPublisher
s for new key
s as emitted by
keySelector
Function
.groupBy(Function, int)
public final <Key> Publisher<GroupedPublisher<Key,T>> groupToMany(java.util.function.Function<? super T,? extends java.util.Iterator<? extends Key>> keySelector, int groupMaxQueueSize, int expectedGroupCountHint)
groupBy(Function, int)
except that the keySelector
can map each
data to multiple keys.
In sequential programming this is similar to the following:
Map<Key, List<T>> results = ...;
for (T t : resultOfThisPublisher()) {
Iterator<Key> keys = keySelector.apply(result);
for (Key key : keys) {
List<T> v = results.get(key);
if (v == null) {
v = // new List
results.put(key, v);
}
v.add(result);
}
}
return results;
Key
- Type of GroupedPublisher
keys.keySelector
- Function
to assign an item emitted by this Publisher
to multiple
GroupedPublisher
s.groupMaxQueueSize
- Maximum number of new groups that will be queued due to the PublisherSource.Subscriber
of the
Publisher
returned from this method not requesting enough via PublisherSource.Subscription.request(long)
.expectedGroupCountHint
- Expected number of groups that would be emitted by this
Publisher
.
This is just a hint for internal data structures and does not have to be precise.Publisher
that emits GroupedPublisher
s for new key
s as emitted by
keySelector
Function
.groupBy(Function, int)
public final Publisher<T> multicastToExactly(int expectedSubscribers)
Publisher
that multicasts all the signals to exactly expectedSubscribers
.
Depending on PublisherSource.Subscription.request(long)
demand it is possible that data maybe queued before being
delivered to each PublisherSource.Subscriber
! For example if there are 2 PublisherSource.Subscriber
s and the first calls
request(10)
, and the second only calls
request(1)
, then 9 elements will be queued to deliver to second when more
PublisherSource.Subscription.request(long)
demand is made.
In sequential programming this is similar to the following:
List<T> results = resultOfThisPublisher();
List<List<T>> multiResults = ...;
for (int i = 0; i < expectedSubscribers; ++i) {
multiResults.add(results);
}
return multiResults;
public final Publisher<T> multicastToExactly(int expectedSubscribers, int maxQueueSize)
Publisher
that allows exactly expectedSubscribers
subscribes.
The events from this Publisher
object will be delivered to each
PublisherSource.Subscriber
.
Depending on PublisherSource.Subscription.request(long)
demand it is possible that data maybe queued before being
delivered to each PublisherSource.Subscriber
! For example if there are 2 PublisherSource.Subscriber
s and the first calls
request(10)
, and the second only calls
request(10)
, then 9 elements will be queued to deliver to second when more
PublisherSource.Subscription.request(long)
demand is made.
In sequential programming this is similar to the following:
List<T> results = resultOfThisPublisher();
List<List<T>> multiResults = ...;
for (int i = 0; i < expectedSubscribers; ++i) {
multiResults.add(results);
}
return multiResults;
expectedSubscribers
- The number of expected subscribe calls required on the returned Publisher
before subscribing to this Publisher
.maxQueueSize
- The maximum number of PublisherSource.Subscriber.onNext(Object)
events that will be queued if there
is no demand for data before the PublisherSource.Subscriber
will be discarded.Publisher
that allows exactly expectedSubscribers
subscribes.public final Publisher<T> beforeOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
onSubscribe
Consumer
argument before
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for PublisherSource.Subscriber
s of the returned
Publisher
.onSubscribe
- Invoked before
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for PublisherSource.Subscriber
s of the returned
Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeOnNext(java.util.function.Consumer<? super T> onNext)
onNext
Consumer
argument before PublisherSource.Subscriber.onNext(Object)
is
called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
for (T result : results) {
onNext.accept(result);
}
nextOperation(results);
onNext
- Invoked before PublisherSource.Subscriber.onNext(Object)
is called for PublisherSource.Subscriber
s
of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument before
PublisherSource.Subscriber.onError(Throwable)
is called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
try {
List<T> results = resultOfThisPublisher();
} catch (Throwable cause) {
onError.accept(cause);
nextOperation(cause);
}
onError
- Invoked before PublisherSource.Subscriber.onError(Throwable)
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeOnComplete(java.lang.Runnable onComplete)
onComplete
Runnable
argument before PublisherSource.Subscriber.onComplete()
is called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
onComplete.run();
nextOperation(results);
onComplete
- Invoked before PublisherSource.Subscriber.onComplete()
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeRequest(java.util.function.LongConsumer onRequest)
onRequest
LongConsumer
argument before
PublisherSource.Subscription.request(long)
is called for PublisherSource.Subscription
s of the returned Publisher
.onRequest
- Invoked before PublisherSource.Subscription.request(long)
is called for
PublisherSource.Subscription
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument before Cancellable.cancel()
is
called for PublisherSource.Subscription
s of the returned Publisher
.onCancel
- Invoked before Cancellable.cancel()
is called for
PublisherSource.Subscription
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeFinally(java.lang.Runnable doFinally)
whenFinally
Runnable
argument before any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
try {
List<T> results = resultOfThisPublisher();
} finally {
whenFinally.run();
nextOperation(); // Maybe notifying of cancellation, or termination
}
doFinally
- Invoked before any of the following terminal methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> beforeSubscriber(java.util.function.Supplier<? extends PublisherSource.Subscriber<? super T>> subscriberSupplier)
PublisherSource.Subscriber
(via the subscriberSupplier
argument) on each call to subscribe and
invokes all the PublisherSource.Subscriber
methods before the PublisherSource.Subscriber
s of the returned
Publisher
.subscriberSupplier
- Creates a new PublisherSource.Subscriber
on each call to subscribe and invokes all the
PublisherSource.Subscriber
methods before the PublisherSource.Subscriber
s of the returned Publisher
.
PublisherSource.Subscriber
methods MUST NOT throw.Publisher
.public final Publisher<T> beforeSubscription(java.util.function.Supplier<? extends PublisherSource.Subscription> subscriptionSupplier)
PublisherSource.Subscription
(via the subscriptionSupplier
argument) on each call to
subscribe and invokes all the PublisherSource.Subscription
methods before the PublisherSource.Subscription
s of
the returned Publisher
.subscriptionSupplier
- Creates a new PublisherSource.Subscription
on each call to subscribe and invokes all the
PublisherSource.Subscription
methods before the PublisherSource.Subscription
s of the returned
Publisher
. PublisherSource.Subscription
methods MUST NOT throw.Publisher
.public final Publisher<T> afterOnSubscribe(java.util.function.Consumer<? super PublisherSource.Subscription> onSubscribe)
onSubscribe
Consumer
argument after
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for PublisherSource.Subscriber
s of the returned
Publisher
.onSubscribe
- Invoked after
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterOnNext(java.util.function.Consumer<? super T> onNext)
onNext
Consumer
argument after PublisherSource.Subscriber.onNext(Object)
is
called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
nextOperation(results);
for (T result : results) {
onNext.accept(result);
}
onNext
- Invoked after PublisherSource.Subscriber.onNext(Object)
is called for PublisherSource.Subscriber
s
of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterOnError(java.util.function.Consumer<java.lang.Throwable> onError)
onError
Consumer
argument after
PublisherSource.Subscriber.onError(Throwable)
is called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
try {
List<T> results = resultOfThisPublisher();
} catch (Throwable cause) {
nextOperation(cause);
onError.accept(cause);
}
onError
- Invoked after PublisherSource.Subscriber.onError(Throwable)
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterOnComplete(java.lang.Runnable onComplete)
onComplete
Runnable
argument after PublisherSource.Subscriber.onComplete()
is called for PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
nextOperation(results);
onComplete.run();
onComplete
- Invoked after PublisherSource.Subscriber.onComplete()
is called for
PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterRequest(java.util.function.LongConsumer onRequest)
onRequest
LongConsumer
argument after
PublisherSource.Subscription.request(long)
is called for PublisherSource.Subscription
s of the returned Publisher
.onRequest
- Invoked after PublisherSource.Subscription.request(long)
is called for
PublisherSource.Subscription
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterCancel(java.lang.Runnable onCancel)
onCancel
Runnable
argument after Cancellable.cancel()
is
called for PublisherSource.Subscription
s of the returned Publisher
.onCancel
- Invoked after Cancellable.cancel()
is called for PublisherSource.Subscription
s
of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterFinally(java.lang.Runnable doFinally)
whenFinally
Runnable
argument after any of the following terminal
methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
.
From a sequential programming point of view this method is roughly equivalent to the following:
try {
List<T> results = resultOfThisPublisher();
} finally {
nextOperation(); // Maybe notifying of cancellation, or termination
whenFinally.run();
}
doFinally
- Invoked after any of the following terminal methods are called:
PublisherSource.Subscriber.onComplete()
PublisherSource.Subscriber.onError(Throwable)
Cancellable.cancel()
PublisherSource.Subscription
s/PublisherSource.Subscriber
s of the returned Publisher
. MUST NOT throw.Publisher
.public final Publisher<T> afterSubscriber(java.util.function.Supplier<? extends PublisherSource.Subscriber<? super T>> subscriberSupplier)
PublisherSource.Subscriber
(via the subscriberSupplier
argument) for each new subscribe and
invokes all the PublisherSource.Subscriber
methods after the PublisherSource.Subscriber
s of the returned
Publisher
.subscriberSupplier
- Creates a new PublisherSource.Subscriber
for each new subscribe and invokes all the
PublisherSource.Subscriber
methods after the PublisherSource.Subscriber
s of the returned Publisher
.
PublisherSource.Subscriber
methods MUST NOT throw.Publisher
.public final Publisher<T> afterSubscription(java.util.function.Supplier<? extends PublisherSource.Subscription> subscriptionSupplier)
PublisherSource.Subscription
(via the subscriptionSupplier
argument) for each new subscribe and
invokes all the PublisherSource.Subscription
methods after the PublisherSource.Subscription
s of the returned
Publisher
.subscriptionSupplier
- Creates a new PublisherSource.Subscription
for each new subscribe and invokes all the
PublisherSource.Subscription
methods after the PublisherSource.Subscription
s of the returned Publisher
.
PublisherSource.Subscription
methods MUST NOT throw.Publisher
.public final Cancellable forEach(java.util.function.Consumer<? super T> forEach)
Publisher
and invokes forEach
Consumer
for each item emitted by this
Publisher
.
This will request Long.MAX_VALUE
from the PublisherSource.Subscription
.
From a sequential programming point of view this method is roughly equivalent to the following:
List<T> results = resultOfThisPublisher();
results.iterator().forEachRemaining(forEach);
forEach
- Consumer
to invoke for each PublisherSource.Subscriber.onNext(Object)
.Cancellable
used to invoke Cancellable.cancel()
on the parameter of
PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
for this Publisher
.public final Publisher<T> publishOn(Executor executor)
Publisher
that will use the passed Executor
to invoke all PublisherSource.Subscriber
methods.
This method does not override preceding Executor
s, if any, specified for this
Publisher
. 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.Publisher
that will use the passed Executor
to invoke all methods of
PublisherSource.Subscriber
.public final Publisher<T> publishOnOverride(Executor executor)
Publisher
that will use the passed Executor
to invoke all PublisherSource.Subscriber
methods.
This method overrides preceding Executor
s, if any, specified for this
Publisher
.
That is to say preceding and subsequent operations for this execution chain will use this Executor
.
If such an override is not required, publishOn(Executor)
can be used.public final Publisher<T> subscribeOn(Executor executor)
Publisher
that will use the passed Executor
to invoke the following methods:
PublisherSource.Subscription
methods.handleSubscribe(PublisherSource.Subscriber)
method.Executor
s, if any, specified for this
Publisher
. 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.Publisher
that will use the passed Executor
to invoke all methods of
PublisherSource.Subscription
and handleSubscribe(PublisherSource.Subscriber)
.public final Publisher<T> subscribeOnOverride(Executor executor)
Publisher
that will use the passed Executor
to invoke the following methods:
PublisherSource.Subscription
methods.handleSubscribe(PublisherSource.Subscriber)
method.Executor
s, if any, specified for this
Publisher
.
That is to say preceding and subsequent operations for this execution chain will use this Executor
.
If such an override is not required, subscribeOn(Executor)
can be used.executor
- Executor
to use.Publisher
that will use the passed Executor
to invoke all methods of
PublisherSource.Subscription
and handleSubscribe(PublisherSource.Subscriber)
both for the returned
Publisher
as well as this
Publisher
.public final Publisher<T> publishAndSubscribeOn(Executor executor)
Publisher
that will use the passed Executor
to invoke the following methods:
PublisherSource.Subscriber
methods.PublisherSource.Subscription
methods.handleSubscribe(PublisherSource.Subscriber)
method.Executor
s, if any, specified for this
Publisher
. 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.Publisher
that will use the passed Executor
to invoke all methods
PublisherSource.Subscriber
, PublisherSource.Subscription
and handleSubscribe(PublisherSource.Subscriber)
.public final Publisher<T> publishAndSubscribeOnOverride(Executor executor)
Publisher
that will use the passed Executor
to invoke the following methods:
PublisherSource.Subscriber
methods.PublisherSource.Subscription
methods.handleSubscribe(PublisherSource.Subscriber)
method.Executor
s, if any, specified for this
Publisher
.
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.Publisher
that will use the passed Executor
to invoke all methods of
PublisherSource.Subscriber
, PublisherSource.Subscription
and handleSubscribe(PublisherSource.Subscriber)
both for the
returned Publisher
as well as this
Publisher
.public final Publisher<T> subscribeShareContext()
Publisher
is subscribed to, the AsyncContext
will be shared
instead of making a copy
.
This operator only impacts behavior if the returned Publisher
is subscribed directly after this operator,
that means this must be the "last operator" in the chain for this to have an impact.
Publisher
that will share the AsyncContext
instead of making a
copy
when subscribed to.public final <R> Publisher<R> liftSync(PublisherOperator<? super T,? extends R> operator)
Returns a Publisher
which when subscribed, the operator
argument will be used to wrap the
PublisherSource.Subscriber
before subscribing to this Publisher
.
Publisher<X> pub = ...;
pub.map(..) // A
.liftSync(original -> modified)
.filter(..) // B
The original -> modified
"operator" MUST be "synchronous" in that it does not interact
with the original PublisherSource.Subscriber
from outside the modified PublisherSource.Subscriber
or PublisherSource.Subscription
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(PublisherOperator)
.R
- Type of the items emitted by the returned Publisher
.operator
- The custom operator logic. The input is the "original" PublisherSource.Subscriber
to this
Publisher
and the return is the "modified" PublisherSource.Subscriber
that provides custom operator business
logic.Publisher
which when subscribed, the operator
argument will be used to wrap the
PublisherSource.Subscriber
before subscribing to this Publisher
.liftAsync(PublisherOperator)
public final <R> Single<R> liftSyncToSingle(PublisherToSingleOperator<? super T,? extends R> operator)
Returns a Single
which when subscribed, the operator
argument will be used to convert between the
SingleSource.Subscriber
to a PublisherSource.Subscriber
before subscribing to this Publisher
.
Publisher<X> pub = ...;
pub.map(..) // A
.liftSync(original -> modified)
.filter(..) // B - we have converted to Single now!
The original -> modified
"operator" MUST be "synchronous" in that it does not interact
with the original PublisherSource.Subscriber
from outside the modified PublisherSource.Subscriber
or PublisherSource.Subscription
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, don't use this operator.R
- Type of the items emitted by the returned Single
.operator
- The custom operator logic. The input is the "original" SingleSource.Subscriber
to the
returned Single
and the return is the "modified" PublisherSource.Subscriber
that provides custom operator
business logic on this Publisher
.Single
which when subscribed, the operator
argument will be used to convert the
SingleSource.Subscriber
to a PublisherSource.Subscriber
before subscribing to this Publisher
.public final <R> Publisher<R> liftAsync(PublisherOperator<? super T,? extends R> operator)
Returns a Publisher
which will wrap the PublisherSource.Subscriber
using the provided
operator
argument before subscribing to this Publisher
.
Publisher<X> pub = ...;
pub.map(..) // A
.liftAsync(original -> modified)
.filter(..) // B
The original -> modified
"operator" MAY be "asynchronous" in that it may interact with the original
PublisherSource.Subscriber
from outside the modified PublisherSource.Subscriber
or PublisherSource.Subscription
threads. More
specifically:
PublisherSource.Subscriber
invocations going "downstream" (i.e. from A to B above) MAY be
offloaded via an Executor
PublisherSource.Subscription
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 Publisher
.operator
- The custom operator logic. The input is the "original" PublisherSource.Subscriber
to this
Publisher
and the return is the "modified" PublisherSource.Subscriber
that provides custom operator business
logic.Publisher
which when subscribed, the operator
argument will be used to wrap the
PublisherSource.Subscriber
before subscribing to this Publisher
.liftSync(PublisherOperator)
public final Single<T> firstOrElse(java.util.function.Supplier<T> defaultValueSupplier)
defaultValueSupplier
- A Supplier
of default value if this Publisher
did not emit any item.Single
that will contain the first item emitted from the this Publisher
.
If the source Publisher
does not emit any item, then the returned Single
will contain the value
as returned by the passed Supplier
.public final Single<T> firstOrError()
Publisher
emits exactly a single PublisherSource.Subscriber.onNext(Object)
to its
PublisherSource.Subscriber
. If this Publisher
terminates without emitting any
items a NoSuchElementException
will be signaled and if this Publisher
emits more than one item,
an IllegalArgumentException
will be signaled. Any error emitted by this Publisher
will be
forwarded to the returned Single
.public final Completable ignoreElements()
Publisher
and forwards the termination signal to the returned
Completable
.Completable
that mirrors the terminal signal from this Publisher
.public final <R> Single<R> collect(java.util.function.Supplier<? extends R> resultFactory, java.util.function.BiFunction<? super R,? super T,R> collector)
Publisher
into a single item.R
- Type of the reduced item.resultFactory
- Factory for the result which collects all items emitted by this Publisher
.
This will be called every time the returned Single
is subscribed.collector
- Invoked for every item emitted by the source Publisher
and returns the same or altered
result
object.Single
that completes with the single result
or any error emitted by the source
Publisher
.public final java.util.concurrent.Future<java.util.Collection<T>> toFuture()
Publisher
into a Future
with a Collection
containing the elements of this
Publisher
upon successful termination. If this Publisher
terminates in an error, then the
intermediate Collection
will be discarded and the Future
will complete exceptionally.Future
with a Collection
containing the elements of this Publisher
upon
successful termination.toFuture(Supplier, BiFunction)
public final <R> java.util.concurrent.Future<R> toFuture(java.util.function.Supplier<? extends R> resultFactory, java.util.function.BiFunction<? super R,? super T,R> reducer)
Publisher
into a Future
of type R
which represents all elements of this
Publisher
upon successful termination. If this Publisher
terminates in an error, then the
intermediate R
will be discarded and the Future
will complete exceptionally.R
- Type of the reduced item.resultFactory
- Factory for the result which collects all items emitted by this Publisher
.reducer
- Invoked for every item emitted by the source Publisher
and returns the same or altered
result
object.Future
of type R
which represents all elements of this Publisher
upon
successful termination.public final java.util.concurrent.CompletionStage<java.util.Collection<T>> toCompletionStage()
Publisher
into a CompletionStage
with a Collection
containing the elements
of this Publisher
upon successful termination. If this Publisher
terminates in an error, then the
intermediate Collection
will be discarded and the CompletionStage
will complete exceptionally.CompletionStage
with a Collection
containing the elements of this Publisher
upon successful termination.toCompletionStage(Supplier, BiFunction)
public final <R> java.util.concurrent.CompletionStage<R> toCompletionStage(java.util.function.Supplier<? extends R> resultFactory, java.util.function.BiFunction<? super R,? super T,R> reducer)
Publisher
into a CompletionStage
of type R
which represents all elements of
this Publisher
upon successful termination. If this Publisher
terminates in an error, then the
intermediate R
will be discarded and the CompletionStage
will complete exceptionally.R
- Type of the reduced item.resultFactory
- Factory for the result which collects all items emitted by this Publisher
.reducer
- Invoked for every item emitted by the source Publisher
and returns the same or altered
result
object.CompletionStage
of type R
which represents all elements of this Publisher
upon
successful termination.public final java.io.InputStream toInputStream(java.util.function.Function<? super T,byte[]> serializer)
this
Publisher
and converts all signals received by the PublisherSource.Subscriber
to the
returned InputStream
following the below rules:
PublisherSource.Subscription
received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is used to
request more data when required. If the returned InputStream
is closed, PublisherSource.Subscription
is
cancelled and any unread data is disposed.PublisherSource.Subscriber.onNext(Object)
are converted to a byte[]
using the
passed serializer
. These byte
s are available to be read from the InputStream
Throwable
received by PublisherSource.Subscriber.onError(Throwable)
is thrown (wrapped in an
IOException
) when data is read from the returned InputStream
. This error will be thrown
only after draining all queued data, if any.PublisherSource.Subscriber.onComplete()
is called, returned InputStream
's read methods will return
-1
to indicate end of stream after emitting all received data.this
Publisher
if available to reduce
blocking for read operations from the returned InputStream
. In order to increase responsiveness of the
InputStream
some amount of buffering may be done. Use toInputStream(Function, int)
to manage
capacity of this buffer.serializer
- Function
that is invoked for every item emitted by this
Publisher
.InputStream
that emits all data emitted by this
Publisher
. If this
Publisher
terminates with an error, same error is thrown (wrapped in an IOException
) from the
returned InputStream
s read methods after emitting all received data.public final java.io.InputStream toInputStream(java.util.function.Function<? super T,byte[]> serializer, int queueCapacity)
this
Publisher
and converts all signals received by the PublisherSource.Subscriber
to the
returned InputStream
following the below rules:
PublisherSource.Subscription
received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is used to
request more data when required. If the returned InputStream
is closed, PublisherSource.Subscription
is
cancelled and any unread data is disposed.PublisherSource.Subscriber.onNext(Object)
are convertedto a byte[]
using the
passed serializer
. These byte
s are available to be read from the InputStream
Throwable
received by PublisherSource.Subscriber.onError(Throwable)
is thrown (wrapped in an
IOException
) when data is read from the returned InputStream
. This error will be thrown
only after draining all queued data, if any.PublisherSource.Subscriber.onComplete()
is called, returned InputStream
's read methods will return
-1
to indicate end of stream after emitting all received data.this
Publisher
if available to reduce blocking for read
operations from the returned InputStream
. In order to increase responsiveness of the InputStream
some amount of buffering may be done. queueCapacity
can be used to bound this buffer.serializer
- Function
that is invoked for every item emitted by this
Publisher
.queueCapacity
- Hint for the capacity of the intermediary queue that stores items that are emitted by
this
Publisher
but has not yet been read from the returned InputStream
.InputStream
that emits all data emitted by this
Publisher
. If this
Publisher
terminates with an error, same error is thrown (wrapped in an IOException
) from the
returned InputStream
s read methods after emitting all received data.public final BlockingIterable<T> toIterable()
this
Publisher
to an BlockingIterable
. Every time
BlockingIterable.iterator()
is called on the returned BlockingIterable
, this
Publisher
is subscribed following the below rules:
PublisherSource.Subscription
received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is used to
request more data when required.PublisherSource.Subscriber.onNext(Object)
is returned from a call to
BlockingIterator.next()
.Throwable
received by PublisherSource.Subscriber.onError(Throwable)
is thrown (wrapped in a
RuntimeException
if required) when BlockingIterator.next()
is called. This error will be
thrown only after draining all queued data, if any.PublisherSource.Subscriber.onComplete()
is called, returned BlockingIterator
s
Iterator.hasNext()
will return false
BlockingIterator.next()
will throw
NoSuchElementException
. This error will be thrown only after draining all queued data, if any.this
Publisher
if available to reduce blocking of
Iterator.hasNext()
from the returned BlockingIterable
. In order to increase responsiveness of
the Iterator
some amount of buffering may be done. Use toIterable(int)
to manage capacity of
this buffer.
BlockingIterator
from the returned BlockingIterable
will block on
Iterator.hasNext()
and BlockingIterator.next()
if no data is available. This operator may
try to reduce this blocking by requesting data ahead of time.BlockingIterable
representing this
Publisher
. Every time
BlockingIterable.iterator()
is invoked on the BlockingIterable
, this
Publisher
is subscribed. BlockingIterator
s returned from this BlockingIterable
do not support
Iterator.remove()
.public final BlockingIterable<T> toIterable(int queueCapacityHint)
this
Publisher
to an BlockingIterable
. Every time
BlockingIterable.iterator()
is called on the returned BlockingIterable
, this
Publisher
is subscribed following the below rules:
PublisherSource.Subscription
received by PublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)
is used to
request more data when required.PublisherSource.Subscriber.onNext(Object)
is returned from a call to
BlockingIterator.next()
.Throwable
received by PublisherSource.Subscriber.onError(Throwable)
is thrown (wrapped in a
RuntimeException
if required) when BlockingIterator.next()
. This error will be thrown
only after draining all queued data, if any.PublisherSource.Subscriber.onComplete()
is called, returned BlockingIterator
s
Iterator.hasNext()
will return false
and BlockingIterator.next()
will throw
NoSuchElementException
. This error will be thrown only after draining all queued data, if any.this
Publisher
if available to reduce blocking of
Iterator.hasNext()
from the returned BlockingIterable
. In order to increase
responsiveness of the BlockingIterator
some amount of buffering may be done. queueCapacityHint
can be used to bound this buffer.
BlockingIterator
from the returned BlockingIterable
will block on
Iterator.hasNext()
and BlockingIterator.next()
if no data is available. This operator may
try to reduce this blocking by requesting data ahead of time.queueCapacityHint
- Hint for the capacity of the intermediary queue that stores items that are emitted by
this
Publisher
but has not yet been returned from the BlockingIterator
.BlockingIterable
representing this
Publisher
. Every time
BlockingIterable.iterator()
is invoked on the BlockingIterable
, this
Publisher
is subscribed. BlockingIterator
s returned from this BlockingIterable
do not support
Iterator.remove()
.protected final void subscribeInternal(PublisherSource.Subscriber<? super T> subscriber)
PublisherSource.subscribe(Subscriber)
which can be used by
different implementations to subscribe.subscriber
- PublisherSource.Subscriber
to subscribe for the result.protected abstract void handleSubscribe(PublisherSource.Subscriber<? super T> subscriber)
Publisher
.subscriber
- the subscriber.public static <T> Publisher<T> from(@Nullable T value)
Publisher
that emits value
to its PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.T
- Type of items emitted by the returned Publisher
.value
- Value that the returned Publisher
will emit.Publisher
that emits value
to its PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.@SafeVarargs public static <T> Publisher<T> from(T... values)
Publisher
that emits all values
to its PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.T
- Type of items emitted by the returned Publisher
.values
- Values that the returned Publisher
will emit.Publisher
that emits all values
to its PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.public static <T> Publisher<T> fromIterable(java.lang.Iterable<? extends T> iterable)
Publisher
that when subscribed will get an Iterator
via Iterable.iterator()
and emit all values to the PublisherSource.Subscriber
and then PublisherSource.Subscriber.onComplete()
.
The Reactive Streams specification provides two criteria (
3.4, and
3.5) stating
the PublisherSource.Subscription
should be "responsive". The responsiveness of the associated PublisherSource.Subscription
s will
depend upon the behavior of the iterable
below. Make sure the Executor
for this execution chain
can tolerate this responsiveness and any blocking behavior.
T
- Type of items emitted by the returned Publisher
.iterable
- used to obtain instances of Iterator
to extract data from. Iterable.iterator()
must not return null
. If this is of type BlockingIterable
then any generated
BlockingIterator
s will have their BlockingIterator.close()
method called if an error
occurs.Publisher
that when subscribed will get an Iterator
via Iterable.iterator()
and emit all values to the PublisherSource.Subscriber
and then PublisherSource.Subscriber.onComplete()
.public static <T> Publisher<T> fromBlockingIterable(BlockingIterable<? extends T> iterable, java.util.function.LongSupplier timeoutSupplier, java.util.concurrent.TimeUnit unit)
Publisher
that when subscribed will get a BlockingIterator
via
BlockingIterable.iterator()
and emit all values to the PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.
The Reactive Streams specification provides two criteria (
3.4, and
3.5) stating
the PublisherSource.Subscription
should be "responsive". The responsiveness of the associated PublisherSource.Subscription
s will
depend upon the behavior of the iterable
below. Make sure the Executor
for this execution chain
can tolerate this responsiveness and any blocking behavior.
T
- Type of items emitted by the returned Publisher
.iterable
- used to obtain instances of Iterator
to extract data from. Iterable.iterator()
must not return null
. Any generated BlockingIterator
s will have their
BlockingIterator.close()
method called if an error occurs.timeoutSupplier
- A LongSupplier
which provides the time duration to wait for each
interaction with iterable
.unit
- The time units for the timeout
duration.Publisher
that when subscribed will get a BlockingIterator
via
BlockingIterable.iterator()
and emit all values to the PublisherSource.Subscriber
and then
PublisherSource.Subscriber.onComplete()
.public static Publisher<byte[]> fromInputStream(java.io.InputStream stream)
Publisher
that when subscribed will emit all data from the InputStream
to the
PublisherSource.Subscriber
and then PublisherSource.Subscriber.onComplete()
.
The Reactive Streams specification provides two criteria (
3.4, and
3.5) stating
the PublisherSource.Subscription
should be "responsive". The responsiveness of the associated PublisherSource.Subscription
s will
depend upon the behavior of the stream
below. Make sure the Executor
for this execution chain
can tolerate this responsiveness and any blocking behavior.
stream
- provides the data in the form of byte[]
to be emitted to the PublisherSource.Subscriber
to the
returned Publisher
. Given the blocking nature of InputStream
, assume PublisherSource.Subscription.request(long)
can block when the underlying InputStream
blocks on InputStream.read(byte[], int, int)
.Publisher
that when subscribed will emit all data from the InputStream
to the
PublisherSource.Subscriber
and then PublisherSource.Subscriber.onComplete()
.public static <T> Publisher<T> empty()
Publisher
that completes when subscribed without emitting any item to its
PublisherSource.Subscriber
.T
- Type of items emitted by the returned Publisher
.Publisher
that completes when subscribed without emitting any item to its
PublisherSource.Subscriber
.public static <T> Publisher<T> never()
Publisher
that never emits any item to its PublisherSource.Subscriber
and never call any terminal
methods on it.T
- Type of items emitted by the returned Publisher
.Publisher
that never emits any item to its PublisherSource.Subscriber
and never call any terminal
methods on it.public static <T> Publisher<T> failed(java.lang.Throwable cause)
Publisher
that terminates its PublisherSource.Subscriber
with an error without emitting any item to
it.T
- Type of items emitted by the returned Publisher
.cause
- The Throwable
that is used to terminate the PublisherSource.Subscriber
.Publisher
that terminates its PublisherSource.Subscriber
with an error without emitting any item to
it.public static <T> Publisher<T> defer(java.util.function.Supplier<? extends Publisher<? extends T>> publisherSupplier)
Publisher
till it is subscribed.T
- Type of items emitted by the returned Publisher
.publisherSupplier
- Supplier
to create a new Publisher
every time the returned
Publisher
is subscribed.Publisher
that creates a new Publisher
using publisherSupplier
every time
it is subscribed and forwards all items and terminal events from the newly created Publisher
to its
PublisherSource.Subscriber
.