All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.rxjava3.core.Single Maven / Gradle / Ivy

There is a newer version: 3.1.9
Show newest version
/*
 * Copyright (c) 2016-present, RxJava Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 * the License for the specific language governing permissions and limitations under the License.
 */

package io.reactivex.rxjava3.core;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;

import org.reactivestreams.*;

import io.reactivex.rxjava3.annotations.*;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.*;
import io.reactivex.rxjava3.internal.fuseable.*;
import io.reactivex.rxjava3.internal.jdk8.*;
import io.reactivex.rxjava3.internal.observers.*;
import io.reactivex.rxjava3.internal.operators.completable.*;
import io.reactivex.rxjava3.internal.operators.flowable.*;
import io.reactivex.rxjava3.internal.operators.maybe.*;
import io.reactivex.rxjava3.internal.operators.mixed.*;
import io.reactivex.rxjava3.internal.operators.observable.ObservableSingleSingle;
import io.reactivex.rxjava3.internal.operators.single.*;
import io.reactivex.rxjava3.internal.util.ErrorMode;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.*;

/**
 * The {@code Single} class implements the Reactive Pattern for a single value response.
 * 

* {@code Single} behaves similarly to {@link Observable} except that it can only emit either a single successful * value or an error (there is no {@code onComplete} notification as there is for an {@code Observable}). *

* The {@code Single} class implements the {@link SingleSource} base interface and the default consumer * type it interacts with is the {@link SingleObserver} via the {@link #subscribe(SingleObserver)} method. *

* The {@code Single} operates with the following sequential protocol: *

 *     onSubscribe (onSuccess | onError)?
 * 
*

* Note that {@code onSuccess} and {@code onError} are mutually exclusive events; unlike {@code Observable}, * {@code onSuccess} is never followed by {@code onError}. *

* Like {@code Observable}, a running {@code Single} can be stopped through the {@link Disposable} instance * provided to consumers through {@link SingleObserver#onSubscribe}. *

* Like an {@code Observable}, a {@code Single} is lazy, can be either "hot" or "cold", synchronous or * asynchronous. {@code Single} instances returned by the methods of this class are cold * and there is a standard hot implementation in the form of a subject: * {@link io.reactivex.rxjava3.subjects.SingleSubject SingleSubject}. *

* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams: *

* *

* See {@link Flowable} or {@code Observable} for the * implementation of the Reactive Pattern for a stream or vector of values. *

* For more information see the ReactiveX * documentation. *

* Example: *


 * Disposable d = Single.just("Hello World")
 *    .delay(10, TimeUnit.SECONDS, Schedulers.io())
 *    .subscribeWith(new DisposableSingleObserver<String>() {
 *        @Override
 *        public void onStart() {
 *            System.out.println("Started");
 *        }
 *
 *        @Override
 *        public void onSuccess(String value) {
 *            System.out.println("Success: " + value);
 *        }
 *
 *        @Override
 *        public void onError(Throwable error) {
 *            error.printStackTrace();
 *        }
 *    });
 * 
 * Thread.sleep(5000);
 * 
 * d.dispose();
 * 
*

* Note that by design, subscriptions via {@link #subscribe(SingleObserver)} can't be disposed * from the outside (hence the * {@code void} return of the {@link #subscribe(SingleObserver)} method) and it is the * responsibility of the implementor of the {@code SingleObserver} to allow this to happen. * RxJava supports such usage with the standard * {@link io.reactivex.rxjava3.observers.DisposableSingleObserver DisposableSingleObserver} instance. * For convenience, the {@link #subscribeWith(SingleObserver)} method is provided as well to * allow working with a {@code SingleObserver} (or subclass) instance to be applied with in * a fluent manner (such as in the example above). * @param * the type of the item emitted by the {@code Single} * @since 2.0 * @see io.reactivex.rxjava3.observers.DisposableSingleObserver */ public abstract class Single<@NonNull T> implements SingleSource { /** * Runs multiple {@link SingleSource}s and signals the events of the first one that signals (disposing * the rest). *

* *

*
Scheduler:
*
{@code amb} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@link Iterable} sequence of sources. A subscription to each source will * occur in the same order as in this {@code Iterable}. * @return the new {@code Single} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single amb(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new SingleAmb<>(null, sources)); } /** * Runs multiple {@link SingleSource}s and signals the events of the first one that signals (disposing * the rest). *

* *

*
Scheduler:
*
{@code ambArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of sources. A subscription to each source will * occur in the same order as in this array. * @return the new {@code Single} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs @NonNull public static <@NonNull T> Single ambArray(@NonNull SingleSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return error(SingleInternalHelper.emptyThrower()); } if (sources.length == 1) { @SuppressWarnings("unchecked") SingleSource source = (SingleSource)sources[0]; return wrap(source); } return RxJavaPlugins.onAssembly(new SingleAmb<>(sources, null)); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided by * an {@link Iterable} sequence. *

* *

*
Backpressure:
*
The returned {@link Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Iterable} sequence of {@code SingleSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public static <@NonNull T> Flowable concat(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).concatMapSingleDelayError(Functions.identity(), false); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided by * an {@link ObservableSource} sequence. *

* *

*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code ObservableSource} of {@code SingleSource} instances * @return the new {@link Observable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Observable concat(@NonNull ObservableSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new ObservableConcatMapSingle<>(sources, Functions.identity(), ErrorMode.IMMEDIATE, 2)); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided by * a {@link Publisher} sequence. *

* *

*
Backpressure:
*
The returned {@link Flowable} honors the backpressure of the downstream consumer * and the sources {@code Publisher} is expected to honor it as well.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Publisher} of {@code SingleSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { return concat(sources, 2); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided by * a {@link Publisher} sequence and prefetched by the specified amount. *

* *

*
Backpressure:
*
The returned {@link Flowable} honors the backpressure of the downstream consumer * and the sources {@code Publisher} is expected to honor it as well.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Publisher} of {@code SingleSource} instances * @param prefetch the number of {@code SingleSource}s to prefetch from the {@code Publisher} * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code prefetch} is non-positive * @since 2.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat(@NonNull Publisher<@NonNull ? extends SingleSource> sources, int prefetch) { Objects.requireNonNull(sources, "sources is null"); ObjectHelper.verifyPositive(prefetch, "prefetch"); return RxJavaPlugins.onAssembly(new FlowableConcatMapSinglePublisher<>(sources, Functions.identity(), ErrorMode.IMMEDIATE, prefetch)); } /** * Returns a {@link Flowable} that emits the items emitted by two {@link SingleSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be concatenated * @param source2 * a {@code SingleSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat( @NonNull SingleSource source1, @NonNull SingleSource source2 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return Flowable.fromArray(source1, source2).concatMapSingleDelayError(Functions.identity(), false); } /** * Returns a {@link Flowable} that emits the items emitted by three {@link SingleSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be concatenated * @param source2 * a {@code SingleSource} to be concatenated * @param source3 * a {@code SingleSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return Flowable.fromArray(source1, source2, source3).concatMapSingleDelayError(Functions.identity(), false); } /** * Returns a {@link Flowable} that emits the items emitted by four {@link SingleSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be concatenated * @param source2 * a {@code SingleSource} to be concatenated * @param source3 * a {@code SingleSource} to be concatenated * @param source4 * a {@code SingleSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return Flowable.fromArray(source1, source2, source3, source4).concatMapSingleDelayError(Functions.identity(), false); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided in * an array. *

* *

*
Backpressure:
*
The returned {@link Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concatArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of {@code SingleSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable concatArray(@NonNull SingleSource... sources) { return Flowable.fromArray(sources).concatMapSingleDelayError(Functions.identity(), false); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link SingleSource}s provided in * an array. *

* *

*
Backpressure:
*
The returned {@link Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concatArrayDelayError} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of {@code SingleSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable concatArrayDelayError(@NonNull SingleSource... sources) { return Flowable.fromArray(sources).concatMapSingleDelayError(Functions.identity(), true); } /** * Concatenates a sequence of {@link SingleSource} eagerly into a single stream of values. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the value emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable concatArrayEager(@NonNull SingleSource... sources) { return Flowable.fromArray(sources).concatMapEager(SingleInternalHelper.toFlowable()); } /** * Concatenates a sequence of {@link SingleSource} eagerly into a single stream of values. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the value emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable concatArrayEagerDelayError(@NonNull SingleSource... sources) { return Flowable.fromArray(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), true); } /** * Concatenates the {@link Iterable} sequence of {@link SingleSource}s into a single sequence by subscribing to each {@code SingleSource}, * one after the other, one at a time and delays any errors till the all inner {@code SingleSource}s terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Iterable} sequence of {@code SingleSource}s * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatDelayError(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).concatMapSingleDelayError(Functions.identity()); } /** * Concatenates the {@link Publisher} sequence of {@link SingleSource}s into a single sequence by subscribing to each inner {@code SingleSource}, * one after the other, one at a time and delays any errors till the all inner and the outer {@code Publisher} terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
{@code concatDelayError} fully supports backpressure.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Publisher} sequence of {@code SingleSource}s * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { return Flowable.fromPublisher(sources).concatMapSingleDelayError(Functions.identity()); } /** * Concatenates the {@link Publisher} sequence of {@link SingleSource}s into a single sequence by subscribing to each inner {@code SingleSource}, * one after the other, one at a time and delays any errors till the all inner and the outer {@code Publisher} terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
{@code concatDelayError} fully supports backpressure.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Publisher} sequence of {@code SingleSource}s * @param prefetch The number of upstream items to prefetch so that fresh items are * ready to be mapped when a previous {@code SingleSource} terminates. * The operator replenishes after half of the prefetch amount has been consumed * and turned into {@code SingleSource}s. * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code prefetch} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources, int prefetch) { return Flowable.fromPublisher(sources).concatMapSingleDelayError(Functions.identity(), true, prefetch); } /** * Concatenates an {@link Iterable} sequence of {@link SingleSource}s eagerly into a single stream of values. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the values emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources an {@code Iterable} sequence of {@code SingleSource} that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEager(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), false); } /** * Concatenates an {@link Iterable} sequence of {@link SingleSource}s eagerly into a single stream of values and * runs a limited number of the inner sources at once. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the values emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources an {@code Iterable} sequence of {@code SingleSource} that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code SingleSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code SingleSource}s can be active at the same time * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEager(@NonNull Iterable<@NonNull ? extends SingleSource> sources, int maxConcurrency) { return Flowable.fromIterable(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), false, maxConcurrency, 1); } /** * Concatenates a {@link Publisher} sequence of {@link SingleSource}s eagerly into a single stream of values. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code SingleSource}s as they are observed. The operator buffers the values emitted by these * {@code SingleSource}s and then drains them in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEager(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { return Flowable.fromPublisher(sources).concatMapEager(SingleInternalHelper.toFlowable()); } /** * Concatenates a {@link Publisher} sequence of {@link SingleSource}s eagerly into a single stream of values and * runs a limited number of those inner {@code SingleSource}s at once. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code SingleSource}s as they are observed. The operator buffers the values emitted by these * {@code SingleSource}s and then drains them in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code SingleSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code SingleSource}s can be active at the same time * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEager(@NonNull Publisher<@NonNull ? extends SingleSource> sources, int maxConcurrency) { return Flowable.fromPublisher(sources).concatMapEager(SingleInternalHelper.toFlowable(), maxConcurrency, 1); } /** * Concatenates an {@link Iterable} sequence of {@link SingleSource}s eagerly into a single stream of values, * delaying errors until all the inner sources terminate. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the values emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources an {@code Iterable} sequence of {@code SingleSource} that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), true); } /** * Concatenates an {@link Iterable} sequence of {@link SingleSource}s eagerly into a single stream of values, * delaying errors until all the inner sources terminate. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * source {@code SingleSource}s. The operator buffers the values emitted by these {@code SingleSource}s and then drains them * in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources an {@code Iterable} sequence of {@code SingleSource} that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code SingleSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code SingleSource}s can be active at the same time * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Iterable<@NonNull ? extends SingleSource> sources, int maxConcurrency) { return Flowable.fromIterable(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), true, maxConcurrency, 1); } /** * Concatenates a {@link Publisher} sequence of {@link SingleSource}s eagerly into a single stream of values, * delaying errors until all the inner and the outer sequence terminate. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code SingleSource}s as they are observed. The operator buffers the values emitted by these * {@code SingleSource}s and then drains them in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { return Flowable.fromPublisher(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), true); } /** * Concatenates a {@link Publisher} sequence of {@link SingleSource}s eagerly into a single stream of values, * running at most the specified number of those inner {@code SingleSource}s at once and * delaying errors until all the inner and the outer sequence terminate. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code SingleSource}s as they are observed. The operator buffers the values emitted by these * {@code SingleSource}s and then drains them in order, each one after the previous one succeeds. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code SingleSource}s that need to be eagerly concatenated * @param maxConcurrency the number of inner {@code SingleSource}s to run at once * @return the new {@link Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources, int maxConcurrency) { return Flowable.fromPublisher(sources).concatMapEagerDelayError(SingleInternalHelper.toFlowable(), true, maxConcurrency, 1); } /** * Provides an API (via a cold {@code Single}) that bridges the reactive world with the callback-style world. *

* *

* Example: *


     * Single.<Event>create(emitter -> {
     *     Callback listener = new Callback() {
     *         @Override
     *         public void onEvent(Event e) {
     *             emitter.onSuccess(e);
     *         }
     *
     *         @Override
     *         public void onFailure(Exception e) {
     *             emitter.onError(e);
     *         }
     *     };
     *
     *     AutoCloseable c = api.someMethod(listener);
     *
     *     emitter.setCancellable(c::close);
     *
     * });
     * 
*

* Whenever a {@link SingleObserver} subscribes to the returned {@code Single}, the provided * {@link SingleOnSubscribe} callback is invoked with a fresh instance of a {@link SingleEmitter} * that will interact only with that specific {@code SingleObserver}. If this {@code SingleObserver} * disposes the flow (making {@link SingleEmitter#isDisposed} return {@code true}), * other observers subscribed to the same returned {@code Single} are not affected. *

*
Scheduler:
*
{@code create} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param source the emitter that is called when a {@code SingleObserver} subscribes to the returned {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code source} is {@code null} * @see SingleOnSubscribe * @see Cancellable */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single create(@NonNull SingleOnSubscribe source) { Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new SingleCreate<>(source)); } /** * Calls a {@link Supplier} for each individual {@link SingleObserver} to return the actual {@link SingleSource} to * be subscribed to. *

* *

*
Scheduler:
*
{@code defer} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param supplier the {@code Supplier} that is called for each individual {@code SingleObserver} and * returns a {@code SingleSource} instance to subscribe to * @throws NullPointerException if {@code supplier} is {@code null} * @return the new {@code Single} instance */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single defer(@NonNull Supplier> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new SingleDefer<>(supplier)); } /** * Signals a {@link Throwable} returned by the callback function for each individual {@link SingleObserver}. *

* *

*
Scheduler:
*
{@code error} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param supplier the {@link Supplier} that is called for each individual {@code SingleObserver} and * returns a {@code Throwable} instance to be emitted. * @throws NullPointerException if {@code supplier} is {@code null} * @return the new {@code Single} instance */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single error(@NonNull Supplier supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new SingleError<>(supplier)); } /** * Returns a {@code Single} that invokes a subscriber's {@link SingleObserver#onError onError} method when the * subscriber subscribes to it. *

* *

*
Scheduler:
*
{@code error} does not operate by default on a particular {@link Scheduler}.
*
* * @param throwable * the particular {@link Throwable} to pass to {@link SingleObserver#onError onError} * @param * the type of the item (ostensibly) emitted by the {@code Single} * @return the new {@code Single} that invokes the subscriber's {@link SingleObserver#onError onError} method when * the subscriber subscribes to it * @throws NullPointerException if {@code throwable} is {@code null} * @see ReactiveX operators documentation: Throw */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single error(@NonNull Throwable throwable) { Objects.requireNonNull(throwable, "throwable is null"); return error(Functions.justSupplier(throwable)); } /** * Returns a {@code Single} that invokes the given {@link Callable} for each incoming {@link SingleObserver} * and emits its value or exception to them. *

* Allows you to defer execution of passed function until {@code SingleObserver} subscribes to the {@code Single}. * It makes passed function "lazy". * Result of the function invocation will be emitted by the {@link Single}. *

* *

*
Scheduler:
*
{@code fromCallable} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the {@code Callable} throws an exception, the respective {@link Throwable} is * delivered to the downstream via {@link SingleObserver#onError(Throwable)}, * except when the downstream has disposed this {@code Single} source. * In this latter case, the {@code Throwable} is delivered to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} as an {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. *
*
* * @param callable * function which execution should be deferred, it will be invoked when {@code SingleObserver} will subscribe to the {@link Single}. * @param * the type of the item emitted by the {@code Single}. * @return the new {@code Single} whose {@code SingleObserver}s' subscriptions trigger an invocation of the given function. * @throws NullPointerException if {@code callable} is {@code null} * @see #defer(Supplier) * @see #fromSupplier(Supplier) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromCallable(@NonNull Callable callable) { Objects.requireNonNull(callable, "callable is null"); return RxJavaPlugins.onAssembly(new SingleFromCallable<>(callable)); } /** * Converts a {@link Future} into a {@code Single} and awaits its outcome in a blocking fashion. *

* *

* The operator calls {@link Future#get()}, which is a blocking method, on the subscription thread. * It is recommended applying {@link #subscribeOn(Scheduler)} to move this blocking wait to a * background thread, and if the {@link Scheduler} supports it, interrupt the wait when the flow * is disposed. *

* A non-{@code null} value is then emitted via {@code onSuccess} or any exception is emitted via * {@code onError}. If the {@code Future} completes with {@code null}, a {@link NullPointerException} * is signaled. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@code Scheduler}.
*
* * @param future * the source {@code Future} * @param * the type of object that the {@code Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return the new {@code Single} that emits the item from the source {@code Future} * @throws NullPointerException if {@code future} is {@code null} * @see ReactiveX operators documentation: From * @see #fromFuture(Future, long, TimeUnit) * @see #fromCompletionStage(CompletionStage) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Single fromFuture(@NonNull Future future) { return toSingle(Flowable.fromFuture(future)); } /** * Converts a {@link Future} into a {@code Single} and awaits its outcome, or timeout, in a blocking fashion. *

* *

* The operator calls {@link Future#get(long, TimeUnit)}, which is a blocking method, on the subscription thread. * It is recommended applying {@link #subscribeOn(Scheduler)} to move this blocking wait to a * background thread, and if the {@link Scheduler} supports it, interrupt the wait when the flow * is disposed. *

* A non-{@code null} value is then emitted via {@code onSuccess} or any exception is emitted via * {@code onError}. If the {@code Future} completes with {@code null}, a {@link NullPointerException} * is signaled. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@code Scheduler}.
*
* * @param future * the source {@code Future} * @param timeout * the maximum time to wait before calling {@code get} * @param unit * the {@link TimeUnit} of the {@code timeout} argument * @param * the type of object that the {@code Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return the new {@code Single} that emits the item from the source {@code Future} * @throws NullPointerException if {@code future} or {@code unit} is {@code null} * @see ReactiveX operators documentation: From */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Single fromFuture(@NonNull Future future, long timeout, @NonNull TimeUnit unit) { return toSingle(Flowable.fromFuture(future, timeout, unit)); } /** * Returns a {@code Single} instance that when subscribed to, subscribes to the {@link MaybeSource} instance and * emits {@code onSuccess} as a single item, turns an {@code onComplete} into {@link NoSuchElementException} error signal or * forwards the {@code onError} signal. *

* *

*
Scheduler:
*
{@code fromMaybe} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the {@code MaybeSource} element * @param maybe the {@code MaybeSource} instance to subscribe to, not {@code null} * @return the new {@code Single} instance * @throws NullPointerException if {@code maybe} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromMaybe(@NonNull MaybeSource maybe) { Objects.requireNonNull(maybe, "maybe is null"); return RxJavaPlugins.onAssembly(new MaybeToSingle<>(maybe, null)); } /** * Returns a {@code Single} instance that when subscribed to, subscribes to the {@link MaybeSource} instance and * emits {@code onSuccess} as a single item, emits the {@code defaultItem} for an {@code onComplete} signal or * forwards the {@code onError} signal. *

* *

*
Scheduler:
*
{@code fromMaybe} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the {@code MaybeSource} element * @param maybe the {@code MaybeSource} instance to subscribe to, not {@code null} * @param defaultItem the item to signal if the current {@code MaybeSource} is empty * @return the new {@code Single} instance * @throws NullPointerException if {@code maybe} or {@code defaultItem} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromMaybe(@NonNull MaybeSource maybe, @NonNull T defaultItem) { Objects.requireNonNull(maybe, "maybe is null"); Objects.requireNonNull(defaultItem, "defaultItem is null"); return RxJavaPlugins.onAssembly(new MaybeToSingle<>(maybe, defaultItem)); } /** * Wraps a specific {@link Publisher} into a {@code Single} and signals its single element or error. *

* *

* If the source {@code Publisher} is empty, a {@link NoSuchElementException} is signaled. If * the source has more than one element, an {@link IndexOutOfBoundsException} is signaled. *

* The {@code Publisher} must follow the * Reactive Streams specification. * Violating the specification may result in undefined behavior. *

* If possible, use {@link #create(SingleOnSubscribe)} to create a * source-like {@code Single} instead. *

* Note that even though {@code Publisher} appears to be a functional interface, it * is not recommended to implement it through a lambda as the specification requires * state management that is not achievable with a stateless lambda. *

*
Backpressure:
*
The {@code publisher} is consumed in an unbounded fashion but will be cancelled * if it produced more than one item.
*
Scheduler:
*
{@code fromPublisher} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param publisher the source {@code Publisher} instance, not {@code null} * @return the new {@code Single} instance * @throws NullPointerException if {@code publisher} is {@code null} * @see #create(SingleOnSubscribe) */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromPublisher(@NonNull Publisher publisher) { Objects.requireNonNull(publisher, "publisher is null"); return RxJavaPlugins.onAssembly(new SingleFromPublisher<>(publisher)); } /** * Wraps a specific {@link ObservableSource} into a {@code Single} and signals its single element or error. *

* *

* If the {@code ObservableSource} is empty, a {@link NoSuchElementException} is signaled. * If the source has more than one element, an {@link IndexOutOfBoundsException} is signaled. *

*
Scheduler:
*
{@code fromObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param observable the source sequence to wrap, not {@code null} * @param * the type of the item emitted by the {@code Single}. * @return the new {@code Single} instance * @throws NullPointerException if {@code observable} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromObservable(@NonNull ObservableSource observable) { Objects.requireNonNull(observable, "observable is null"); return RxJavaPlugins.onAssembly(new ObservableSingleSingle<>(observable, null)); } /** * Returns a {@code Single} that invokes passed supplier and emits its result * for each individual {@link SingleObserver} that subscribes. *

* Allows you to defer execution of passed function until a {@code SingleObserver} subscribes to the {@link Single}. * It makes passed function "lazy". * Result of the function invocation will be emitted by the {@link Single}. *

* *

*
Scheduler:
*
{@code fromSupplier} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the {@link Supplier} throws an exception, the respective {@link Throwable} is * delivered to the downstream via {@link SingleObserver#onError(Throwable)}, * except when the downstream has disposed this {@code Single} source. * In this latter case, the {@code Throwable} is delivered to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} as an {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. *
*
* * @param supplier * function which execution should be deferred, it will be invoked when {@code SingleObserver} subscribes to the {@code Single}. * @param * the type of the item emitted by the {@code Single}. * @return the new {@code Single} whose {@code SingleObserver}s' subscriptions trigger an invocation of the given function. * @throws NullPointerException if {@code supplier} is {@code null} * @see #defer(Supplier) * @see #fromCallable(Callable) * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single fromSupplier(@NonNull Supplier supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new SingleFromSupplier<>(supplier)); } /** * Returns a {@code Single} that emits a specified item. *

* *

* To convert any object into a {@code Single} that emits that object, pass that object into the * {@code just} method. *

*
Scheduler:
*
{@code just} does not operate by default on a particular {@link Scheduler}.
*
* * @param item * the item to emit * @param * the type of that item * @return the new {@code Single} that emits {@code item} * @throws NullPointerException if {@code item} is {@code null} * @see ReactiveX operators documentation: Just */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Single just(T item) { Objects.requireNonNull(item, "item is null"); return RxJavaPlugins.onAssembly(new SingleJust<>(item)); } /** * Merges an {@link Iterable} sequence of {@link SingleSource} instances into a single {@link Flowable} sequence, * running all {@code SingleSource}s at once. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(Iterable)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the {@code Iterable} sequence of {@code SingleSource} sources * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.0 * @see #mergeDelayError(Iterable) */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).flatMapSingle(Functions.identity()); } /** * Merges a sequence of {@link SingleSource} instances emitted by a {@link Publisher} into a single {@link Flowable} sequence, * running all {@code SingleSource}s at once. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(Publisher)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the {@code Publisher} emitting a sequence of {@code SingleSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #mergeDelayError(Publisher) * @since 2.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableFlatMapSinglePublisher<>(sources, Functions.identity(), false, Integer.MAX_VALUE)); } /** * Flattens a {@link SingleSource} that emits a {@code SingleSingle} into a single {@code Single} that emits the item * emitted by the nested {@code SingleSource}, without any transformation. *

* *

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
The resulting {@code Single} emits the outer source's or the inner {@code SingleSource}'s {@link Throwable} as is. * Unlike the other {@code merge()} operators, this operator won't and can't produce a {@link CompositeException} because there is * only one possibility for the outer or the inner {@code SingleSource} to emit an {@code onError} signal. * Therefore, there is no need for a {@code mergeDelayError(SingleSource>)} operator. *
*
* * @param the value type of the sources and the output * @param source * a {@code Single} that emits a {@code Single} * @return the new {@code Single} that emits the item that is the result of flattening the {@code Single} emitted * by {@code source} * @throws NullPointerException if {@code source} is {@code null} * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single merge(@NonNull SingleSource> source) { Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new SingleFlatMap, T>(source, Functions.identity())); } /** * Flattens two {@link SingleSource}s into one {@link Flowable} sequence, without any transformation. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as a single {@code Flowable}, by * using the {@code merge} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(SingleSource, SingleSource)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(SingleSource, SingleSource) */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull SingleSource source1, @NonNull SingleSource source2 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return Flowable.fromArray(source1, source2).flatMapSingle(Functions.identity(), false, Integer.MAX_VALUE); } /** * Flattens three {@link SingleSource}s into one {@link Flowable} sequence, without any transformation. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as a single {@code Flowable}, by * the {@code merge} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(SingleSource, SingleSource, SingleSource)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @param source3 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(SingleSource, SingleSource, SingleSource) */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return Flowable.fromArray(source1, source2, source3).flatMapSingle(Functions.identity(), false, Integer.MAX_VALUE); } /** * Flattens four {@link SingleSource}s into one {@link Flowable} sequence, without any transformation. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as a single {@code Flowable}, by * the {@code merge} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(SingleSource, SingleSource, SingleSource, SingleSource)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @param source3 * a {@code SingleSource} to be merged * @param source4 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(SingleSource, SingleSource, SingleSource, SingleSource) */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return Flowable.fromArray(source1, source2, source3, source4).flatMapSingle(Functions.identity(), false, Integer.MAX_VALUE); } /** * Merges an array of {@link SingleSource} instances into a single {@link Flowable} sequence, * running all {@code SingleSource}s at once. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeArray} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code SingleSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code SingleSource}s are disposed. * If more than one {@code SingleSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeArrayDelayError(SingleSource...)} to merge sources and terminate only when all source {@code SingleSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the array sequence of {@code SingleSource} sources * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #mergeArrayDelayError(SingleSource...) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable mergeArray(SingleSource... sources) { return Flowable.fromArray(sources).flatMapSingle(Functions.identity(), false, Math.max(1, sources.length)); } /** * Flattens an array of {@link SingleSource}s into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from each of the source {@code SingleSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(Publisher)} except that if any of the merged {@code SingleSource}s notify of an * error via {@link Subscriber#onError onError}, {@code mergeArrayDelayError} will refrain from propagating that * error notification until all of the merged {@code SingleSource}s have finished emitting items. *

* Even if multiple merged {@code SingleSource}s send {@code onError} notifications, {@code mergeArrayDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeArrayDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources * the array of {@code SingleSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs @NonNull public static <@NonNull T> Flowable mergeArrayDelayError(@NonNull SingleSource... sources) { return Flowable.fromArray(sources).flatMapSingle(Functions.identity(), true, Math.max(1, sources.length)); } /** * Merges an {@link Iterable} sequence of {@link SingleSource} instances into one {@link Flowable} sequence, * running all {@code SingleSource}s at once and delaying any error(s) until all sources succeed or fail. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common and resulting value type * @param sources the {@code Iterable} sequence of {@code SingleSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #merge(Iterable) * @since 2.2 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError(@NonNull Iterable<@NonNull ? extends SingleSource> sources) { return Flowable.fromIterable(sources).flatMapSingle(Functions.identity(), true, Integer.MAX_VALUE); } /** * Merges a sequence of {@link SingleSource} instances emitted by a {@link Publisher} into a {@link Flowable} sequence, * running all {@code SingleSource}s at once and delaying any error(s) until all sources succeed or fail. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common and resulting value type * @param sources the {@code Flowable} sequence of {@code SingleSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 2.2 * @see #merge(Publisher) */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableFlatMapSinglePublisher<>(sources, Functions.identity(), true, Integer.MAX_VALUE)); } /** * Flattens two {@link SingleSource}s into one {@link Flowable}, without any transformation, delaying * any error(s) until all sources succeed or fail. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as one {@code Flowable}, by * using the {@code mergeDelayError} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Merge * @see #merge(SingleSource, SingleSource) * @since 2.2 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError( @NonNull SingleSource source1, @NonNull SingleSource source2 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return Flowable.fromArray(source1, source2).flatMapSingle(Functions.identity(), true, Integer.MAX_VALUE); } /** * Flattens two {@link SingleSource}s into one {@link Flowable}, without any transformation, delaying * any error(s) until all sources succeed or fail. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as one {@code Flowable}, by * the {@code mergeDelayError} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @param source3 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Merge * @see #merge(SingleSource, SingleSource, SingleSource) * @since 2.2 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return Flowable.fromArray(source1, source2, source3).flatMapSingle(Functions.identity(), true, Integer.MAX_VALUE); } /** * Flattens two {@link SingleSource}s into one {@link Flowable}, without any transformation, delaying * any error(s) until all sources succeed or fail. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as one {@code Flowable}, by * the {@code mergeDelayError} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common value type * @param source1 * a {@code SingleSource} to be merged * @param source2 * a {@code SingleSource} to be merged * @param source3 * a {@code SingleSource} to be merged * @param source4 * a {@code SingleSource} to be merged * @return the new {@code Flowable} that emits all of the items emitted by the source {@code SingleSource}s * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Merge * @see #merge(SingleSource, SingleSource, SingleSource, SingleSource) * @since 2.2 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return Flowable.fromArray(source1, source2, source3, source4).flatMapSingle(Functions.identity(), true, Integer.MAX_VALUE); } /** * Returns a singleton instance of a never-signaling {@code Single} (only calls {@code onSubscribe}). *

* *

*
Scheduler:
*
{@code never} does not operate by default on a particular {@link Scheduler}.
*
* @param the target value type * @return the singleton never instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public static <@NonNull T> Single never() { return RxJavaPlugins.onAssembly((Single) SingleNever.INSTANCE); } /** * Signals success with 0L value after the given delay when a {@link SingleObserver} subscribes. *

* *

*
Scheduler:
*
{@code timer} operates by default on the {@code computation} {@link Scheduler}.
*
* @param delay the delay amount * @param unit the time unit of the delay * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public static Single timer(long delay, @NonNull TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); } /** * Signals success with 0L value on the specified {@link Scheduler} after the given * delay when a {@link SingleObserver} subscribes. *

* *

*
Scheduler:
*
you specify the {@code Scheduler} to signal on.
*
* @param delay the delay amount * @param unit the time unit of the delay * @param scheduler the {@code Scheduler} where the single 0L will be emitted * @return the new {@code Single} instance * @throws NullPointerException * if {@code unit} is {@code null}, or * if {@code scheduler} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public static Single timer(long delay, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleTimer(delay, unit, scheduler)); } /** * Compares two {@link SingleSource}s and emits {@code true} if they emit the same value (compared via {@link Object#equals(Object)}). *

* *

*
Scheduler:
*
{@code sequenceEqual} does not operate by default on a particular {@link Scheduler}.
*
* @param the common value type * @param source1 the first {@code SingleSource} instance * @param source2 the second {@code SingleSource} instance * @return the new {@code Single} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single sequenceEqual(@NonNull SingleSource source1, @NonNull SingleSource source2) { // NOPMD Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return RxJavaPlugins.onAssembly(new SingleEquals<>(source1, source2)); } /** * Switches between {@link SingleSource}s emitted by the source {@link Publisher} whenever * a new {@code SingleSource} is emitted, disposing the previously running {@code SingleSource}, * exposing the success items as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The {@code sources} {@code Publisher} is consumed in an unbounded manner (requesting {@link Long#MAX_VALUE}). * The returned {@code Flowable} respects the backpressure from the downstream.
*
Scheduler:
*
{@code switchOnNext} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
The returned sequence fails with the first error signaled by the {@code sources} {@code Publisher} * or the currently running {@code SingleSource}, disposing the rest. Late errors are * forwarded to the global error handler via {@link RxJavaPlugins#onError(Throwable)}.
*
* @param the element type of the {@code SingleSource}s * @param sources the {@code Publisher} sequence of inner {@code SingleSource}s to switch between * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 * @see #switchOnNextDelayError(Publisher) * @see ReactiveX operators documentation: Switch */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable switchOnNext(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableSwitchMapSinglePublisher<>(sources, Functions.identity(), false)); } /** * Switches between {@link SingleSource}s emitted by the source {@link Publisher} whenever * a new {@code SingleSource} is emitted, disposing the previously running {@code SingleSource}, * exposing the success items as a {@link Flowable} sequence and delaying all errors from * all of them until all terminate. *

* *

*
Backpressure:
*
The {@code sources} {@code Publisher} is consumed in an unbounded manner (requesting {@link Long#MAX_VALUE}). * The returned {@code Flowable} respects the backpressure from the downstream.
*
Scheduler:
*
{@code switchOnNextDelayError} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
The returned {@code Flowable} collects all errors emitted by either the {@code sources} * {@code Publisher} or any inner {@code SingleSource} and emits them as a {@link CompositeException} * when all sources terminate. If only one source ever failed, its error is emitted as-is at the end.
*
* @param the element type of the {@code SingleSource}s * @param sources the {@code Publisher} sequence of inner {@code SingleSource}s to switch between * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 * @see #switchOnNext(Publisher) * @see ReactiveX operators documentation: Switch */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable switchOnNextDelayError(@NonNull Publisher<@NonNull ? extends SingleSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableSwitchMapSinglePublisher<>(sources, Functions.identity(), true)); } /** * Advanced use only: creates a {@code Single} instance without * any safeguards by using a callback that is called with a {@link SingleObserver}. *

* *

*
Scheduler:
*
{@code unsafeCreate} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param onSubscribe the function that is called with the subscribing {@code SingleObserver} * @return the new {@code Single} instance * @throws NullPointerException if {@code onSubscribe} is {@code null} * @throws IllegalArgumentException if {@code source} is a subclass of {@code Single}; such * instances don't need conversion and is possibly a port remnant from 1.x or one should use {@link #hide()} * instead. * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single unsafeCreate(@NonNull SingleSource onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); if (onSubscribe instanceof Single) { throw new IllegalArgumentException("unsafeCreate(Single) should be upgraded"); } return RxJavaPlugins.onAssembly(new SingleFromUnsafeSource<>(onSubscribe)); } /** * Allows using and disposing a resource while running a {@link SingleSource} instance generated from * that resource (similar to a try-with-resources). *

* *

*
Scheduler:
*
{@code using} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the {@code SingleSource} generated * @param the resource type * @param resourceSupplier the {@link Supplier} called for each {@link SingleObserver} to generate a resource object * @param sourceSupplier the function called with the returned resource * object from {@code resourceSupplier} and should return a {@code SingleSource} instance * to be run by the operator * @param resourceCleanup the consumer of the generated resource that is called exactly once for * that particular resource when the generated {@code SingleSource} terminates * (successfully or with an error) or gets disposed. * @return the new {@code Single} instance * @throws NullPointerException if {@code resourceSupplier}, {@code sourceSupplier} and {@code resourceCleanup} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T, @NonNull U> Single using(@NonNull Supplier resourceSupplier, @NonNull Function> sourceSupplier, @NonNull Consumer resourceCleanup) { return using(resourceSupplier, sourceSupplier, resourceCleanup, true); } /** * Allows using and disposing a resource while running a {@link SingleSource} instance generated from * that resource (similar to a try-with-resources). *

* *

*
Scheduler:
*
{@code using} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the {@code SingleSource} generated * @param the resource type * @param resourceSupplier the {@link Supplier} called for each {@link SingleObserver} to generate a resource object * @param sourceSupplier the function called with the returned resource * object from {@code resourceSupplier} and should return a {@code SingleSource} instance * to be run by the operator * @param resourceCleanup the consumer of the generated resource that is called exactly once for * that particular resource when the generated {@code SingleSource} terminates * (successfully or with an error) or gets disposed. * @param eager * If {@code true} then resource disposal will happen either on a {@code dispose()} call before the upstream is disposed * or just before the emission of a terminal event ({@code onSuccess} or {@code onError}). * If {@code false} the resource disposal will happen either on a {@code dispose()} call after the upstream is disposed * or just after the emission of a terminal event ({@code onSuccess} or {@code onError}). * @return the new {@code Single} instance * @throws NullPointerException if {@code resourceSupplier}, {@code sourceSupplier} or {@code resourceCleanup} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T, @NonNull U> Single using( @NonNull Supplier resourceSupplier, @NonNull Function> sourceSupplier, @NonNull Consumer resourceCleanup, boolean eager) { Objects.requireNonNull(resourceSupplier, "resourceSupplier is null"); Objects.requireNonNull(sourceSupplier, "sourceSupplier is null"); Objects.requireNonNull(resourceCleanup, "resourceCleanup is null"); return RxJavaPlugins.onAssembly(new SingleUsing<>(resourceSupplier, sourceSupplier, resourceCleanup, eager)); } /** * Wraps a {@link SingleSource} instance into a new {@code Single} instance if not already a {@code Single} * instance. *

* *

*
Scheduler:
*
{@code wrap} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param source the source to wrap * @return the new {@code Single} instance * @throws NullPointerException if {@code source} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single wrap(@NonNull SingleSource source) { Objects.requireNonNull(source, "source is null"); if (source instanceof Single) { return RxJavaPlugins.onAssembly((Single)source); } return RxJavaPlugins.onAssembly(new SingleFromUnsafeSource<>(source)); } /** * Waits until all {@link SingleSource} sources provided by the {@link Iterable} sequence signal a success * value and calls a zipper function with an array of these values to return a result * to be emitted to the downstream. *

* If the {@code Iterable} of {@code SingleSource}s is empty a {@link NoSuchElementException} error is signaled after subscription. *

* Note on method signature: since Java doesn't allow creating a generic array with {@code new T[]}, the * implementation of this operator has to create an {@code Object[]} instead. Unfortunately, a * {@code Function} passed to the method would trigger a {@link ClassCastException}. * *

* *

* If any of the {@code SingleSources} signal an error, all other {@code SingleSource}s get disposed and the * error emitted to downstream immediately. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* @param the common value type * @param the result value type * @param sources the {@code Iterable} sequence of {@code SingleSource} instances. An empty sequence will result in an * {@code onError} signal of {@code NoSuchElementException}. * @param zipper the function that receives an array with values from each {@code SingleSource} * and should return a value to be emitted to downstream * @return the new {@code Single} instance * @throws NullPointerException if {@code zipper} or {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T, @NonNull R> Single zip(@NonNull Iterable<@NonNull ? extends SingleSource> sources, @NonNull Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new SingleZipIterable<>(sources, zipper)); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to two items emitted by * two other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull BiFunction zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to three items emitted * by three other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull Function3 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to four items * emitted by four other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull Function4 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to five items * emitted by five other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the fifth source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param source5 * a fifth source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} * {@code source5} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull SingleSource source5, @NonNull Function5 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to six items * emitted by six other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the fifth source {@code SingleSource}'s value type * @param the sixth source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param source5 * a fifth source {@code SingleSource} * @param source6 * a sixth source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} * {@code source5}, {@code source6} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull SingleSource source5, @NonNull SingleSource source6, @NonNull Function6 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to seven items * emitted by seven other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the fifth source {@code SingleSource}'s value type * @param the sixth source {@code SingleSource}'s value type * @param the seventh source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param source5 * a fifth source {@code SingleSource} * @param source6 * a sixth source {@code SingleSource} * @param source7 * a seventh source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} * {@code source5}, {@code source6}, {@code source7} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull SingleSource source5, @NonNull SingleSource source6, @NonNull SingleSource source7, @NonNull Function7 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to eight items * emitted by eight other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the fifth source {@code SingleSource}'s value type * @param the sixth source {@code SingleSource}'s value type * @param the seventh source {@code SingleSource}'s value type * @param the eighth source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param source5 * a fifth source {@code SingleSource} * @param source6 * a sixth source {@code SingleSource} * @param source7 * a seventh source {@code SingleSource} * @param source8 * an eighth source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} * {@code source5}, {@code source6}, {@code source7}, {@code source8} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull T8, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull SingleSource source5, @NonNull SingleSource source6, @NonNull SingleSource source7, @NonNull SingleSource source8, @NonNull Function8 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(source8, "source8 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7, source8); } /** * Returns a {@code Single} that emits the results of a specified combiner function applied to nine items * emitted by nine other {@link SingleSource}s. *

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source {@code SingleSource}'s value type * @param the second source {@code SingleSource}'s value type * @param the third source {@code SingleSource}'s value type * @param the fourth source {@code SingleSource}'s value type * @param the fifth source {@code SingleSource}'s value type * @param the sixth source {@code SingleSource}'s value type * @param the seventh source {@code SingleSource}'s value type * @param the eighth source {@code SingleSource}'s value type * @param the ninth source {@code SingleSource}'s value type * @param the result value type * @param source1 * the first source {@code SingleSource} * @param source2 * a second source {@code SingleSource} * @param source3 * a third source {@code SingleSource} * @param source4 * a fourth source {@code SingleSource} * @param source5 * a fifth source {@code SingleSource} * @param source6 * a sixth source {@code SingleSource} * @param source7 * a seventh source {@code SingleSource} * @param source8 * an eighth source {@code SingleSource} * @param source9 * a ninth source {@code SingleSource} * @param zipper * a function that, when applied to the item emitted by each of the source {@code SingleSource}s, results in an * item that will be emitted by the resulting {@code Single} * @return the new {@code Single} that emits the zipped results * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, {@code source4} * {@code source5}, {@code source6}, {@code source7}, {@code source8}, * {@code source9} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull T8, @NonNull T9, @NonNull R> Single zip( @NonNull SingleSource source1, @NonNull SingleSource source2, @NonNull SingleSource source3, @NonNull SingleSource source4, @NonNull SingleSource source5, @NonNull SingleSource source6, @NonNull SingleSource source7, @NonNull SingleSource source8, @NonNull SingleSource source9, @NonNull Function9 zipper ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(source8, "source8 is null"); Objects.requireNonNull(source9, "source9 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7, source8, source9); } /** * Waits until all {@link SingleSource} sources provided via an array signal a success * value and calls a zipper function with an array of these values to return a result * to be emitted to downstream. *

* *

* If the array of {@code SingleSource}s is empty a {@link NoSuchElementException} error is signaled immediately. *

* Note on method signature: since Java doesn't allow creating a generic array with {@code new T[]}, the * implementation of this operator has to create an {@code Object[]} instead. Unfortunately, a * {@code Function} passed to the method would trigger a {@link ClassCastException}. *

* If any of the {@code SingleSource}s signal an error, all other {@code SingleSource}s get disposed and the * error emitted to downstream immediately. *

*
Scheduler:
*
{@code zipArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the common value type * @param the result value type * @param sources the array of {@code SingleSource} instances. An empty sequence will result in an * {@code onError} signal of {@code NoSuchElementException}. * @param zipper the function that receives an array with values from each {@code SingleSource} * and should return a value to be emitted to downstream * @return the new {@code Single} instance * @throws NullPointerException if {@code zipper} or {@code sources} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T, @NonNull R> Single zipArray(@NonNull Function zipper, @NonNull SingleSource... sources) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return error(new NoSuchElementException()); } return RxJavaPlugins.onAssembly(new SingleZipArray<>(sources, zipper)); } /** * Signals the event of this or the other {@link SingleSource} whichever signals first. *

* *

*
Scheduler:
*
{@code ambWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code SingleSource} to race for the first emission of success or error * @return the new {@code Single} instance. A subscription to this provided source will occur after subscribing * to the current source. * @throws NullPointerException if {@code other} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single ambWith(@NonNull SingleSource other) { Objects.requireNonNull(other, "other is null"); return ambArray(this, other); } /** * Hides the identity of the current {@code Single}, including the {@link Disposable} that is sent * to the downstream via {@code onSubscribe()}. *

* *

*
Scheduler:
*
{@code hide} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Single} instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single hide() { return RxJavaPlugins.onAssembly(new SingleHide<>(this)); } /** * Transform a {@code Single} by applying a particular {@link SingleTransformer} function to it. *

* *

* This method operates on the {@code Single} itself whereas {@link #lift} operates on {@link SingleObserver}s. *

* If the operator you are creating is designed to act on the individual item emitted by a {@code Single}, use * {@link #lift}. If your operator is designed to transform the current {@code Single} as a whole (for instance, by * applying a particular set of existing RxJava operators to it) use {@code compose}. *

*
Scheduler:
*
{@code compose} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the single returned by the transformer function * @param transformer the transformer function, not {@code null} * @return the new {@code Single} instance * @throws NullPointerException if {@code transformer} is {@code null} * @see RxJava wiki: Implementing Your Own Operators */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Single compose(@NonNull SingleTransformer transformer) { return wrap(((SingleTransformer) Objects.requireNonNull(transformer, "transformer is null")).apply(this)); } /** * Stores the success value or exception from the current {@code Single} and replays it to late {@link SingleObserver}s. *

* *

* The returned {@code Single} subscribes to the current {@code Single} when the first {@code SingleObserver} subscribes. *

*
Scheduler:
*
{@code cache} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Single} instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single cache() { return RxJavaPlugins.onAssembly(new SingleCache<>(this)); } /** * Casts the success value of the current {@code Single} into the target type or signals a * {@link ClassCastException} if not compatible. *

* *

*
Scheduler:
*
{@code cast} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param clazz the type token to use for casting the success result from the current {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code clazz} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Single cast(@NonNull Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return map(Functions.castFunction(clazz)); } /** * Returns a {@code Single} that is based on applying a specified function to the item emitted by the current {@code Single}, * where that function returns a {@link SingleSource}. *

* *

* The operator is an alias for {@link #flatMap(Function)} *

*
Scheduler:
*
{@code concatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a {@code SingleSource} * @return the new {@code Single} returned from {@code mapper} when applied to the item emitted by the current {@code Single} * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Single concatMap(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMap<>(this, mapper)); } /** * Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the * current {@code Single}, where that function returns a {@link CompletableSource}. *

* *

* The operator is an alias for {@link #flatMapCompletable(Function)}. *

*
Scheduler:
*
{@code concatMapCompletable} does not operate by default on a particular {@link Scheduler}.
*
* * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a * {@code CompletableSource} * @return the new {@code Completable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Completable concatMapCompletable(@NonNull Function mapper) { return flatMapCompletable(mapper); } /** * Returns a {@link Maybe} that is based on applying a specified function to the item emitted by the current {@code Single}, * where that function returns a {@link MaybeSource}. *

* *

* The operator is an alias for {@link #flatMapMaybe(Function)}. *

*
Scheduler:
*
{@code concatMapMaybe} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a {@code MaybeSource} * @return the new {@code Maybe} returned from {@code mapper} when applied to the item emitted by the current {@code Single} * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe concatMapMaybe(@NonNull Function> mapper) { return flatMapMaybe(mapper); } /** * Returns a {@link Flowable} that emits the item emitted by the current {@code Single}, then the item emitted by the * specified {@link SingleSource}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concatWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a {@code SingleSource} to be concatenated after the current * @return the new {@code Flowable} that emits the item emitted by the current {@code Single}, followed by the item emitted by * {@code other} * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: Concat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable concatWith(@NonNull SingleSource other) { return concat(this, other); } /** * Delays the emission of the success signal from the current {@code Single} by the specified amount. * An error signal will not be delayed. *

* *

*
Scheduler:
*
{@code delay} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param time the amount of time the success signal should be delayed for * @param unit the time unit * @return the new {@code Single} instance * @since 2.0 * @throws NullPointerException if {@code unit} is {@code null} * @see #delay(long, TimeUnit, boolean) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Single delay(long time, @NonNull TimeUnit unit) { return delay(time, unit, Schedulers.computation(), false); } /** * Delays the emission of the success or error signal from the current {@code Single} by the specified amount. *

* *

*
Scheduler:
*
{@code delay} operates by default on the {@code computation} {@link Scheduler}.
*
*

History: 2.1.5 - experimental * @param time the amount of time the success or error signal should be delayed for * @param unit the time unit * @param delayError if {@code true}, both success and error signals are delayed. if {@code false}, only success signals are delayed. * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 2.2 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Single delay(long time, @NonNull TimeUnit unit, boolean delayError) { return delay(time, unit, Schedulers.computation(), delayError); } /** * Delays the emission of the success signal from the current {@code Single} by the specified amount. * An error signal will not be delayed. *

* *

*
Scheduler:
*
you specify the {@link Scheduler} where the non-blocking wait and emission happens
*
* * @param time the amount of time the success signal should be delayed for * @param unit the time unit * @param scheduler the target scheduler to use for the non-blocking wait and emission * @return the new {@code Single} instance * @throws NullPointerException * if {@code unit} is {@code null}, or * if {@code scheduler} is {@code null} * @since 2.0 * @see #delay(long, TimeUnit, Scheduler, boolean) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Single delay(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return delay(time, unit, scheduler, false); } /** * Delays the emission of the success or error signal from the current {@code Single} by the specified amount. *

* *

*
Scheduler:
*
you specify the {@link Scheduler} where the non-blocking wait and emission happens
*
*

History: 2.1.5 - experimental * @param time the amount of time the success or error signal should be delayed for * @param unit the time unit * @param scheduler the target scheduler to use for the non-blocking wait and emission * @param delayError if {@code true}, both success and error signals are delayed. if {@code false}, only success signals are delayed. * @return the new {@code Single} instance * @throws NullPointerException * if {@code unit} is {@code null}, or * if {@code scheduler} is {@code null} * @since 2.2 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single delay(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler, boolean delayError) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleDelay<>(this, time, unit, scheduler, delayError)); } /** * Delays the actual subscription to the current {@code Single} until the given other {@link CompletableSource} * completes. *

* *

If the delaying source signals an error, that error is re-emitted and no subscription * to the current {@code Single} happens. *

*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param subscriptionIndicator the {@code CompletableSource} that has to complete before the subscription to the * current {@code Single} happens * @return the new {@code Single} instance * @throws NullPointerException if {@code subscriptionIndicator} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single delaySubscription(@NonNull CompletableSource subscriptionIndicator) { Objects.requireNonNull(subscriptionIndicator, "subscriptionIndicator is null"); return RxJavaPlugins.onAssembly(new SingleDelayWithCompletable<>(this, subscriptionIndicator)); } /** * Delays the actual subscription to the current {@code Single} until the given other {@link SingleSource} * signals success. *

* *

If the delaying source signals an error, that error is re-emitted and no subscription * to the current {@code Single} happens. *

*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the other source * @param subscriptionIndicator the {@code SingleSource} that has to complete before the subscription to the * current {@code Single} happens * @return the new {@code Single} instance * @throws NullPointerException if {@code subscriptionIndicator} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Single delaySubscription(@NonNull SingleSource subscriptionIndicator) { Objects.requireNonNull(subscriptionIndicator, "subscriptionIndicator is null"); return RxJavaPlugins.onAssembly(new SingleDelayWithSingle<>(this, subscriptionIndicator)); } /** * Delays the actual subscription to the current {@code Single} until the given other {@link ObservableSource} * signals its first value or completes. *

* *

If the delaying source signals an error, that error is re-emitted and no subscription * to the current {@code Single} happens. *

*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the other source * @param subscriptionIndicator the {@code ObservableSource} that has to signal a value or complete before the * subscription to the current {@code Single} happens * @return the new {@code Single} instance * @throws NullPointerException if {@code subscriptionIndicator} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Single delaySubscription(@NonNull ObservableSource subscriptionIndicator) { Objects.requireNonNull(subscriptionIndicator, "subscriptionIndicator is null"); return RxJavaPlugins.onAssembly(new SingleDelayWithObservable<>(this, subscriptionIndicator)); } /** * Delays the actual subscription to the current {@code Single} until the given other {@link Publisher} * signals its first value or completes. *

* *

If the delaying source signals an error, that error is re-emitted and no subscription * to the current {@code Single} happens. *

The other source is consumed in an unbounded manner (requesting {@link Long#MAX_VALUE} from it). *

*
Backpressure:
*
The {@code other} publisher is consumed in an unbounded fashion but will be * cancelled after the first item it produced.
*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the other source * @param subscriptionIndicator the {@code Publisher} that has to signal a value or complete before the * subscription to the current {@code Single} happens * @return the new {@code Single} instance * @throws NullPointerException if {@code subscriptionIndicator} is {@code null} * @since 2.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Single delaySubscription(@NonNull Publisher subscriptionIndicator) { Objects.requireNonNull(subscriptionIndicator, "subscriptionIndicator is null"); return RxJavaPlugins.onAssembly(new SingleDelayWithPublisher<>(this, subscriptionIndicator)); } /** * Delays the actual subscription to the current {@code Single} until the given time delay elapsed. *

* *

*
Scheduler:
*
{@code delaySubscription} does by default subscribe to the current {@code Single} * on the {@code computation} {@link Scheduler} after the delay.
*
* @param time the time amount to wait with the subscription * @param unit the time unit of the waiting * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Single delaySubscription(long time, @NonNull TimeUnit unit) { return delaySubscription(time, unit, Schedulers.computation()); } /** * Delays the actual subscription to the current {@code Single} until the given time delay elapsed. *

* *

*
Scheduler:
*
{@code delaySubscription} does by default subscribe to the current {@code Single} * on the {@link Scheduler} you provided, after the delay.
*
* @param time the time amount to wait with the subscription * @param unit the time unit of the waiting * @param scheduler the {@code Scheduler} to wait on and subscribe on to the current {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Single delaySubscription(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return delaySubscription(Observable.timer(time, unit, scheduler)); } /** * Maps the {@link Notification} success value of the current {@code Single} back into normal * {@code onSuccess}, {@code onError} or {@code onComplete} signals as a * {@link Maybe} source. *

* *

* The intended use of the {@code selector} function is to perform a * type-safe identity mapping (see example) on a source that is already of type * {@code Notification}. The Java language doesn't allow * limiting instance methods to a certain generic argument shape, therefore, * a function is used to ensure the conversion remains type safe. *

*
Scheduler:
*
{@code dematerialize} does not operate by default on a particular {@link Scheduler}.
*
*

* Example: *


     * Single.just(Notification.createOnNext(1))
     * .dematerialize(notification -> notification)
     * .test()
     * .assertResult(1);
     * 
*

History: 2.2.4 - experimental * @param the result type * @param selector the function called with the success item and should * return a {@code Notification} instance. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code selector} is {@code null} * @since 3.0.0 * @see #materialize() */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe dematerialize(@NonNull Function> selector) { Objects.requireNonNull(selector, "selector is null"); return RxJavaPlugins.onAssembly(new SingleDematerialize<>(this, selector)); } /** * Calls the specified consumer with the success item after this item has been emitted to the downstream. *

* *

* Note that the {@code doAfterSuccess} action is shared between subscriptions and as such * should be thread-safe. *

*
Scheduler:
*
{@code doAfterSuccess} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.0.1 - experimental * @param onAfterSuccess the {@link Consumer} that will be called after emitting an item from upstream to the downstream * @return the new {@code Single} instance * @throws NullPointerException if {@code onAfterSuccess} is {@code null} * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doAfterSuccess(@NonNull Consumer onAfterSuccess) { Objects.requireNonNull(onAfterSuccess, "onAfterSuccess is null"); return RxJavaPlugins.onAssembly(new SingleDoAfterSuccess<>(this, onAfterSuccess)); } /** * Registers an {@link Action} to be called after this {@code Single} invokes either {@code onSuccess} or {@code onError}. *

* *

* Note that the {@code doAfterTerminate} action is shared between subscriptions and as such * should be thread-safe.

* *
*
Scheduler:
*
{@code doAfterTerminate} does not operate by default on a particular {@link Scheduler}.
*
* *

History: 2.0.6 - experimental * @param onAfterTerminate * an {@code Action} to be invoked when the current {@code Single} finishes * @return the new {@code Single} that emits the same items as the current {@code Single}, then invokes the * {@code Action} * @throws NullPointerException if {@code onAfterTerminate} is {@code null} * @see ReactiveX operators documentation: Do * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doAfterTerminate(@NonNull Action onAfterTerminate) { Objects.requireNonNull(onAfterTerminate, "onAfterTerminate is null"); return RxJavaPlugins.onAssembly(new SingleDoAfterTerminate<>(this, onAfterTerminate)); } /** * Calls the specified action after this {@code Single} signals {@code onSuccess} or {@code onError} or gets disposed by * the downstream. *

In case of a race between a terminal event and a dispose call, the provided {@code onFinally} action * is executed once per subscription. *

Note that the {@code onFinally} action is shared between subscriptions and as such * should be thread-safe. *

* *

*
*
Scheduler:
*
{@code doFinally} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.0.1 - experimental * @param onFinally the action called when this {@code Single} terminates or gets disposed * @return the new {@code Single} instance * @throws NullPointerException if {@code onFinally} is {@code null} * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doFinally(@NonNull Action onFinally) { Objects.requireNonNull(onFinally, "onFinally is null"); return RxJavaPlugins.onAssembly(new SingleDoFinally<>(this, onFinally)); } /** * Calls the appropriate {@code onXXX} method (shared between all {@link SingleObserver}s) for the lifecycle events of * the sequence (subscription, disposal). *

* *

*
Scheduler:
*
{@code doOnLifecycle} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSubscribe * a {@link Consumer} called with the {@link Disposable} sent via {@link SingleObserver#onSubscribe(Disposable)} * @param onDispose * called when the downstream disposes the {@code Disposable} via {@code dispose()} * @return the new {@code Single} instance * @throws NullPointerException if {@code onSubscribe} or {@code onDispose} is {@code null} * @see ReactiveX operators documentation: Do * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single doOnLifecycle(@NonNull Consumer onSubscribe, @NonNull Action onDispose) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); Objects.requireNonNull(onDispose, "onDispose is null"); return RxJavaPlugins.onAssembly(new SingleDoOnLifecycle<>(this, onSubscribe, onDispose)); } /** * Calls the shared consumer with the {@link Disposable} sent through the {@code onSubscribe} for each * {@link SingleObserver} that subscribes to the current {@code Single}. *

* *

*
*
Scheduler:
*
{@code doOnSubscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param onSubscribe the consumer called with the {@code Disposable} sent via {@code onSubscribe} * @return the new {@code Single} instance * @throws NullPointerException if {@code onSubscribe} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnSubscribe(@NonNull Consumer onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); return RxJavaPlugins.onAssembly(new SingleDoOnSubscribe<>(this, onSubscribe)); } /** * Returns a {@code Single} instance that calls the given {@code onTerminate} callback * just before this {@code Single} completes normally or with an exception. *

* *

* This differs from {@code doAfterTerminate} in that this happens before the {@code onSuccess} or * {@code onError} notification. *

*
Scheduler:
*
{@code doOnTerminate} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.2.7 - experimental * @param onTerminate the action to invoke when the consumer calls {@code onSuccess} or {@code onError} * @return the new {@code Single} instance * @throws NullPointerException if {@code onTerminate} is {@code null} * @see ReactiveX operators documentation: Do * @see #doOnTerminate(Action) * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnTerminate(@NonNull Action onTerminate) { Objects.requireNonNull(onTerminate, "onTerminate is null"); return RxJavaPlugins.onAssembly(new SingleDoOnTerminate<>(this, onTerminate)); } /** * Calls the shared consumer with the success value sent via {@code onSuccess} for each * {@link SingleObserver} that subscribes to the current {@code Single}. *

* *

*
*
Scheduler:
*
{@code doOnSuccess} does not operate by default on a particular {@link Scheduler}.
*
* @param onSuccess the consumer called with the success value of {@code onSuccess} * @return the new {@code Single} instance * @throws NullPointerException if {@code onSuccess} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnSuccess(@NonNull Consumer onSuccess) { Objects.requireNonNull(onSuccess, "onSuccess is null"); return RxJavaPlugins.onAssembly(new SingleDoOnSuccess<>(this, onSuccess)); } /** * Calls the shared consumer with the error sent via {@code onError} or the value * via {@code onSuccess} for each {@link SingleObserver} that subscribes to the current {@code Single}. *

* *

*
Scheduler:
*
{@code doOnEvent} does not operate by default on a particular {@link Scheduler}.
*
* @param onEvent the consumer called with the success value of onEvent * @return the new {@code Single} instance * @throws NullPointerException if {@code onEvent} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnEvent(@NonNull BiConsumer<@Nullable ? super T, @Nullable ? super Throwable> onEvent) { Objects.requireNonNull(onEvent, "onEvent is null"); return RxJavaPlugins.onAssembly(new SingleDoOnEvent<>(this, onEvent)); } /** * Calls the shared consumer with the error sent via {@code onError} for each * {@link SingleObserver} that subscribes to the current {@code Single}. *

* *

*
*
Scheduler:
*
{@code doOnError} does not operate by default on a particular {@link Scheduler}.
*
* @param onError the consumer called with the success value of {@code onError} * @return the new {@code Single} instance * @throws NullPointerException if {@code onError} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnError(@NonNull Consumer onError) { Objects.requireNonNull(onError, "onError is null"); return RxJavaPlugins.onAssembly(new SingleDoOnError<>(this, onError)); } /** * Calls the shared {@link Action} if a {@link SingleObserver} subscribed to the current {@code Single} * disposes the common {@link Disposable} it received via {@code onSubscribe}. *

* *

*
*
Scheduler:
*
{@code doOnDispose} does not operate by default on a particular {@link Scheduler}.
*
* @param onDispose the action called when the subscription is disposed * @return the new {@code Single} instance * @throws NullPointerException if {@code onDispose} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnDispose(@NonNull Action onDispose) { Objects.requireNonNull(onDispose, "onDispose is null"); return RxJavaPlugins.onAssembly(new SingleDoOnDispose<>(this, onDispose)); } /** * Filters the success item of the {@code Single} via a predicate function and emitting it if the predicate * returns {@code true}, completing otherwise. *

* *

*
Scheduler:
*
{@code filter} does not operate by default on a particular {@link Scheduler}.
*
* * @param predicate * a function that evaluates the item emitted by the current {@code Single}, returning {@code true} * if it passes the filter * @return the new {@link Maybe} that emit the item emitted by the current {@code Single} that the filter * evaluates as {@code true} * @throws NullPointerException if {@code predicate} is {@code null} * @see ReactiveX operators documentation: Filter */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe filter(@NonNull Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return RxJavaPlugins.onAssembly(new MaybeFilterSingle<>(this, predicate)); } /** * Returns a {@code Single} that is based on applying a specified function to the item emitted by the current {@code Single}, * where that function returns a {@link SingleSource}. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a {@code SingleSource} * @return the new {@code Single} returned from {@code mapper} when applied to the item emitted by the current {@code Single} * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Single flatMap(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMap<>(this, mapper)); } /** * Returns a {@code Single} that emits the results of a specified function to the pair of values emitted by the * current {@code Single} and a specified mapped {@link SingleSource}. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items emitted by the {@code SingleSource} returned by the {@code mapper} function * @param * the type of items emitted by the resulting {@code Single} * @param mapper * a function that returns a {@code SingleSource} for the item emitted by the current {@code Single} * @param combiner * a function that combines one item emitted by each of the source and collection {@code SingleSource} and * returns an item to be emitted by the resulting {@code SingleSource} * @return the new {@code Single} instance * @throws NullPointerException if {@code mapper} or {@code combiner} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U, @NonNull R> Single flatMap(@NonNull Function> mapper, @NonNull BiFunction combiner) { Objects.requireNonNull(mapper, "mapper is null"); Objects.requireNonNull(combiner, "combiner is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapBiSelector<>(this, mapper, combiner)); } /** * Maps the {@code onSuccess} or {@code onError} signals of the current {@code Single} into a {@link SingleSource} and emits that * {@code SingleSource}'s signals. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the result type * @param onSuccessMapper * a function that returns a {@code SingleSource} to merge for the {@code onSuccess} item emitted by this {@code Single} * @param onErrorMapper * a function that returns a {@code SingleSource} to merge for an {@code onError} notification from this {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code onSuccessMapper} or {@code onErrorMapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Single flatMap( @NonNull Function> onSuccessMapper, @NonNull Function> onErrorMapper) { Objects.requireNonNull(onSuccessMapper, "onSuccessMapper is null"); Objects.requireNonNull(onErrorMapper, "onErrorMapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapNotification<>(this, onSuccessMapper, onErrorMapper)); } /** * Returns a {@link Maybe} that is based on applying a specified function to the item emitted by the current {@code Single}, * where that function returns a {@link MaybeSource}. *

* *

*
Scheduler:
*
{@code flatMapMaybe} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a {@code MaybeSource} * @return the new {@code Maybe} returned from {@code mapper} when applied to the item emitted by the current {@code Single} * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe flatMapMaybe(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapMaybe<>(this, mapper)); } /** * Returns a {@link Flowable} that emits items based on applying a specified function to the item emitted by the * current {@code Single}, where that function returns a {@link Publisher}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer * and the {@code Publisher} returned by the mapper function is expected to honor it as well.
*
Scheduler:
*
{@code flatMapPublisher} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a * {@code Publisher} * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Flowable flatMapPublisher(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapPublisher<>(this, mapper)); } /** * Maps the success value of the current {@code Single} into an {@link Iterable} and emits its items as a * {@link Flowable} sequence. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code flattenAsFlowable} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of item emitted by the resulting {@code Iterable} * @param mapper * a function that returns an {@code Iterable} sequence of values for when given an item emitted by the * current {@code Single} * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @see #flattenStreamAsFlowable(Function) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Flowable flattenAsFlowable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapIterableFlowable<>(this, mapper)); } /** * Maps the success value of the current {@code Single} into an {@link Iterable} and emits its items as an * {@link Observable} sequence. *

* *

*
Scheduler:
*
{@code flattenAsObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of item emitted by the resulting {@code Iterable} * @param mapper * a function that returns an {@code Iterable} sequence of values for when given an item emitted by the * current {@code Single} * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @see #flattenStreamAsObservable(Function) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Observable flattenAsObservable(@NonNull Function<@NonNull ? super T, @NonNull ? extends Iterable> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapIterableObservable<>(this, mapper)); } /** * Returns an {@link Observable} that is based on applying a specified function to the item emitted by the current {@code Single}, * where that function returns an {@link ObservableSource}. *

* *

*
Scheduler:
*
{@code flatMapObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns an {@code ObservableSource} * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Observable flatMapObservable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapObservable<>(this, mapper)); } /** * Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the * current {@code Single}, where that function returns a {@link CompletableSource}. *

* *

*
Scheduler:
*
{@code flatMapCompletable} does not operate by default on a particular {@link Scheduler}.
*
* * @param mapper * a function that, when applied to the item emitted by the current {@code Single}, returns a * {@code CompletableSource} * @return the new {@code Completable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Completable flatMapCompletable(@NonNull Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlatMapCompletable<>(this, mapper)); } /** * Waits in a blocking fashion until the current {@code Single} signals a success value (which is returned) or * an exception (which is propagated). *

* *

*
Scheduler:
*
{@code blockingGet} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the source signals an error, the operator wraps a checked {@link Exception} * into {@link RuntimeException} and throws that. Otherwise, {@code RuntimeException}s and * {@link Error}s are rethrown as they are.
*
* @return the success value */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final T blockingGet() { BlockingMultiObserver observer = new BlockingMultiObserver<>(); subscribe(observer); return observer.blockingGet(); } /** * Subscribes to the current {@code Single} and blocks the current thread until it terminates. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the current {@code Single} signals an error, * the {@link Throwable} is routed to the global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, an {@link InterruptedException} is routed to the same global error handler. *
*
* @since 3.0.0 * @see #blockingSubscribe(Consumer) * @see #blockingSubscribe(Consumer, Consumer) */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe() { blockingSubscribe(Functions.emptyConsumer(), Functions.ERROR_CONSUMER); } /** * Subscribes to the current {@code Single} and calls given {@code onSuccess} callback on the current thread * when it completes normally. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If either the current {@code Single} signals an error or {@code onSuccess} throws, * the respective {@link Throwable} is routed to the global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, an {@link InterruptedException} is routed to the same global error handler. *
*
* @param onSuccess the {@link Consumer} to call if the current {@code Single} succeeds * @throws NullPointerException if {@code onSuccess} is {@code null} * @since 3.0.0 * @see #blockingSubscribe(Consumer, Consumer) */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull Consumer onSuccess) { blockingSubscribe(onSuccess, Functions.ERROR_CONSUMER); } /** * Subscribes to the current {@code Single} and calls the appropriate callback on the current thread * when it terminates. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If either {@code onSuccess} or {@code onError} throw, the {@link Throwable} is routed to the * global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, the {@code onError} consumer is called with an {@link InterruptedException}. *
*
* @param onSuccess the {@link Consumer} to call if the current {@code Single} succeeds * @param onError the {@code Consumer} to call if the current {@code Single} signals an error * @throws NullPointerException if {@code onSuccess} or {@code onError} is {@code null} * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); BlockingMultiObserver observer = new BlockingMultiObserver<>(); subscribe(observer); observer.blockingConsume(onSuccess, onError, Functions.EMPTY_ACTION); } /** * Subscribes to the current {@code Single} and calls the appropriate {@link SingleObserver} method on the current thread. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
An {@code onError} signal is delivered to the {@link SingleObserver#onError(Throwable)} method. * If any of the {@code SingleObserver}'s methods throw, the {@link RuntimeException} is propagated to the caller of this method. * If the current thread is interrupted, an {@link InterruptedException} is delivered to {@code observer.onError}. *
*
* @param observer the {@code SingleObserver} to call methods on the current thread * @throws NullPointerException if {@code observer} is {@code null} * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull SingleObserver observer) { Objects.requireNonNull(observer, "observer is null"); BlockingDisposableMultiObserver blockingObserver = new BlockingDisposableMultiObserver<>(); observer.onSubscribe(blockingObserver); subscribe(blockingObserver); blockingObserver.blockingConsume(observer); } /** * This method requires advanced knowledge about building operators, please consider * other standard composition methods first; * Returns a {@code Single} which, when subscribed to, invokes the {@link SingleOperator#apply(SingleObserver) apply(SingleObserver)} method * of the provided {@link SingleOperator} for each individual downstream {@link Single} and allows the * insertion of a custom operator by accessing the downstream's {@link SingleObserver} during this subscription phase * and providing a new {@code SingleObserver}, containing the custom operator's intended business logic, that will be * used in the subscription process going further upstream. *

* *

* Generally, such a new {@code SingleObserver} will wrap the downstream's {@code SingleObserver} and forwards the * {@code onSuccess} and {@code onError} events from the upstream directly or according to the * emission pattern the custom operator's business logic requires. In addition, such operator can intercept the * flow control calls of {@code dispose} and {@code isDisposed} that would have traveled upstream and perform * additional actions depending on the same business logic requirements. *

* Example: *


     * // Step 1: Create the consumer type that will be returned by the SingleOperator.apply():
     *
     * public final class CustomSingleObserver<T> implements SingleObserver<T>, Disposable {
     *
     *     // The downstream's SingleObserver that will receive the onXXX events
     *     final SingleObserver<? super String> downstream;
     *
     *     // The connection to the upstream source that will call this class' onXXX methods
     *     Disposable upstream;
     *
     *     // The constructor takes the downstream subscriber and usually any other parameters
     *     public CustomSingleObserver(SingleObserver<? super String> downstream) {
     *         this.downstream = downstream;
     *     }
     *
     *     // In the subscription phase, the upstream sends a Disposable to this class
     *     // and subsequently this class has to send a Disposable to the downstream.
     *     // Note that relaying the upstream's Disposable directly is not allowed in RxJava
     *     @Override
     *     public void onSubscribe(Disposable d) {
     *         if (upstream != null) {
     *             d.dispose();
     *         } else {
     *             upstream = d;
     *             downstream.onSubscribe(this);
     *         }
     *     }
     *
     *     // The upstream calls this with the next item and the implementation's
     *     // responsibility is to emit an item to the downstream based on the intended
     *     // business logic, or if it can't do so for the particular item,
     *     // request more from the upstream
     *     @Override
     *     public void onSuccess(T item) {
     *         String str = item.toString();
     *         if (str.length() < 2) {
     *             downstream.onSuccess(str);
     *         } else {
     *             // Single is usually expected to produce one of the onXXX events
     *             downstream.onError(new NoSuchElementException());
     *         }
     *     }
     *
     *     // Some operators may handle the upstream's error while others
     *     // could just forward it to the downstream.
     *     @Override
     *     public void onError(Throwable throwable) {
     *         downstream.onError(throwable);
     *     }
     *
     *     // Some operators may use their own resources which should be cleaned up if
     *     // the downstream disposes the flow before it completed. Operators without
     *     // resources can simply forward the dispose to the upstream.
     *     // In some cases, a disposed flag may be set by this method so that other parts
     *     // of this class may detect the dispose and stop sending events
     *     // to the downstream.
     *     @Override
     *     public void dispose() {
     *         upstream.dispose();
     *     }
     *
     *     // Some operators may simply forward the call to the upstream while others
     *     // can return the disposed flag set in dispose().
     *     @Override
     *     public boolean isDisposed() {
     *         return upstream.isDisposed();
     *     }
     * }
     *
     * // Step 2: Create a class that implements the SingleOperator interface and
     * //         returns the custom consumer type from above in its apply() method.
     * //         Such class may define additional parameters to be submitted to
     * //         the custom consumer type.
     *
     * final class CustomSingleOperator<T> implements SingleOperator<String> {
     *     @Override
     *     public SingleObserver<? super String> apply(SingleObserver<? super T> upstream) {
     *         return new CustomSingleObserver<T>(upstream);
     *     }
     * }
     *
     * // Step 3: Apply the custom operator via lift() in a flow by creating an instance of it
     * //         or reusing an existing one.
     *
     * Single.just(5)
     * .lift(new CustomSingleOperator<Integer>())
     * .test()
     * .assertResult("5");
     *
     * Single.just(15)
     * .lift(new CustomSingleOperator<Integer>())
     * .test()
     * .assertFailure(NoSuchElementException.class);
     * 
*

* Creating custom operators can be complicated and it is recommended one consults the * RxJava wiki: Writing operators page about * the tools, requirements, rules, considerations and pitfalls of implementing them. *

* Note that implementing custom operators via this {@code lift()} method adds slightly more overhead by requiring * an additional allocation and indirection per assembled flows. Instead, extending the abstract {@code Single} * class and creating a {@link SingleTransformer} with it is recommended. *

* Note also that it is not possible to stop the subscription phase in {@code lift()} as the {@code apply()} method * requires a non-{@code null} {@code SingleObserver} instance to be returned, which is then unconditionally subscribed to * the current {@code Single}. For example, if the operator decided there is no reason to subscribe to the * upstream source because of some optimization possibility or a failure to prepare the operator, it still has to * return a {@code SingleObserver} that should immediately dispose the upstream's {@link Disposable} in its * {@code onSubscribe} method. Again, using a {@code SingleTransformer} and extending the {@code Single} is * a better option as {@link #subscribeActual} can decide to not subscribe to its upstream after all. *

*
Scheduler:
*
{@code lift} does not operate by default on a particular {@link Scheduler}, however, the * {@code SingleOperator} may use a {@code Scheduler} to support its own asynchronous behavior.
*
* * @param the output value type * @param lift the {@code SingleOperator} that receives the downstream's {@code SingleObserver} and should return * a {@code SingleObserver} with custom behavior to be used as the consumer for the current * {@code Single}. * @return the new {@code Single} instance * @throws NullPointerException if {@code lift} is {@code null} * @see RxJava wiki: Writing operators * @see #compose(SingleTransformer) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Single lift(@NonNull SingleOperator lift) { Objects.requireNonNull(lift, "lift is null"); return RxJavaPlugins.onAssembly(new SingleLift<>(this, lift)); } /** * Returns a {@code Single} that applies a specified function to the item emitted by the current {@code Single} and * emits the result of this function application. *

* *

*
Scheduler:
*
{@code map} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function to apply to the item emitted by the {@code Single} * @return the new {@code Single} that emits the item from the current {@code Single}, transformed by the specified function * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: Map */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Single map(@NonNull Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleMap<>(this, mapper)); } /** * Maps the signal types of this {@code Single} into a {@link Notification} of the same kind * and emits it as a single success value to downstream. *

* *

*
Scheduler:
*
{@code materialize} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.2.4 - experimental * @return the new {@code Single} instance * @since 3.0.0 * @see #dematerialize(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single> materialize() { return RxJavaPlugins.onAssembly(new SingleMaterialize<>(this)); } /** * Signals {@code true} if the current {@code Single} signals a success value that is {@link Object#equals(Object)} with the value * provided. *

* *

* *

*
Scheduler:
*
{@code contains} does not operate by default on a particular {@link Scheduler}.
*
* @param item the value to compare against the success value of this {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code item} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single contains(@NonNull Object item) { return contains(item, ObjectHelper.equalsPredicate()); } /** * Signals {@code true} if the current {@code Single} signals a success value that is equal with * the value provided by calling a {@link BiPredicate}. *

* *

*
Scheduler:
*
{@code contains} does not operate by default on a particular {@link Scheduler}.
*
* @param item the value to compare against the success value of this {@code Single} * @param comparer the function that receives the success value of this {@code Single}, the value provided * and should return {@code true} if they are considered equal * @return the new {@code Single} instance * @throws NullPointerException if {@code item} or {@code comparer} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single contains(@NonNull Object item, @NonNull BiPredicate comparer) { Objects.requireNonNull(item, "item is null"); Objects.requireNonNull(comparer, "comparer is null"); return RxJavaPlugins.onAssembly(new SingleContains<>(this, item, comparer)); } /** * Flattens this {@code Single} and another {@link SingleSource} into one {@link Flowable}, without any transformation. *

* *

* You can combine items emitted by multiple {@code SingleSource}s so that they appear as one {@code Flowable}, by using * the {@code mergeWith} method. *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code mergeWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a {@code SingleSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable mergeWith(@NonNull SingleSource other) { return merge(this, other); } /** * Filters the items emitted by the current {@code Single}, only emitting its success value if that * is an instance of the supplied {@link Class}. *

* *

*
Scheduler:
*
{@code ofType} does not operate by default on a particular {@link Scheduler}.
*
* * @param the output type * @param clazz * the class type to filter the items emitted by the current {@code Single} * @return the new {@link Maybe} instance * @throws NullPointerException if {@code clazz} is {@code null} * @see ReactiveX operators documentation: Filter * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe ofType(@NonNull Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return filter(Functions.isInstanceOf(clazz)).cast(clazz); } /** * Signals the success item or the terminal signals of the current {@code Single} on the specified {@link Scheduler}, * asynchronously. *

* *

*
Scheduler:
*
you specify which {@code Scheduler} this operator will use.
*
* * @param scheduler * the {@code Scheduler} to notify subscribers on * @return the new {@code Single} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @see ReactiveX operators documentation: ObserveOn * @see RxJava Threading Examples * @see #subscribeOn */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single observeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleObserveOn<>(this, scheduler)); } /** * Ends the flow with a success item returned by a function for the {@link Throwable} error signaled by the current * {@code Single} instead of signaling the error via {@code onError}. *

* *

* By default, when a {@code Single} encounters an error that prevents it from emitting the expected item to its * subscriber, the {@code Single} invokes its subscriber's {@link SingleObserver#onError} method, and then quits * without invoking any more of its observer's methods. The {@code onErrorReturn} method changes this * behavior. If you pass a function ({@code resumeFunction}) to a {@code Single}'s {@code onErrorReturn} method, if * the original {@code Single} encounters an error, instead of invoking its observer's * {@link SingleObserver#onError} method, it will instead emit the return value of {@code resumeFunction}. *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorReturn} does not operate by default on a particular {@link Scheduler}.
*
* * @param itemSupplier * a function that returns an item that the new {@code Single} will emit if the current {@code Single} encounters * an error * @return the new {@code Single} instance * @throws NullPointerException if {@code itemSupplier} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorReturn(@NonNull Function itemSupplier) { Objects.requireNonNull(itemSupplier, "itemSupplier is null"); return RxJavaPlugins.onAssembly(new SingleOnErrorReturn<>(this, itemSupplier, null)); } /** * Signals the specified value as success in case the current {@code Single} signals an error. *

* *

*
Scheduler:
*
{@code onErrorReturnItem} does not operate by default on a particular {@link Scheduler}.
*
* @param item the value to signal if the current {@code Single} fails * @return the new {@code Single} instance * @throws NullPointerException if {@code item} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorReturnItem(@NonNull T item) { Objects.requireNonNull(item, "item is null"); return RxJavaPlugins.onAssembly(new SingleOnErrorReturn<>(this, null, item)); } /** * Resumes the flow with the given {@link SingleSource} when the current {@code Single} fails instead of * signaling the error via {@code onError}. *

* *

* By default, when a {@code Single} encounters an error that prevents it from emitting the expected item to * its {@link SingleObserver}, the {@code Single} invokes its {@code SingleObserver}'s {@code onError} method, and then quits * without invoking any more of its {@code SingleObserver}'s methods. The {@code onErrorResumeWith} method changes this * behavior. If you pass another {@code Single} ({@code resumeSingleInCaseOfError}) to a {@code Single}'s * {@code onErrorResumeWith} method, if the original {@code Single} encounters an error, instead of invoking its * {@code SingleObserver}'s {@code onError} method, it will instead relinquish control to {@code resumeSingleInCaseOfError} which * will invoke the {@code SingleObserver}'s {@link SingleObserver#onSuccess onSuccess} method if it is able to do so. In such a case, * because no {@code Single} necessarily invokes {@code onError}, the {@code SingleObserver} may never know that an error * happened. *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorResumeWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param fallback a {@code Single} that will take control if source {@code Single} encounters an error. * @return the new {@code Single} instance * @throws NullPointerException if {@code fallback} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorResumeWith(@NonNull SingleSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return onErrorResumeNext(Functions.justFunction(fallback)); } /** * Returns a {@link Maybe} instance that if the current {@code Single} emits an error, it will emit an {@code onComplete} * and swallow the throwable. *

* *

*
Scheduler:
*
{@code onErrorComplete} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Maybe} instance * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe onErrorComplete() { return onErrorComplete(Functions.alwaysTrue()); } /** * Returns a {@link Maybe} instance that if this {@code Single} emits an error and the predicate returns * {@code true}, it will emit an {@code onComplete} and swallow the throwable. *

* *

*
Scheduler:
*
{@code onErrorComplete} does not operate by default on a particular {@link Scheduler}.
*
* @param predicate the predicate to call when an {@link Throwable} is emitted which should return {@code true} * if the {@code Throwable} should be swallowed and replaced with an {@code onComplete}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorComplete(@NonNull Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return RxJavaPlugins.onAssembly(new SingleOnErrorComplete<>(this, predicate)); } /** * Resumes the flow with a {@link SingleSource} returned for the failure {@link Throwable} of the current {@code Single} by a * function instead of signaling the error via {@code onError}. *

* *

* By default, when a {@code Single} encounters an error that prevents it from emitting the expected item to * its {@link SingleObserver}, the {@code Single} invokes its {@code SingleObserver}'s {@code onError} method, and then quits * without invoking any more of its {@code SingleObserver}'s methods. The {@code onErrorResumeNext} method changes this * behavior. If you pass a function that will return another {@code Single} ({@code resumeFunctionInCaseOfError}) to a {@code Single}'s * {@code onErrorResumeNext} method, if the original {@code Single} encounters an error, instead of invoking its * {@code SingleObserver}'s {@code onError} method, it will instead relinquish control to {@code resumeSingleInCaseOfError} which * will invoke the {@code SingleObserver}'s {@link SingleObserver#onSuccess onSuccess} method if it is able to do so. In such a case, * because no {@code Single} necessarily invokes {@code onError}, the {@code SingleObserver} may never know that an error * happened. *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorResumeNext} does not operate by default on a particular {@link Scheduler}.
*
* * @param fallbackSupplier a function that returns a {@code SingleSource} that will take control if source {@code Single} encounters an error. * @return the new {@code Single} instance * @throws NullPointerException if {@code fallbackSupplier} is {@code null} * @see ReactiveX operators documentation: Catch * @since .20 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorResumeNext( @NonNull Function> fallbackSupplier) { Objects.requireNonNull(fallbackSupplier, "fallbackSupplier is null"); return RxJavaPlugins.onAssembly(new SingleResumeNext<>(this, fallbackSupplier)); } /** * Nulls out references to the upstream producer and downstream {@link SingleObserver} if * the sequence is terminated or downstream calls {@code dispose()}. *

* *

*
Scheduler:
*
{@code onTerminateDetach} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.5 - experimental * @return the new {@code Single} which {@code null}s out references to the upstream producer and downstream {@code SingleObserver} if * the sequence is terminated or downstream calls {@code dispose()} * @since 2.2 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single onTerminateDetach() { return RxJavaPlugins.onAssembly(new SingleDetach<>(this)); } /** * Repeatedly re-subscribes to the current {@code Single} and emits each success value as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code repeat} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Flowable} instance * @since 2.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeat() { return toFlowable().repeat(); } /** * Re-subscribes to the current {@code Single} at most the given number of times and emits each success value as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code repeat} does not operate by default on a particular {@link Scheduler}.
*
* @param times the number of times to re-subscribe to the current {@code Single} * @return the new {@code Flowable} instance * @throws IllegalArgumentException if {@code times} is negative * @since 2.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeat(long times) { return toFlowable().repeat(times); } /** * Re-subscribes to the current {@code Single} if * the {@link Publisher} returned by the handler function signals a value in response to a * value signaled through the {@link Flowable} the handler receives. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer. * The {@code Publisher} returned by the handler function is expected to honor backpressure as well.
*
Scheduler:
*
{@code repeatWhen} does not operate by default on a particular {@link Scheduler}.
*
* @param handler the function that is called with a {@code Flowable} that signals a value when the {@code Single} * signaled a success value and returns a {@code Publisher} that has to signal a value to * trigger a resubscription to the current {@code Single}, otherwise the terminal signal of * the {@code Publisher} will be the terminal signal of the sequence as well. * @return the new {@code Flowable} instance * @throws NullPointerException if {@code handler} is {@code null} * @since 2.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeatWhen(@NonNull Function, @NonNull ? extends Publisher<@NonNull ?>> handler) { return toFlowable().repeatWhen(handler); } /** * Re-subscribes to the current {@code Single} until the given {@link BooleanSupplier} returns {@code true} * and emits the success items as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code repeatUntil} does not operate by default on a particular {@link Scheduler}.
*
* @param stop the {@code BooleanSupplier} called after the current {@code Single} succeeds and if returns {@code false}, * the {@code Single} is re-subscribed; otherwise the sequence completes. * @return the new {@code Flowable} instance * @throws NullPointerException if {@code stop} is {@code null} * @since 2.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeatUntil(@NonNull BooleanSupplier stop) { return toFlowable().repeatUntil(stop); } /** * Repeatedly re-subscribes to the current {@code Single} indefinitely if it fails with an {@code onError}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Single} instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retry() { return toSingle(toFlowable().retry()); } /** * Repeatedly re-subscribe at most the specified times to the current {@code Single} * if it fails with an {@code onError}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @param times the number of times to resubscribe if the current {@code Single} fails * @return the new {@code Single} instance * @throws IllegalArgumentException if {@code times} is negative * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retry(long times) { return toSingle(toFlowable().retry(times)); } /** * Re-subscribe to the current {@code Single} if the given predicate returns {@code true} when the {@code Single} fails * with an {@code onError}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @param predicate the predicate called with the resubscription count and the failure {@link Throwable} * and should return {@code true} if a resubscription should happen * @return the new {@code Single} instance * @throws NullPointerException if {@code predicate} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retry(@NonNull BiPredicate predicate) { return toSingle(toFlowable().retry(predicate)); } /** * Repeatedly re-subscribe at most times or until the predicate returns {@code false}, whichever happens first * if it fails with an {@code onError}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.8 - experimental * @param times the number of times to resubscribe if the current {@code Single} fails * @param predicate the predicate called with the failure {@link Throwable} * and should return {@code true} if a resubscription should happen * @return the new {@code Single} instance * @throws NullPointerException if {@code predicate} is {@code null} * @throws IllegalArgumentException if {@code times} is negative * @since 2.2 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retry(long times, @NonNull Predicate predicate) { return toSingle(toFlowable().retry(times, predicate)); } /** * Re-subscribe to the current {@code Single} if the given predicate returns {@code true} when the {@code Single} fails * with an {@code onError}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @param predicate the predicate called with the failure {@link Throwable} * and should return {@code true} if a resubscription should happen * @return the new {@code Single} instance * @throws NullPointerException if {@code predicate} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retry(@NonNull Predicate predicate) { return toSingle(toFlowable().retry(predicate)); } /** * Retries until the given stop function returns {@code true}. *

* *

*
Scheduler:
*
{@code retryUntil} does not operate by default on a particular {@link Scheduler}.
*
* @param stop the function that should return {@code true} to stop retrying * @return the new {@code Single} instance * @throws NullPointerException if {@code stop} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single retryUntil(@NonNull BooleanSupplier stop) { Objects.requireNonNull(stop, "stop is null"); return retry(Long.MAX_VALUE, Functions.predicateReverseFor(stop)); } /** * Re-subscribes to the current {@code Single} if and when the {@link Publisher} returned by the handler * function signals a value. *

* *

* If the {@code Publisher} signals an {@code onComplete}, the resulting {@code Single} will signal a {@link NoSuchElementException}. *

* Note that the inner {@code Publisher} returned by the handler function should signal * either {@code onNext}, {@code onError} or {@code onComplete} in response to the received * {@link Throwable} to indicate the operator should retry or terminate. If the upstream to * the operator is asynchronous, signaling {@code onNext} followed by {@code onComplete} immediately may * result in the sequence to be completed immediately. Similarly, if this inner * {@code Publisher} signals {@code onError} or {@code onComplete} while the upstream is * active, the sequence is terminated with the same signal immediately. *

* The following example demonstrates how to retry an asynchronous source with a delay: *


     * Single.timer(1, TimeUnit.SECONDS)
     *     .doOnSubscribe(s -> System.out.println("subscribing"))
     *     .map(v -> { throw new RuntimeException(); })
     *     .retryWhen(errors -> {
     *         AtomicInteger counter = new AtomicInteger();
     *         return errors
     *                   .takeWhile(e -> counter.getAndIncrement() != 3)
     *                   .flatMap(e -> {
     *                       System.out.println("delay retry by " + counter.get() + " second(s)");
     *                       return Flowable.timer(counter.get(), TimeUnit.SECONDS);
     *                   });
     *     })
     *     .blockingGet();
     * 
*
*
Scheduler:
*
{@code retryWhen} does not operate by default on a particular {@link Scheduler}.
*
* * @param handler the function that receives a {@link Flowable} of the error the {@code Single} emits and should * return a {@code Publisher} that should signal a normal value (in response to the * throwable the {@code Flowable} emits) to trigger a resubscription or signal an error to * be the output of the resulting {@code Single} * @return the new {@code Single} instance * @throws NullPointerException if {@code handler} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single retryWhen(@NonNull Function, @NonNull ? extends Publisher<@NonNull ?>> handler) { return toSingle(toFlowable().retryWhen(handler)); } /** * Wraps the given {@link SingleObserver}, catches any {@link RuntimeException}s thrown by its * {@link SingleObserver#onSubscribe(Disposable)}, {@link SingleObserver#onSuccess(Object)} or * {@link SingleObserver#onError(Throwable)} methods* and routes those to the global error handler * via {@link RxJavaPlugins#onError(Throwable)}. *

* By default, the {@code Single} protocol forbids the {@code onXXX} methods to throw, but some * {@code SingleObserver} implementation may do it anyway, causing undefined behavior in the * upstream. This method and the underlying safe wrapper ensures such misbehaving consumers don't * disrupt the protocol. *

*
Scheduler:
*
{@code safeSubscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param observer the potentially misbehaving {@code SingleObserver} * @throws NullPointerException if {@code observer} is {@code null} * @see #subscribe(Consumer,Consumer) * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void safeSubscribe(@NonNull SingleObserver observer) { Objects.requireNonNull(observer, "observer is null"); subscribe(new SafeSingleObserver<>(observer)); } /** * Returns a {@link Flowable} which first runs the other {@link CompletableSource} * then the current {@code Single} if the other completed normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code CompletableSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull CompletableSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Completable.wrap(other).toFlowable(), toFlowable()); } /** * Returns a {@link Flowable} which first runs the other {@link SingleSource} * then the current {@code Single} if the other succeeded normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code SingleSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull SingleSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Single.wrap(other).toFlowable(), toFlowable()); } /** * Returns a {@link Flowable} which first runs the other {@link MaybeSource} * then the current {@code Single} if the other succeeded or completed normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code MaybeSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Maybe.wrap(other).toFlowable(), toFlowable()); } /** * Returns an {@link Observable} which first delivers the events * of the other {@link ObservableSource} then runs the current {@code Single}. *

* *

*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code ObservableSource} to run first * @return the new {@code Observable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWith(@NonNull ObservableSource other) { Objects.requireNonNull(other, "other is null"); return Observable.wrap(other).concatWith(this.toObservable()); } /** * Returns a {@link Flowable} which first delivers the events * of the other {@link Publisher} then runs the current {@code Single}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer * and expects the other {@code Publisher} to honor it as well.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code Publisher} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public final Flowable startWith(@NonNull Publisher other) { Objects.requireNonNull(other, "other is null"); return toFlowable().startWith(other); } /** * Subscribes to a {@code Single} but ignore its emission or notification. *

* *

* If the {@code Single} emits an error, it is wrapped into an * {@link io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the {@link RxJavaPlugins#onError(Throwable)} handler. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, DisposableContainer) */ @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), Functions.ON_ERROR_MISSING); } /** * Subscribes to a {@code Single} and provides a composite callback to handle the item it emits * or any error notification it issues. *

* *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onCallback * the callback that receives either the success value or the failure {@link Throwable} * (whichever is not {@code null}) * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onCallback} is {@code null} * @see #subscribe(Consumer, Consumer, DisposableContainer) * @see ReactiveX operators documentation: Subscribe */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(@NonNull BiConsumer<@Nullable ? super T, @Nullable ? super Throwable> onCallback) { Objects.requireNonNull(onCallback, "onCallback is null"); BiConsumerSingleObserver observer = new BiConsumerSingleObserver<>(onCallback); subscribe(observer); return observer; } /** * Subscribes to a {@code Single} and provides a callback to handle the item it emits. *

* *

* If the {@code Single} emits an error, it is wrapped into an * {@link io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the {@link RxJavaPlugins#onError(Throwable)} handler. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSuccess * the {@code Consumer} you have designed to accept the emission from the {@code Single} * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onSuccess} is {@code null} * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, DisposableContainer) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe(@NonNull Consumer onSuccess) { return subscribe(onSuccess, Functions.ON_ERROR_MISSING); } /** * Subscribes to a {@code Single} and provides callbacks to handle the item it emits or any error notification it * issues. *

* *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSuccess * the {@code Consumer} you have designed to accept the emission from the {@code Single} * @param onError * the {@code Consumer} you have designed to accept any error notification from the * {@code Single} * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onSuccess} or {@code onError} is {@code null} * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, DisposableContainer) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); ConsumerSingleObserver observer = new ConsumerSingleObserver<>(onSuccess, onError); subscribe(observer); return observer; } /** * Wraps the given onXXX callbacks into a {@link Disposable} {@link SingleObserver}, * adds it to the given {@link DisposableContainer} and ensures, that if the upstream * terminates or this particular {@code Disposable} is disposed, the {@code SingleObserver} is removed * from the given container. *

* The {@code SingleObserver} will be removed after the callback for the terminal event has been invoked. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param onSuccess the callback for upstream items * @param onError the callback for an upstream error if any * @param container the {@code DisposableContainer} (such as {@link CompositeDisposable}) to add and remove the * created {@code Disposable} {@code SingleObserver} * @return the {@code Disposable} that allows disposing the particular subscription. * @throws NullPointerException * if {@code onSuccess}, {@code onError} * or {@code container} is {@code null} * @since 3.1.0 */ @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe( @NonNull Consumer onSuccess, @NonNull Consumer onError, @NonNull DisposableContainer container) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(container, "container is null"); DisposableAutoReleaseMultiObserver observer = new DisposableAutoReleaseMultiObserver<>( container, onSuccess, onError, Functions.EMPTY_ACTION); container.add(observer); subscribe(observer); return observer; } @SchedulerSupport(SchedulerSupport.NONE) @Override public final void subscribe(@NonNull SingleObserver observer) { Objects.requireNonNull(observer, "observer is null"); observer = RxJavaPlugins.onSubscribe(this, observer); Objects.requireNonNull(observer, "The RxJavaPlugins.onSubscribe hook returned a null SingleObserver. Please check the handler provided to RxJavaPlugins.setOnSingleSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins"); try { subscribeActual(observer); } catch (NullPointerException ex) { throw ex; } catch (Throwable ex) { Exceptions.throwIfFatal(ex); NullPointerException npe = new NullPointerException("subscribeActual failed"); npe.initCause(ex); throw npe; } } /** * Implement this method in subclasses to handle the incoming {@link SingleObserver}s. *

There is no need to call any of the plugin hooks on the current {@code Single} instance or * the {@code SingleObserver}; all hooks and basic safeguards have been * applied by {@link #subscribe(SingleObserver)} before this method gets called. * @param observer the {@code SingleObserver} to handle, not {@code null} */ protected abstract void subscribeActual(@NonNull SingleObserver observer); /** * Subscribes a given {@link SingleObserver} (subclass) to this {@code Single} and returns the given * {@code SingleObserver} as is. *

* *

Usage example: *


     * Single<Integer> source = Single.just(1);
     * CompositeDisposable composite = new CompositeDisposable();
     *
     * DisposableSingleObserver<Integer> ds = new DisposableSingleObserver<>() {
     *     // ...
     * };
     *
     * composite.add(source.subscribeWith(ds));
     * 
*
*
Scheduler:
*
{@code subscribeWith} does not operate by default on a particular {@link Scheduler}.
*
* @param the type of the {@code SingleObserver} to use and return * @param observer the {@code SingleObserver} (subclass) to use and return, not {@code null} * @return the input {@code observer} * @throws NullPointerException if {@code observer} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull E extends SingleObserver> E subscribeWith(E observer) { subscribe(observer); return observer; } /** * Asynchronously subscribes {@link SingleObserver}s to this {@code Single} on the specified {@link Scheduler}. *

* *

*
Scheduler:
*
You specify which {@code Scheduler} this operator will use.
*
* * @param scheduler * the {@code Scheduler} to perform subscription actions on * @return the new {@code Single} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @see ReactiveX operators documentation: SubscribeOn * @see RxJava Threading Examples * @see #observeOn */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single subscribeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleSubscribeOn<>(this, scheduler)); } /** * Measures the time (in milliseconds) between the subscription and success item emission * of the current {@code Single} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Single} fails, the resulting {@code Single} will * pass along the signal to the downstream. To measure the time to error, * use {@link #materialize()} and apply {@link #timeInterval()}. *

*
Scheduler:
*
{@code timeInterval} uses the {@code computation} {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Single}.
*
* @return the new {@code Single} instance * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single> timeInterval() { return timeInterval(TimeUnit.MILLISECONDS, Schedulers.computation()); } /** * Measures the time (in milliseconds) between the subscription and success item emission * of the current {@code Single} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Single} fails, the resulting {@code Single} will * pass along the signal to the downstream. To measure the time to error, * use {@link #materialize()} and apply {@link #timeInterval(Scheduler)}. *

*
Scheduler:
*
{@code timeInterval} uses the provided {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Single}.
*
* @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Single} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single> timeInterval(@NonNull Scheduler scheduler) { return timeInterval(TimeUnit.MILLISECONDS, scheduler); } /** * Measures the time between the subscription and success item emission * of the current {@code Single} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Single} fails, the resulting {@code Single} will * pass along the signals to the downstream. To measure the time to error, * use {@link #materialize()} and apply {@link #timeInterval(TimeUnit, Scheduler)}. *

*
Scheduler:
*
{@code timeInterval} uses the {@code computation} {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Single}.
*
* @param unit the time unit for measurement * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single> timeInterval(@NonNull TimeUnit unit) { return timeInterval(unit, Schedulers.computation()); } /** * Measures the time between the subscription and success item emission * of the current {@code Single} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Single} is empty or fails, the resulting {@code Single} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link #timeInterval(TimeUnit, Scheduler)}. *

*
Scheduler:
*
{@code timeInterval} uses the provided {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Single}.
*
* @param unit the time unit for measurement * @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single> timeInterval(@NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleTimeInterval<>(this, unit, scheduler, true)); } /** * Combines the success value from the current {@code Single} with the current time (in milliseconds) of * its reception, using the {@code computation} {@link Scheduler} as time source, * then signals them as a {@link Timed} instance. *

* *

* If the current {@code Single} is empty or fails, the resulting {@code Single} will * pass along the signals to the downstream. To get the timestamp of the error, * use {@link #materialize()} and apply {@link #timestamp()}. *

*
Scheduler:
*
{@code timestamp} uses the {@code computation} {@code Scheduler} * for determining the current time upon receiving the * success item from the current {@code Single}.
*
* @return the new {@code Single} instance * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single> timestamp() { return timestamp(TimeUnit.MILLISECONDS, Schedulers.computation()); } /** * Combines the success value from the current {@code Single} with the current time (in milliseconds) of * its reception, using the given {@link Scheduler} as time source, * then signals them as a {@link Timed} instance. *

* *

* If the current {@code Single} is empty or fails, the resulting {@code Single} will * pass along the signals to the downstream. To get the timestamp of the error, * use {@link #materialize()} and apply {@link #timestamp(Scheduler)}. *

*
Scheduler:
*
{@code timestamp} uses the provided {@code Scheduler} * for determining the current time upon receiving the * success item from the current {@code Single}.
*
* @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Single} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single> timestamp(@NonNull Scheduler scheduler) { return timestamp(TimeUnit.MILLISECONDS, scheduler); } /** * Combines the success value from the current {@code Single} with the current time of * its reception, using the {@code computation} {@link Scheduler} as time source, * then signals it as a {@link Timed} instance. *

* *

* If the current {@code Single} is empty or fails, the resulting {@code Single} will * pass along the signals to the downstream. To get the timestamp of the error, * use {@link #materialize()} and apply {@link #timestamp(TimeUnit)}. *

*
Scheduler:
*
{@code timestamp} uses the {@code computation} {@code Scheduler}, * for determining the current time upon receiving the * success item from the current {@code Single}.
*
* @param unit the time unit for measurement * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single> timestamp(@NonNull TimeUnit unit) { return timestamp(unit, Schedulers.computation()); } /** * Combines the success value from the current {@code Single} with the current time of * its reception, using the given {@link Scheduler} as time source, * then signals it as a {@link Timed} instance. *

* *

* If the current {@code Single} is empty or fails, the resulting {@code Single} will * pass along the signals to the downstream. To get the timestamp of the error, * use {@link #materialize()} and apply {@link #timestamp(TimeUnit, Scheduler)}. *

*
Scheduler:
*
{@code timestamp} uses the provided {@code Scheduler}, * which is used for determining the current time upon receiving the * success item from the current {@code Single}.
*
* @param unit the time unit for measurement * @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single> timestamp(@NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleTimeInterval<>(this, unit, scheduler, false)); } /** * Returns a {@code Single} that emits the item emitted by the current {@code Single} until a {@link CompletableSource} terminates. Upon * termination of {@code other}, this will emit a {@link CancellationException} rather than go to * {@link SingleObserver#onSuccess(Object)}. *

* *

*
Scheduler:
*
{@code takeUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the {@code CompletableSource} whose termination will cause {@code takeUntil} to emit the item from the current * {@code Single} * @return the new {@code Single} that emits the item emitted by the current {@code Single} until such time as {@code other} terminates. * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single takeUntil(@NonNull CompletableSource other) { Objects.requireNonNull(other, "other is null"); return takeUntil(new CompletableToFlowable(other)); } /** * Returns a {@code Single} that emits the item emitted by the current {@code Single} until a {@link Publisher} emits an item or completes. Upon * emission of an item from {@code other}, this will emit a {@link CancellationException} rather than go to * {@link SingleObserver#onSuccess(Object)}. *

* *

*
Backpressure:
*
The {@code other} publisher is consumed in an unbounded fashion but will be * cancelled after the first item it produced.
*
Scheduler:
*
{@code takeUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the {@code Publisher} whose first emitted item or completion will cause {@code takeUntil} to emit {@code CancellationException} * if the current {@code Single} hasn't completed till then * @param * the type of items emitted by {@code other} * @return the new {@code Single} that emits the item emitted by the current {@code Single} until such time as {@code other} emits * its first item * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: TakeUntil */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull E> Single takeUntil(@NonNull Publisher other) { Objects.requireNonNull(other, "other is null"); return RxJavaPlugins.onAssembly(new SingleTakeUntil<>(this, other)); } /** * Returns a {@code Single} that emits the item emitted by the current {@code Single} until a second {@code Single} emits an item. Upon * emission of an item from {@code other}, this will emit a {@link CancellationException} rather than go to * {@link SingleObserver#onSuccess(Object)}. *

* *

*
Scheduler:
*
{@code takeUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the {@code Single} whose emitted item will cause {@code takeUntil} to emit {@code CancellationException} * if the current {@code Single} hasn't completed till then * @param * the type of item emitted by {@code other} * @return the new {@code Single} that emits the item emitted by the current {@code Single} until such time as {@code other} emits its item * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull E> Single takeUntil(@NonNull SingleSource other) { Objects.requireNonNull(other, "other is null"); return takeUntil(new SingleToFlowable(other)); } /** * Signals a {@link TimeoutException} if the current {@code Single} doesn't signal a success value within the * specified timeout window. *

* *

*
Scheduler:
*
{@code timeout} signals the {@code TimeoutException} on the {@code computation} {@link Scheduler}.
*
* @param timeout the timeout amount * @param unit the time unit * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Single timeout(long timeout, @NonNull TimeUnit unit) { return timeout0(timeout, unit, Schedulers.computation(), null); } /** * Signals a {@link TimeoutException} if the current {@code Single} doesn't signal a success value within the * specified timeout window. *

* *

*
Scheduler:
*
{@code timeout} signals the {@code TimeoutException} on the {@link Scheduler} you specify.
*
* @param timeout the timeout amount * @param unit the time unit * @param scheduler the target {@code Scheduler} where the timeout is awaited and the {@code TimeoutException} * signaled * @return the new {@code Single} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Single timeout(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return timeout0(timeout, unit, scheduler, null); } /** * Runs the current {@code Single} and if it doesn't signal within the specified timeout window, it is * disposed and the other {@link SingleSource} subscribed to. *

* *

*
Scheduler:
*
{@code timeout} subscribes to the other {@code SingleSource} on the {@link Scheduler} you specify.
*
* @param timeout the timeout amount * @param unit the time unit * @param scheduler the {@code Scheduler} where the timeout is awaited and the subscription to other happens * @param fallback the other {@code SingleSource} that gets subscribed to if the current {@code Single} times out * @return the new {@code Single} instance * @throws NullPointerException if {@code unit}, {@code scheduler} or {@code fallback} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single timeout(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler, @NonNull SingleSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return timeout0(timeout, unit, scheduler, fallback); } /** * Runs the current {@code Single} and if it doesn't signal within the specified timeout window, it is * disposed and the other {@link SingleSource} subscribed to. *

* *

*
Scheduler:
*
{@code timeout} subscribes to the other {@code SingleSource} on * the {@code computation} {@link Scheduler}.
*
* @param timeout the timeout amount * @param unit the time unit * @param fallback the other {@code SingleSource} that gets subscribed to if the current {@code Single} times out * @return the new {@code Single} instance * @throws NullPointerException * if {@code fallback} or {@code unit} is {@code null} * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single timeout(long timeout, @NonNull TimeUnit unit, @NonNull SingleSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return timeout0(timeout, unit, Schedulers.computation(), fallback); } private Single timeout0(final long timeout, final TimeUnit unit, final Scheduler scheduler, final SingleSource fallback) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleTimeout<>(this, timeout, unit, scheduler, fallback)); } /** * Calls the specified converter function during assembly time and returns its resulting value. *

* *

* This allows fluent conversion to any other type. *

*
Scheduler:
*
{@code to} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.7 - experimental * @param the resulting object type * @param converter the function that receives the current {@code Single} instance and returns a value * @return the converted value * @throws NullPointerException if {@code converter} is {@code null} * @since 2.2 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final R to(@NonNull SingleConverter converter) { return Objects.requireNonNull(converter, "converter is null").apply(this); } /** * Returns a {@link Completable} that ignores the success value of this {@code Single} * and signals {@code onComplete} instead. *

* *

*
Scheduler:
*
{@code ignoreElement} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Completable} instance * @since 2.1.13 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Completable ignoreElement() { return RxJavaPlugins.onAssembly(new CompletableFromSingle<>(this)); } /** * Converts this {@code Single} into a {@link Flowable}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code toFlowable} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Flowable} instance */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public final Flowable toFlowable() { if (this instanceof FuseToFlowable) { return ((FuseToFlowable)this).fuseToFlowable(); } return RxJavaPlugins.onAssembly(new SingleToFlowable<>(this)); } /** * Returns a {@link Future} representing the single value emitted by this {@code Single}. *

* *

* Cancelling the {@code Future} will cancel the subscription to the current {@code Single}. *

*
Scheduler:
*
{@code toFuture} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Future} instance * @see ReactiveX documentation: To */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Future toFuture() { return subscribeWith(new FutureMultiObserver<>()); } /** * Converts this {@code Single} into a {@link Maybe}. *

* *

*
Scheduler:
*
{@code toMaybe} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Maybe} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public final Maybe toMaybe() { if (this instanceof FuseToMaybe) { return ((FuseToMaybe)this).fuseToMaybe(); } return RxJavaPlugins.onAssembly(new MaybeFromSingle<>(this)); } /** * Converts this {@code Single} into an {@link Observable}. *

* *

*
Scheduler:
*
{@code toObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Observable} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public final Observable toObservable() { if (this instanceof FuseToObservable) { return ((FuseToObservable)this).fuseToObservable(); } return RxJavaPlugins.onAssembly(new SingleToObservable<>(this)); } /** * Returns a {@code Single} which makes sure when a {@link SingleObserver} disposes the {@link Disposable}, * that call is propagated up on the specified {@link Scheduler}. *

* *

*
Scheduler:
*
{@code unsubscribeOn} calls {@code dispose()} of the upstream on the {@code Scheduler} you specify.
*
*

History: 2.0.9 - experimental * @param scheduler the target scheduler where to execute the disposal * @return the new {@code Single} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @since 2.2 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single unsubscribeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new SingleUnsubscribeOn<>(this, scheduler)); } /** * Returns a {@code Single} that emits the result of applying a specified function to the pair of items emitted by * the current {@code Single} and another specified {@link SingleSource}. *

* *

*
Scheduler:
*
{@code zipWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items emitted by the {@code other} {@code Single} * @param * the type of items emitted by the resulting {@code Single} * @param other * the other {@code SingleSource} * @param zipper * a function that combines the pairs of items from the two {@code SingleSource}s to generate the items to * be emitted by the resulting {@code Single} * @return the new {@code Single} that pairs up values from the current {@code Single} and the {@code other} {@code SingleSource} * and emits the results of {@code zipFunction} applied to these pairs * @throws NullPointerException if {@code other} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull U, @NonNull R> Single zipWith(@NonNull SingleSource other, @NonNull BiFunction zipper) { return zip(this, other, zipper); } // ------------------------------------------------------------------------- // Fluent test support, super handy and reduces test preparation boilerplate // ------------------------------------------------------------------------- /** * Creates a {@link TestObserver} and subscribes it to this {@code Single}. *

* *

*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code TestObserver} instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final TestObserver test() { TestObserver to = new TestObserver<>(); subscribe(to); return to; } /** * Creates a {@link TestObserver} optionally in cancelled state, then subscribes it to this {@code Single}. *

* *

*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @param dispose if {@code true}, the {@code TestObserver} will be cancelled before subscribing to this * {@code Single}. * @return the new {@code TestObserver} instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final TestObserver test(boolean dispose) { TestObserver to = new TestObserver<>(); if (dispose) { to.dispose(); } subscribe(to); return to; } @NonNull private static Single toSingle(@NonNull Flowable source) { return RxJavaPlugins.onAssembly(new FlowableSingleSingle<>(source, null)); } // ------------------------------------------------------------------------- // JDK 8 Support // ------------------------------------------------------------------------- /** * Signals the completion value or error of the given (hot) {@link CompletionStage}-based asynchronous calculation. *

* *

* Note that the operator takes an already instantiated, running or terminated {@code CompletionStage}. * If the optional is to be created per consumer upon subscription, use {@link #defer(Supplier)} * around {@code fromCompletionStage}: *


     * Single.defer(() -> Single.fromCompletionStage(createCompletionStage()));
     * 
*

* If the {@code CompletionStage} completes with {@code null}, the resulting {@code Single} is terminated with * a {@link NullPointerException}. *

* Canceling the flow can't cancel the execution of the {@code CompletionStage} because {@code CompletionStage} * itself doesn't support cancellation. Instead, the operator detaches from the {@code CompletionStage}. *

*
Scheduler:
*
{@code fromCompletionStage} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code CompletionStage} * @param stage the {@code CompletionStage} to convert to {@code Single} and signal its success value or error * @return the new {@code Single} instance * @throws NullPointerException if {@code stage} is {@code null} * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Single<@NonNull T> fromCompletionStage(@NonNull CompletionStage stage) { Objects.requireNonNull(stage, "stage is null"); return RxJavaPlugins.onAssembly(new SingleFromCompletionStage<>(stage)); } /** * Maps the upstream success value into an {@link Optional} and emits the contained item if not empty as a {@link Maybe}. *

* * *

*
Scheduler:
*
{@code mapOptional} does not operate by default on a particular {@link Scheduler}.
*
* @param the non-{@code null} output type * @param mapper the function that receives the upstream success item and should return a non-empty {@code Optional} * to emit as the success output or an empty {@code Optional} to complete the {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #map(Function) * @see #filter(Predicate) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Maybe mapOptional(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleMapOptional<>(this, mapper)); } /** * Signals the upstream success item (or error) via a {@link CompletionStage}. *

* *

* The upstream can be canceled by converting the resulting {@code CompletionStage} into * {@link CompletableFuture} via {@link CompletionStage#toCompletableFuture()} and * calling {@link CompletableFuture#cancel(boolean)} on it. * The upstream will be also cancelled if the resulting {@code CompletionStage} is converted to and * completed manually by {@link CompletableFuture#complete(Object)} or {@link CompletableFuture#completeExceptionally(Throwable)}. *

*
Scheduler:
*
{@code toCompletionStage} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code CompletionStage} instance * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final CompletionStage toCompletionStage() { return subscribeWith(new CompletionStageConsumer<>(false, null)); } /** * Maps the upstream succecss value into a Java {@link Stream} and emits its * items to the downstream consumer as a {@link Flowable}. * *

* The operator closes the {@code Stream} upon cancellation and when it terminates. The exceptions raised when * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsFlowable(Function)}: *


     * source.flattenAsFlowable(item -> createStream(item)::iterator);
     * 
*

* Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): *


     * source.flattenStreamAsFlowable(item -> IntStream.rangeClosed(1, 10).boxed());
     * 
*

* {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times * from multiple threads can lead to undefined behavior. *

*
Backpressure:
*
The operator honors backpressure from downstream and iterates the given {@code Stream} * on demand (i.e., when requested).
*
Scheduler:
*
{@code flattenStreamAsFlowable} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code Stream} and the output {@code Flowable} * @param mapper the function that receives the upstream success item and should * return a {@code Stream} of values to emit. * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #flattenAsFlowable(Function) * @see #flattenStreamAsObservable(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) @NonNull public final <@NonNull R> Flowable flattenStreamAsFlowable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlattenStreamAsFlowable<>(this, mapper)); } /** * Maps the upstream succecss value into a Java {@link Stream} and emits its * items to the downstream consumer as an {@link Observable}. *

* *

* The operator closes the {@code Stream} upon cancellation and when it terminates. The exceptions raised when * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsObservable(Function)}: *


     * source.flattenAsObservable(item -> createStream(item)::iterator);
     * 
*

* Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): *


     * source.flattenStreamAsObservable(item -> IntStream.rangeClosed(1, 10).boxed());
     * 
*

* {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times * from multiple threads can lead to undefined behavior. *

*
Scheduler:
*
{@code flattenStreamAsObservable} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code Stream} and the output {@code Observable} * @param mapper the function that receives the upstream success item and should * return a {@code Stream} of values to emit. * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #flattenAsObservable(Function) * @see #flattenStreamAsFlowable(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Observable flattenStreamAsObservable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new SingleFlattenStreamAsObservable<>(this, mapper)); } }