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

io.reactivex.observable.Single Maven / Gradle / Ivy

The 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.observable;

import java.util.NoSuchElementException;
import java.util.concurrent.*;

import io.reactivex.common.*;
import io.reactivex.common.annotations.*;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.*;
import io.reactivex.common.internal.functions.*;
import io.reactivex.common.internal.utils.ExceptionHelper;
import io.reactivex.observable.extensions.*;
import io.reactivex.observable.internal.observers.*;
import io.reactivex.observable.internal.operators.*;
import io.reactivex.observable.observers.TestObserver;

/**
 * The Single class implements the Reactive Pattern for a single value response.
 * See {@link Observable} or {@link Observable} for the
 * implementation of the Reactive Pattern for a stream or vector of values.
 * 

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

* Like an {@link Observable}, a {@code Single} is lazy, can be either "hot" or "cold", synchronous or * asynchronous. *

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

* *

* For more information see the ReactiveX * documentation. * * @param * the type of the item emitted by the Single * @since 2.0 */ public abstract class Single implements SingleSource { /** * Runs multiple Single sources and signals the events of the first one that signals (cancelling * the rest). *

*
Scheduler:
*
{@code amb} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the Iterable sequence of sources. A subscription to each source will * occur in the same order as in this Iterable. * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single amb(final Iterable> sources) { ObjectHelper.requireNonNull(sources, "sources is null"); return RxJavaObservablePlugins.onAssembly(new SingleAmb(null, sources)); } /** * Runs multiple Single sources and signals the events of the first one that signals (cancelling * 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 Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Single ambArray(final SingleSource... sources) { if (sources.length == 0) { return error(SingleInternalHelper.emptyThrower()); } if (sources.length == 1) { return wrap((SingleSource)sources[0]); } return RxJavaObservablePlugins.onAssembly(new SingleAmb(sources, null)); } /** * Concatenate the single values, in a non-overlapping fashion, of the Single sources provided by * an Iterable sequence. *
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the Iterable sequence of SingleSource instances * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Observable concat(Iterable> sources) { return concat(Observable.fromIterable(sources)); } /** * Concatenate the single values, in a non-overlapping fashion, of the Single sources provided by * an Observable sequence. *
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the ObservableSource of SingleSource instances * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable concat(ObservableSource> sources) { ObjectHelper.requireNonNull(sources, "sources is null"); return RxJavaObservablePlugins.onAssembly(new ObservableConcatMap(sources, SingleInternalHelper.toObservable(), 2, ErrorMode.IMMEDIATE)); } /** * Concatenate the single values, in a non-overlapping fashion, of the Single sources provided by * an ObservableSource sequence and prefetched by the specified amount. *
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the ObservableSource of SingleSource instances * @param prefetch the number of SingleSources to prefetch from the ObservableSource * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable concat(ObservableSource> sources, int prefetch) { ObjectHelper.requireNonNull(sources, "sources is null"); ObjectHelper.verifyPositive(prefetch, "prefetch"); return RxJavaObservablePlugins.onAssembly(new ObservableConcatMap(sources, SingleInternalHelper.toObservable(), prefetch, ErrorMode.IMMEDIATE)); } /** * Returns an Observable that emits the items emitted by two Singles, one after the other. *

* *

*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be concatenated * @param source2 * a Single to be concatenated * @return an Observable that emits items emitted by the two source Singles, one after the other. * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable concat( SingleSource source1, SingleSource source2 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); return concat(Observable.fromArray(source1, source2)); } /** * Returns an Observable that emits the items emitted by three Singles, one after the other. *

* *

*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be concatenated * @param source2 * a Single to be concatenated * @param source3 * a Single to be concatenated * @return an Observable that emits items emitted by the three source Singles, one after the other. * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable concat( SingleSource source1, SingleSource source2, SingleSource source3 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); ObjectHelper.requireNonNull(source3, "source3 is null"); return concat(Observable.fromArray(source1, source2, source3)); } /** * Returns an Observable that emits the items emitted by four Singles, one after the other. *

* *

*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be concatenated * @param source2 * a Single to be concatenated * @param source3 * a Single to be concatenated * @param source4 * a Single to be concatenated * @return an Observable that emits items emitted by the four source Singles, one after the other. * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable concat( SingleSource source1, SingleSource source2, SingleSource source3, SingleSource source4 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); ObjectHelper.requireNonNull(source3, "source3 is null"); ObjectHelper.requireNonNull(source4, "source4 is null"); return concat(Observable.fromArray(source1, source2, source3, source4)); } /** * Concatenate the single values, in a non-overlapping fashion, of the Single sources provided in * an array. *
*
Scheduler:
*
{@code concatArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of SingleSource instances * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable concatArray(SingleSource... sources) { return RxJavaObservablePlugins.onAssembly(new ObservableConcatMap(Observable.fromArray(sources), SingleInternalHelper.toObservable(), 2, ErrorMode.BOUNDARY)); } /** * Provides an API (via a cold Completable) 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);
     *
     * });
     * 
*
*
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 SingleObserver subscribes to the returned {@code Single} * @return the new Single instance * @see SingleOnSubscribe * @see Cancellable */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single create(SingleOnSubscribe source) { ObjectHelper.requireNonNull(source, "source is null"); return RxJavaObservablePlugins.onAssembly(new SingleCreate(source)); } /** * Calls a Callable for each individual SingleObserver to return the actual Single source to * be subscribed to. *
*
Scheduler:
*
{@code defer} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param singleSupplier the Callable that is called for each individual SingleObserver and * returns a SingleSource instance to subscribe to * @return the new Single instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single defer(final Callable> singleSupplier) { ObjectHelper.requireNonNull(singleSupplier, "singleSupplier is null"); return RxJavaObservablePlugins.onAssembly(new SingleDefer(singleSupplier)); } /** * Signals a Throwable returned by the callback function for each individual SingleObserver. *
*
Scheduler:
*
{@code error} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param errorSupplier the callable that is called for each individual SingleObserver and * returns a Throwable instance to be emitted. * @return the new Single instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single error(final Callable errorSupplier) { ObjectHelper.requireNonNull(errorSupplier, "errorSupplier is null"); return RxJavaObservablePlugins.onAssembly(new SingleError(errorSupplier)); } /** * Returns a 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 exception * the particular Throwable to pass to {@link SingleObserver#onError onError} * @param * the type of the item (ostensibly) emitted by the Single * @return a Single that invokes the subscriber's {@link SingleObserver#onError onError} method when * the subscriber subscribes to it * @see ReactiveX operators documentation: Throw */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single error(final Throwable exception) { ObjectHelper.requireNonNull(exception, "error is null"); return error(Functions.justCallable(exception)); } /** * Returns a {@link Single} that invokes passed function and emits its result for each new SingleObserver that subscribes. *

* Allows you to defer execution of passed function until 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 fromCallable} does not operate by default on a particular {@link Scheduler}.
*
* * @param callable * function which execution should be deferred, it will be invoked when SingleObserver will subscribe to the {@link Single}. * @param * the type of the item emitted by the {@link Single}. * @return a {@link Single} whose {@link SingleObserver}s' subscriptions trigger an invocation of the given function. */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single fromCallable(final Callable callable) { ObjectHelper.requireNonNull(callable, "callable is null"); return RxJavaObservablePlugins.onAssembly(new SingleFromCallable(callable)); } /** * Converts a {@link Future} into a {@code Single}. *

* *

* You can convert any object that supports the {@link Future} interface into a Single that emits the return * value of the {@link Future#get} method of that object, by passing the object into the {@code from} * method. *

* Important note: This Single is blocking; you cannot dispose it. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@link Scheduler}.
*
* * @param future * the source {@link Future} * @param * the type of object that the {@link Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return a {@code Single} that emits the item from the source {@link Future} * @see ReactiveX operators documentation: From */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single fromFuture(Future future) { return toSingle(Observable.fromFuture(future)); } /** * Converts a {@link Future} into a {@code Single}, with a timeout on the Future. *

* *

* You can convert any object that supports the {@link Future} interface into a {@code Single} that emits * the return value of the {@link Future#get} method of that object, by passing the object into the * {@code from} method. *

* Important note: This {@code Single} is blocking; you cannot dispose it. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@link Scheduler}.
*
* * @param future * the source {@link 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 {@link Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return a {@code Single} that emits the item from the source {@link Future} * @see ReactiveX operators documentation: From */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single fromFuture(Future future, long timeout, TimeUnit unit) { return toSingle(Observable.fromFuture(future, timeout, unit)); } /** * Converts a {@link Future} into a {@code Single}, with a timeout on the Future. *

* *

* You can convert any object that supports the {@link Future} interface into a {@code Single} that emits * the return value of the {@link Future#get} method of that object, by passing the object into the * {@code from} method. *

* Important note: This {@code Single} is blocking; you cannot dispose it. *

*
Scheduler:
*
You specify the {@link Scheduler} where the blocking wait will happen.
*
* * @param future * the source {@link Future} * @param timeout * the maximum time to wait before calling {@code get} * @param unit * the {@link TimeUnit} of the {@code timeout} argument * @param scheduler * the Scheduler to use for the blocking wait * @param * the type of object that the {@link Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return a {@code Single} that emits the item from the source {@link Future} * @see ReactiveX operators documentation: From */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public static Single fromFuture(Future future, long timeout, TimeUnit unit, Scheduler scheduler) { return toSingle(Observable.fromFuture(future, timeout, unit, scheduler)); } /** * Converts a {@link Future}, operating on a specified {@link Scheduler}, into a {@code Single}. *

* *

* You can convert any object that supports the {@link Future} interface into a {@code Single} that emits * the return value of the {@link Future#get} method of that object, by passing the object into the * {@code from} method. *

*
Scheduler:
*
You specify which {@link Scheduler} this operator will use
*
* * @param future * the source {@link Future} * @param scheduler * the {@link Scheduler} to wait for the Future on. Use a Scheduler such as * {@link Schedulers#io()} that can block and wait on the Future * @param * the type of object that the {@link Future} returns, and also the type of item to be emitted by * the resulting {@code Single} * @return a {@code Single} that emits the item from the source {@link Future} * @see ReactiveX operators documentation: From */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public static Single fromFuture(Future future, Scheduler scheduler) { return toSingle(Observable.fromFuture(future, scheduler)); } /** * Wraps a specific ObservableSource into a Single and signals its single element or error. *

If the ObservableSource is empty, a NoSuchElementException is signalled. * If the source has more than one element, an IndexOutOfBoundsException is signalled. *

*
Scheduler:
*
{@code fromObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param observableSource the source Observable, not null * @param * the type of the item emitted by the {@link Single}. * @return the new Single instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single fromObservable(ObservableSource observableSource) { ObjectHelper.requireNonNull(observableSource, "observableSource is null"); return RxJavaObservablePlugins.onAssembly(new ObservableSingleSingle(observableSource, null)); } /** * 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 a {@code Single} that emits {@code item} * @see ReactiveX operators documentation: Just */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single just(final T item) { ObjectHelper.requireNonNull(item, "value is null"); return RxJavaObservablePlugins.onAssembly(new SingleJust(item)); } /** * Merges an Iterable sequence of SingleSource instances into a single Observable sequence, * running all SingleSources at once. *
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* @param the common and resulting value type * @param sources the Iterable sequence of SingleSource sources * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Observable merge(Iterable> sources) { return merge(Observable.fromIterable(sources)); } /** * Merges an Observable sequence of SingleSource instances into a single Observable sequence, * running all SingleSources at once. *
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* @param the common and resulting value type * @param sources the Observable sequence of SingleSource sources * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static Observable merge(ObservableSource> sources) { ObjectHelper.requireNonNull(sources, "sources is null"); return RxJavaObservablePlugins.onAssembly(new ObservableFlatMap(sources, SingleInternalHelper.toObservable(), false, Integer.MAX_VALUE, Observable.bufferSize())); } /** * Flattens a {@code Single} that emits a {@code Single} into a single {@code Single} that emits the item * emitted by the nested {@code Single}, without any transformation. *

* *

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the sources and the output * @param source * a {@code Single} that emits a {@code Single} * @return a {@code Single} that emits the item that is the result of flattening the {@code Single} emitted * by {@code source} * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static Single merge(SingleSource> source) { ObjectHelper.requireNonNull(source, "source is null"); return RxJavaObservablePlugins.onAssembly(new SingleFlatMap, T>(source, (Function)Functions.identity())); } /** * Flattens two Singles into a single Observable, without any transformation. *

* *

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

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be merged * @param source2 * a Single to be merged * @return an Observable that emits all of the items emitted by the source Singles * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable merge( SingleSource source1, SingleSource source2 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); return merge(Observable.fromArray(source1, source2)); } /** * Flattens three Singles into a single Observable, without any transformation. *

* *

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

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be merged * @param source2 * a Single to be merged * @param source3 * a Single to be merged * @return an Observable that emits all of the items emitted by the source Singles * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable merge( SingleSource source1, SingleSource source2, SingleSource source3 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); ObjectHelper.requireNonNull(source3, "source3 is null"); return merge(Observable.fromArray(source1, source2, source3)); } /** * Flattens four Singles into a single Observable, without any transformation. *

* *

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

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a Single to be merged * @param source2 * a Single to be merged * @param source3 * a Single to be merged * @param source4 * a Single to be merged * @return an Observable that emits all of the items emitted by the source Singles * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Observable merge( SingleSource source1, SingleSource source2, SingleSource source3, SingleSource source4 ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); ObjectHelper.requireNonNull(source3, "source3 is null"); ObjectHelper.requireNonNull(source4, "source4 is null"); return merge(Observable.fromArray(source1, source2, source3, source4)); } /** * Returns a singleton instance of a never-signalling Single (only calls 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") public static Single never() { return RxJavaObservablePlugins.onAssembly((Single) SingleNever.INSTANCE); } /** * Signals success with 0L value after the given delay for each SingleObserver. *
*
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 Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Single timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); } /** * Signals success with 0L value after the given delay for each SingleObserver. *
*
Scheduler:
*
you specify the {@link Scheduler} to signal on.
*
* @param delay the delay amount * @param unit the time unit of the delay * @param scheduler the scheduler where the single 0L will be emitted * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public static Single timer(final long delay, final TimeUnit unit, final Scheduler scheduler) { ObjectHelper.requireNonNull(unit, "unit is null"); ObjectHelper.requireNonNull(scheduler, "scheduler is null"); return RxJavaObservablePlugins.onAssembly(new SingleTimer(delay, unit, scheduler)); } /** * Compares two SingleSources and emits true if they emit the same value (compared via Object.equals). *
*
Scheduler:
*
{@code equals} does not operate by default on a particular {@link Scheduler}.
*
* @param the common value type * @param first the first SingleSource instance * @param second the second SingleSource instance * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single equals(final SingleSource first, final SingleSource second) { // NOPMD ObjectHelper.requireNonNull(first, "first is null"); ObjectHelper.requireNonNull(second, "second is null"); return RxJavaObservablePlugins.onAssembly(new SingleEquals(first, second)); } /** * Advanced use only: creates a Single instance without * any safeguards by using a callback that is called with a 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 SingleObserver * @return the new Single instance * @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 @SchedulerSupport(SchedulerSupport.NONE) public static Single unsafeCreate(SingleSource onSubscribe) { ObjectHelper.requireNonNull(onSubscribe, "onSubscribe is null"); if (onSubscribe instanceof Single) { throw new IllegalArgumentException("unsafeCreate(Single) should be upgraded"); } return RxJavaObservablePlugins.onAssembly(new SingleFromUnsafeSource(onSubscribe)); } /** * Allows using and disposing a resource while running a 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 SingleSource generated * @param the resource type * @param resourceSupplier the Callable called for each SingleObserver to generate a resource Object * @param singleFunction the function called with the returned resource * Object from {@code resourceSupplier} and should return a SingleSource instance * to be run by the operator * @param disposer the consumer of the generated resource that is called exactly once for * that particular resource when the generated SingleSource terminates * (successfully or with an error) or gets cancelled. * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single using(Callable resourceSupplier, Function> singleFunction, Consumer disposer) { return using(resourceSupplier, singleFunction, disposer, true); } /** * Allows using and disposing a resource while running a 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 SingleSource generated * @param the resource type * @param resourceSupplier the Callable called for each SingleObserver to generate a resource Object * @param singleFunction the function called with the returned resource * Object from {@code resourceSupplier} and should return a SingleSource instance * to be run by the operator * @param disposer the consumer of the generated resource that is called exactly once for * that particular resource when the generated SingleSource terminates * (successfully or with an error) or gets cancelled. * @param eager * if true, the disposer is called before the terminal event is signalled * if false, the disposer is called after the terminal event is delivered to downstream * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single using( final Callable resourceSupplier, final Function> singleFunction, final Consumer disposer, final boolean eager) { ObjectHelper.requireNonNull(resourceSupplier, "resourceSupplier is null"); ObjectHelper.requireNonNull(singleFunction, "singleFunction is null"); ObjectHelper.requireNonNull(disposer, "disposer is null"); return RxJavaObservablePlugins.onAssembly(new SingleUsing(resourceSupplier, singleFunction, disposer, eager)); } /** * Wraps a SingleSource instance into a new Single instance if not already a 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 Single wrapper or the source cast to Single (if possible) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single wrap(SingleSource source) { ObjectHelper.requireNonNull(source, "source is null"); if (source instanceof Single) { return RxJavaObservablePlugins.onAssembly((Single)source); } return RxJavaObservablePlugins.onAssembly(new SingleFromUnsafeSource(source)); } /** * Waits until all SingleSource sources provided by the 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 downstream. *

* 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 {@code ClassCastException}. * *

* *

* If any of the SingleSources signal an error, all other SingleSources get cancelled 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 Iterable sequence of SingleSource instances * @param zipper the function that receives an array with values from each SingleSource * and should return a value to be emitted to downstream * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single zip(final Iterable> sources, Function zipper) { ObjectHelper.requireNonNull(zipper, "zipper is null"); ObjectHelper.requireNonNull(sources, "sources is null"); return RxJavaObservablePlugins.onAssembly(new SingleZipIterable(sources, zipper)); } /** * Returns a Single that emits the results of a specified combiner function applied to two items emitted by * two other Singles. *

* *

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

* *

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

* *

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

* *

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

* *

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

* *

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

* *

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

* *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the first source Single's value type * @param the second source Single's value type * @param the third source Single's value type * @param the fourth source Single's value type * @param the fifth source Single's value type * @param the sixth source Single's value type * @param the seventh source Single's value type * @param the eighth source Single's value type * @param the ninth source Single's value type * @param the result value type * @param source1 * the first source Single * @param source2 * a second source Single * @param source3 * a third source Single * @param source4 * a fourth source Single * @param source5 * a fifth source Single * @param source6 * a sixth source Single * @param source7 * a seventh source Single * @param source8 * an eighth source Single * @param source9 * a ninth source Single * @param zipper * a function that, when applied to the item emitted by each of the source Singles, results in an * item that will be emitted by the resulting Single * @return a Single that emits the zipped results * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public static Single zip( SingleSource source1, SingleSource source2, SingleSource source3, SingleSource source4, SingleSource source5, SingleSource source6, SingleSource source7, SingleSource source8, SingleSource source9, Function9 zipper ) { ObjectHelper.requireNonNull(source1, "source1 is null"); ObjectHelper.requireNonNull(source2, "source2 is null"); ObjectHelper.requireNonNull(source3, "source3 is null"); ObjectHelper.requireNonNull(source4, "source4 is null"); ObjectHelper.requireNonNull(source5, "source5 is null"); ObjectHelper.requireNonNull(source6, "source6 is null"); ObjectHelper.requireNonNull(source7, "source7 is null"); ObjectHelper.requireNonNull(source8, "source8 is null"); ObjectHelper.requireNonNull(source9, "source9 is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7, source8, source9); } /** * Waits until all 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. *

* 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 {@code ClassCastException}. * *

* *

* If any of the SingleSources signal an error, all other SingleSources get cancelled 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 SingleSource instances * @param zipper the function that receives an array with values from each SingleSource * and should return a value to be emitted to downstream * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Single zipArray(Function zipper, SingleSource... sources) { ObjectHelper.requireNonNull(zipper, "zipper is null"); ObjectHelper.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return error(new NoSuchElementException()); } return RxJavaObservablePlugins.onAssembly(new SingleZipArray(sources, zipper)); } /** * Signals the event of this or the other SingleSource whichever signals first. *
*
Scheduler:
*
{@code ambWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other SingleSource to race for the first emission of success or error * @return the new Single instance. A subscription to this provided source will occur after subscribing * to the current source. * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Single ambWith(SingleSource other) { ObjectHelper.requireNonNull(other, "other is null"); return ambArray(this, other); } /** * Hides the identity of the current Single, including the 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 Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single hide() { return RxJavaObservablePlugins.onAssembly(new SingleHide(this)); } /** * Transform a Single by applying a particular Transformer function to it. *

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

* If the operator you are creating is designed to act on the individual item emitted by a Single, use * {@link #lift}. If your operator is designed to transform the source 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 null * @return the source Single, transformed by the transformer function * @see RxJava wiki: Implementing Your Own Operators */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single compose(SingleTransformer transformer) { return wrap(((SingleTransformer) ObjectHelper.requireNonNull(transformer, "transformer is null")).apply(this)); } /** * Stores the success value or exception from the current Single and replays it to late SingleObservers. *

* The returned Single subscribes to the current Single when the first SingleObserver subscribes. *

*
Scheduler:
*
{@code cache} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single cache() { return RxJavaObservablePlugins.onAssembly(new SingleCache(this)); } /** * Casts the success value of the current Single into the target type or signals a * 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 Single * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single cast(final Class clazz) { ObjectHelper.requireNonNull(clazz, "clazz is null"); return map(Functions.castFunction(clazz)); } /** * Returns an Observable that emits the item emitted by the source Single, then the item emitted by the * specified Single. *

* *

*
Scheduler:
*
{@code concatWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a Single to be concatenated after the current * @return an Observable that emits the item emitted by the source Single, followed by the item emitted by * {@code t1} * @see ReactiveX operators documentation: Concat */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable concatWith(SingleSource other) { return concat(this, other); } /** * Delays the emission of the success signal from the current Single by the specified amount. *
*
Scheduler:
*
{@code delay} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param time the time amount to delay the emission of the success signal * @param unit the time unit * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single delay(long time, TimeUnit unit) { return delay(time, unit, Schedulers.computation()); } /** * Delays the emission of the success signal from the current Single by the specified amount. *
*
Scheduler:
*
you specify the {@link Scheduler} where the non-blocking wait and emission happens
*
* * @param time the time amount to delay the emission of the success signal * @param unit the time unit * @param scheduler the target scheduler to use for the non-blocking wait and emission * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single delay(final long time, final TimeUnit unit, final Scheduler scheduler) { ObjectHelper.requireNonNull(unit, "unit is null"); ObjectHelper.requireNonNull(scheduler, "scheduler is null"); return RxJavaObservablePlugins.onAssembly(new SingleDelay(this, time, unit, scheduler)); } /** * Delays the actual subscription to the current Single until the given other CompletableSource * completes. *

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

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

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

*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the other source * @param other the SingleSource that has to complete before the subscription to the * current Single happens * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single delaySubscription(SingleSource other) { ObjectHelper.requireNonNull(other, "other is null"); return RxJavaObservablePlugins.onAssembly(new SingleDelayWithSingle(this, other)); } /** * Delays the actual subscription to the current Single until the given other 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 Single happens. *

*
Scheduler:
*
{@code delaySubscription} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the other source * @param other the ObservableSource that has to signal a value or complete before the * subscription to the current Single happens * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single delaySubscription(ObservableSource other) { ObjectHelper.requireNonNull(other, "other is null"); return RxJavaObservablePlugins.onAssembly(new SingleDelayWithObservable(this, other)); } /** * Delays the actual subscription to the current Single until the given time delay elapsed. *
*
Scheduler:
*
{@code delaySubscription} does by default subscribe to the current Single * on the {@code computation} {@link Scheduler} after the delay.
*
* @param the element type of the other source * @param time the time amount to wait with the subscription * @param unit the time unit of the waiting * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single delaySubscription(long time, TimeUnit unit) { return delaySubscription(time, unit, Schedulers.computation()); } /** * Delays the actual subscription to the current Single until the given time delay elapsed. *
*
Scheduler:
*
{@code delaySubscription} does by default subscribe to the current Single * on the {@link Scheduler} you provided, after the delay.
*
* @param the element type of the other source * @param time the time amount to wait with the subscription * @param unit the time unit of the waiting * @param scheduler the scheduler to wait on and subscribe on to the current Single * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single delaySubscription(long time, TimeUnit unit, Scheduler scheduler) { return delaySubscription(Observable.timer(time, unit, scheduler)); } /** * 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 Consumer that will be called after emitting an item from upstream to the downstream * @return the new Single instance * @since 2.1 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doAfterSuccess(Consumer onAfterSuccess) { ObjectHelper.requireNonNull(onAfterSuccess, "doAfterSuccess is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoAfterSuccess(this, onAfterSuccess)); } /** * Registers an {@link Action} to be called after this Single invokes either onSuccess or 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 {@link Action} to be invoked when the source Single finishes * @return a Single that emits the same items as the source Single, then invokes the * {@link Action} * @see ReactiveX operators documentation: Do * @since 2.1 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doAfterTerminate(Action onAfterTerminate) { ObjectHelper.requireNonNull(onAfterTerminate, "onAfterTerminate is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoAfterTerminate(this, onAfterTerminate)); } /** * Calls the specified action after this Single signals onSuccess or 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 Single terminates or gets cancelled * @return the new Single instance * @since 2.1 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doFinally(Action onFinally) { ObjectHelper.requireNonNull(onFinally, "onFinally is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoFinally(this, onFinally)); } /** * Calls the shared consumer with the Disposable sent through the onSubscribe for each * SingleObserver that subscribes to the current Single. *

*
Scheduler:
*
{@code doOnSubscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param onSubscribe the consumer called with the Disposable sent via onSubscribe * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnSubscribe(final Consumer onSubscribe) { ObjectHelper.requireNonNull(onSubscribe, "onSubscribe is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoOnSubscribe(this, onSubscribe)); } /** * Calls the shared consumer with the success value sent via onSuccess for each * SingleObserver that subscribes to the current Single. *
*
Scheduler:
*
{@code doOnSuccess} does not operate by default on a particular {@link Scheduler}.
*
* @param onSuccess the consumer called with the success value of onSuccess * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnSuccess(final Consumer onSuccess) { ObjectHelper.requireNonNull(onSuccess, "onSuccess is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoOnSuccess(this, onSuccess)); } /** * Calls the shared consumer with the error sent via onError or the value * via onSuccess for each SingleObserver that subscribes to the current 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 Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnEvent(final BiConsumer onEvent) { ObjectHelper.requireNonNull(onEvent, "onEvent is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoOnEvent(this, onEvent)); } /** * Calls the shared consumer with the error sent via onError for each * SingleObserver that subscribes to the current Single. *
*
Scheduler:
*
{@code doOnError} does not operate by default on a particular {@link Scheduler}.
*
* @param onError the consumer called with the success value of onError * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnError(final Consumer onError) { ObjectHelper.requireNonNull(onError, "onError is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoOnError(this, onError)); } /** * Calls the shared runnable if a SingleObserver subscribed to the current Single * disposes the common Disposable it received via 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 Single instance * @throws NullPointerException if onDispose is null * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single doOnDispose(final Action onDispose) { ObjectHelper.requireNonNull(onDispose, "onDispose is null"); return RxJavaObservablePlugins.onAssembly(new SingleDoOnDispose(this, onDispose)); } /** * Filters the success item of the Single via a predicate function and emitting it if the predicate * returns 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 source Maybe, returning {@code true} * if it passes the filter * @return a Maybe that emit the item emitted by the source Maybe that the filter * evaluates as {@code true} * @see ReactiveX operators documentation: Filter */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Maybe filter(Predicate predicate) { ObjectHelper.requireNonNull(predicate, "predicate is null"); return RxJavaObservablePlugins.onAssembly(new MaybeFilterSingle(this, predicate)); } /** * Returns a Single that is based on applying a specified function to the item emitted by the source Single, * where that function returns a 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 source Single, returns a SingleSource * @return the Single returned from {@code mapper} when applied to the item emitted by the source Single * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single flatMap(Function> mapper) { ObjectHelper.requireNonNull(mapper, "mapper is null"); return RxJavaObservablePlugins.onAssembly(new SingleFlatMap(this, mapper)); } /** * Returns a Maybe that is based on applying a specified function to the item emitted by the source Single, * where that function returns a 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 source Single, returns a MaybeSource * @return the Maybe returned from {@code mapper} when applied to the item emitted by the source Single * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Maybe flatMapMaybe(final Function> mapper) { ObjectHelper.requireNonNull(mapper, "mapper is null"); return RxJavaObservablePlugins.onAssembly(new SingleFlatMapMaybe(this, mapper)); } /** * Returns an Observable that maps a success value into an Iterable and emits its items. *

* *

*
Scheduler:
*
{@code flattenAsObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of item emitted by the resulting Iterable * @param mapper * a function that returns an Iterable sequence of values for when given an item emitted by the * source Single * @return the new Observable instance * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable flattenAsObservable(final Function> mapper) { ObjectHelper.requireNonNull(mapper, "mapper is null"); return RxJavaObservablePlugins.onAssembly(new SingleFlatMapIterableObservable(this, mapper)); } /** * Returns an Observable that is based on applying a specified function to the item emitted by the source Single, * where that function returns an 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 source Single, returns an ObservableSource * @return the Observable returned from {@code func} when applied to the item emitted by the source Single * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable flatMapObservable(Function> mapper) { return toObservable().flatMap(mapper); } /** * Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the * source {@link Single}, where that function returns a {@link Completable}. *

* *

*
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 source Single, returns a * Completable * @return the Completable returned from {@code func} when applied to the item emitted by the source Single * @see ReactiveX operators documentation: FlatMap * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Completable flatMapCompletable(final Function mapper) { ObjectHelper.requireNonNull(mapper, "mapper is null"); return RxJavaObservablePlugins.onAssembly(new SingleFlatMapCompletable(this, mapper)); } /** * Waits in a blocking fashion until the current 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}.
*
* @return the success value */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final T blockingGet() { BlockingMultiObserver observer = new BlockingMultiObserver(); subscribe(observer); return observer.blockingGet(); } /** * Lifts a function to the current Single and returns a new Single that when subscribed to will pass the * values of the current Single through the Operator function. *

* In other words, this allows chaining TaskExecutors together on a Single for acting on the values within * the Single. *

* {@code task.map(...).filter(...).lift(new OperatorA()).lift(new OperatorB(...)).subscribe() } *

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

*
Scheduler:
*
{@code lift} does not operate by default on a particular {@link Scheduler}.
*
* * @param the downstream's value type (output) * @param lift * the Operator that implements the Single-operating function to be applied to the source Single * @return a Single that is the result of applying the lifted Operator to the source Single * @see RxJava wiki: Implementing Your Own Operators */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single lift(final SingleOperator lift) { ObjectHelper.requireNonNull(lift, "onLift is null"); return RxJavaObservablePlugins.onAssembly(new SingleLift(this, lift)); } /** * Returns a Single that applies a specified function to the item emitted by the source 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 Single * @return a Single that emits the item from the source Single, transformed by the specified function * @see ReactiveX operators documentation: Map */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single map(Function mapper) { ObjectHelper.requireNonNull(mapper, "mapper is null"); return RxJavaObservablePlugins.onAssembly(new SingleMap(this, mapper)); } /** * Signals true if the current Single signals a success value that is Object-equals with the value * provided. *
*
Scheduler:
*
{@code contains} does not operate by default on a particular {@link Scheduler}.
*
* @param value the value to compare against the success value of this Single * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single contains(Object value) { return contains(value, ObjectHelper.equalsPredicate()); } /** * Signals true if the current Single signals a success value that is equal with * the value provided by calling a bi-predicate. *
*
Scheduler:
*
{@code contains} does not operate by default on a particular {@link Scheduler}.
*
* @param value the value to compare against the success value of this Single * @param comparer the function that receives the success value of this Single, the value provided * and should return true if they are considered equal * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single contains(final Object value, final BiPredicate comparer) { ObjectHelper.requireNonNull(value, "value is null"); ObjectHelper.requireNonNull(comparer, "comparer is null"); return RxJavaObservablePlugins.onAssembly(new SingleContains(this, value, comparer)); } /** * Flattens this and another Single into a single Observable, without any transformation. *

* *

* You can combine items emitted by multiple Singles so that they appear as a single Observable, by using * the {@code mergeWith} method. *

*
Scheduler:
*
{@code mergeWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a Single to be merged * @return that emits all of the items emitted by the source Singles * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable mergeWith(SingleSource other) { return merge(this, other); } /** * Modifies a Single to emit its item (or notify of its error) on a specified {@link Scheduler}, * asynchronously. *

* *

*
Scheduler:
*
you specify which {@link Scheduler} this operator will use
*
* * @param scheduler * the {@link Scheduler} to notify subscribers on * @return the source Single modified so that its subscribers are notified on the specified * {@link Scheduler} * @see ReactiveX operators documentation: ObserveOn * @see RxJava Threading Examples * @see #subscribeOn */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single observeOn(final Scheduler scheduler) { ObjectHelper.requireNonNull(scheduler, "scheduler is null"); return RxJavaObservablePlugins.onAssembly(new SingleObserveOn(this, scheduler)); } /** * Instructs a Single to emit an item (returned by a specified function) rather than invoking * {@link SingleObserver#onError onError} if it encounters an error. *

* *

* By default, when a Single encounters an error that prevents it from emitting the expected item to its * subscriber, the Single invokes its subscriber's {@link SingleObserver#onError} method, and then quits * without invoking any more of its subscriber's methods. The {@code onErrorReturn} method changes this * behavior. If you pass a function ({@code resumeFunction}) to a Single's {@code onErrorReturn} method, if * the original Single encounters an error, instead of invoking its subscriber'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 resumeFunction * a function that returns an item that the new Single will emit if the source Single encounters * an error * @return the original Single with appropriately modified behavior * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorReturn(final Function resumeFunction) { ObjectHelper.requireNonNull(resumeFunction, "resumeFunction is null"); return RxJavaObservablePlugins.onAssembly(new SingleOnErrorReturn(this, resumeFunction, null)); } /** * Signals the specified value as success in case the current Single signals an error. *
*
Scheduler:
*
{@code onErrorReturnItem} does not operate by default on a particular {@link Scheduler}.
*
* @param value the value to signal if the current Single fails * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorReturnItem(final T value) { ObjectHelper.requireNonNull(value, "value is null"); return RxJavaObservablePlugins.onAssembly(new SingleOnErrorReturn(this, null, value)); } /** * Instructs a Single to pass control to another Single rather than invoking * {@link SingleObserver#onError(Throwable)} if it encounters an error. *

* *

* By default, when a Single encounters an error that prevents it from emitting the expected item to * its {@link SingleObserver}, the Single invokes its SingleObserver's {@code onError} method, and then quits * without invoking any more of its SingleObserver's methods. The {@code onErrorResumeNext} method changes this * behavior. If you pass another Single ({@code resumeSingleInCaseOfError}) to a Single's * {@code onErrorResumeNext} method, if the original Single encounters an error, instead of invoking its * SingleObserver's {@code onError} method, it will instead relinquish control to {@code resumeSingleInCaseOfError} which * will invoke the SingleObserver's {@link SingleObserver#onSuccess onSuccess} method if it is able to do so. In such a case, * because no Single necessarily invokes {@code onError}, the 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 resumeSingleInCaseOfError a Single that will take control if source Single encounters an error. * @return the original Single, with appropriately modified behavior. * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorResumeNext(final Single resumeSingleInCaseOfError) { ObjectHelper.requireNonNull(resumeSingleInCaseOfError, "resumeSingleInCaseOfError is null"); return onErrorResumeNext(Functions.justFunction(resumeSingleInCaseOfError)); } /** * Instructs a Single to pass control to another Single rather than invoking * {@link SingleObserver#onError(Throwable)} if it encounters an error. *

* *

* By default, when a Single encounters an error that prevents it from emitting the expected item to * its {@link SingleObserver}, the Single invokes its SingleObserver's {@code onError} method, and then quits * without invoking any more of its SingleObserver's methods. The {@code onErrorResumeNext} method changes this * behavior. If you pass a function that will return another Single ({@code resumeFunctionInCaseOfError}) to a Single's * {@code onErrorResumeNext} method, if the original Single encounters an error, instead of invoking its * SingleObserver's {@code onError} method, it will instead relinquish control to {@code resumeSingleInCaseOfError} which * will invoke the SingleObserver's {@link SingleObserver#onSuccess onSuccess} method if it is able to do so. In such a case, * because no Single necessarily invokes {@code onError}, the 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 resumeFunctionInCaseOfError a function that returns a Single that will take control if source Single encounters an error. * @return the original Single, with appropriately modified behavior. * @see ReactiveX operators documentation: Catch * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single onErrorResumeNext( final Function> resumeFunctionInCaseOfError) { ObjectHelper.requireNonNull(resumeFunctionInCaseOfError, "resumeFunctionInCaseOfError is null"); return RxJavaObservablePlugins.onAssembly(new SingleResumeNext(this, resumeFunctionInCaseOfError)); } /** * Repeatedly re-subscribes to the current Single and emits each success value. *
*
Scheduler:
*
{@code repeat} does not operate by default on a particular {@link Scheduler}.
*
* @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeat() { return toObservable().repeat(); } /** * Re-subscribes to the current Single at most the given number of times and emits each success value. *
*
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 Single * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeat(long times) { return toObservable().repeat(times); } /** * Re-subscribes to the current Single if * the ObservableSource returned by the handler function signals a value in response to a * value signalled through the Observable the handle receives. *
*
Scheduler:
*
{@code repeatWhen} does not operate by default on a particular {@link Scheduler}.
*
* @param handler the function that is called with an Observable that signals a value when the Single * signalled a success value and returns an ObservableSource that has to signal a value to * trigger a resubscription to the current Single, otherwise the terminal signal of * the ObservableSource will be the terminal signal of the sequence as well. * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeatWhen(Function, ? extends ObservableSource> handler) { return toObservable().repeatWhen(handler); } /** * Re-subscribes to the current Single until the given BooleanSupplier returns true. *
*
Scheduler:
*
{@code repeatUntil} does not operate by default on a particular {@link Scheduler}.
*
* @param stop the BooleanSupplier called after the current Single succeeds and if returns false, * the Single is re-subscribed; otherwise the sequence completes. * @return the new Observable instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable repeatUntil(BooleanSupplier stop) { return toObservable().repeatUntil(stop); } /** * Repeatedly re-subscribes to the current Single indefinitely if it fails with an onError. *
*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single retry() { return toSingle(toObservable().retry()); } /** * Repeatedly re-subscribe at most the specified times to the current Single * if it fails with an 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 Single fails * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single retry(long times) { return toSingle(toObservable().retry(times)); } /** * Re-subscribe to the current Single if the given predicate returns true when the Single fails * with an 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 Throwable * and should return true if a resubscription should happen * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single retry(BiPredicate predicate) { return toSingle(toObservable().retry(predicate)); } /** * Re-subscribe to the current Single if the given predicate returns true when the Single fails * with an onError. *
*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @param predicate the predicate called with the failure Throwable * and should return true if a resubscription should happen * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single retry(Predicate predicate) { return toSingle(toObservable().retry(predicate)); } /** * Re-subscribes to the current Single if and when the ObservableSource returned by the handler * function signals a value. *

* If the ObservableSource signals an onComplete, the resulting Single will signal a NoSuchElementException. *

*
Scheduler:
*
{@code retryWhen} does not operate by default on a particular {@link Scheduler}.
*
* * @param handler the function that receives an Observable of the error the Single emits and should * return an ObservableSource that should signal a normal value (in response to the * throwable the Observable emits) to trigger a resubscription or signal an error to * be the output of the resulting Single * @return the new Single instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single retryWhen(Function, ? extends ObservableSource> handler) { return toSingle(toObservable().retryWhen(handler)); } /** * Subscribes to a Single but ignore its emission or notification. *

* If the Single emits an error, it is wrapped into an * {@link io.reactivex.common.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the RxJavaCommonPlugins.onError handler. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @return a {@link Disposable} reference can request the {@link Single} stop work. * @see ReactiveX operators documentation: Subscribe */ @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), Functions.ON_ERROR_MISSING); } /** * Subscribes to a 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 Throwable * (whichever is not null) * @return a {@link Disposable} reference can request the {@link Single} stop work. * @see ReactiveX operators documentation: Subscribe * @throws NullPointerException * if {@code onCallback} is null */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(final BiConsumer onCallback) { ObjectHelper.requireNonNull(onCallback, "onCallback is null"); BiConsumerSingleObserver s = new BiConsumerSingleObserver(onCallback); subscribe(s); return s; } /** * Subscribes to a Single and provides a callback to handle the item it emits. *

* If the Single emits an error, it is wrapped into an * {@link io.reactivex.common.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the RxJavaCommonPlugins.onError 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 Single * @return a {@link Disposable} reference can request the {@link Single} stop work. * @throws NullPointerException * if {@code onSuccess} is null * @see ReactiveX operators documentation: Subscribe */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(Consumer onSuccess) { return subscribe(onSuccess, Functions.ON_ERROR_MISSING); } /** * Subscribes to a 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 Single * @param onError * the {@code Consumer} you have designed to accept any error notification from the * Single * @return a {@link Disposable} reference can request the {@link Single} stop work. * @see ReactiveX operators documentation: Subscribe * @throws NullPointerException * if {@code onSuccess} is null, or * if {@code onError} is null */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(final Consumer onSuccess, final Consumer onError) { ObjectHelper.requireNonNull(onSuccess, "onSuccess is null"); ObjectHelper.requireNonNull(onError, "onError is null"); ConsumerSingleObserver s = new ConsumerSingleObserver(onSuccess, onError); subscribe(s); return s; } @SchedulerSupport(SchedulerSupport.NONE) @Override public final void subscribe(SingleObserver subscriber) { ObjectHelper.requireNonNull(subscriber, "subscriber is null"); subscriber = RxJavaObservablePlugins.onSubscribe(this, subscriber); ObjectHelper.requireNonNull(subscriber, "subscriber returned by the RxJavaObservablePlugins hook is null"); try { subscribeActual(subscriber); } catch (NullPointerException ex) { throw ex; } catch (Throwable ex) { Exceptions.throwIfFatal(ex); NullPointerException npe = new NullPointerException("subscribeActual failed"); npe.initCause(ex); throw npe; } } /** * Override this method in subclasses to handle the incoming SingleObservers. * @param observer the SingleObserver to handle, not null */ protected abstract void subscribeActual(@NonNull SingleObserver observer); /** * Subscribes a given SingleObserver (subclass) to this Single and returns the given * SingleObserver as is. *

Usage example: *


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

* *

*
Scheduler:
*
You specify which {@link Scheduler} this operator will use
*
* * @param scheduler * the {@link Scheduler} to perform subscription actions on * @return the source Single modified so that its subscriptions happen on the specified {@link Scheduler} * @see ReactiveX operators documentation: SubscribeOn * @see RxJava Threading Examples * @see #observeOn */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single subscribeOn(final Scheduler scheduler) { ObjectHelper.requireNonNull(scheduler, "scheduler is null"); return RxJavaObservablePlugins.onAssembly(new SingleSubscribeOn(this, scheduler)); } /** * Returns a Single that emits the item emitted by the source Single until a Completable 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 Completable whose termination will cause {@code takeUntil} to emit the item from the source * Single * @return a Single that emits the item emitted by the source Single until such time as {@code other} terminates. * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single takeUntil(final CompletableSource other) { ObjectHelper.requireNonNull(other, "other is null"); return takeUntil(new CompletableToObservable(other)); } /** * Returns a Single that emits the item emitted by the source Single until an ObservableSource 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 ObservableSource whose first emitted item will cause {@code takeUntil} to emit the item from the source * Single * @param * the type of items emitted by {@code other} * @return a Single that emits the item emitted by the source Single until such time as {@code other} emits * its first item * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single takeUntil(final ObservableSource other) { ObjectHelper.requireNonNull(other, "other is null"); return RxJavaObservablePlugins.onAssembly(new SingleTakeUntil(this, other)); } /** * Returns a Single that emits the item emitted by the source Single until a second 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 Single whose emitted item will cause {@code takeUntil} to emit the item from the source Single * @param * the type of item emitted by {@code other} * @return a Single that emits the item emitted by the source Single until such time as {@code other} emits its item * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single takeUntil(final SingleSource other) { ObjectHelper.requireNonNull(other, "other is null"); return takeUntil(new SingleToObservable(other)); } /** * Signals a TimeoutException if the current Single doesn't signal a success value within the * specified timeout window. *
*
Scheduler:
*
{@code timeout} signals the TimeoutException on the {@code computation} {@link Scheduler}.
*
* @param timeout the timeout amount * @param unit the time unit * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single timeout(long timeout, TimeUnit unit) { return timeout0(timeout, unit, Schedulers.computation(), null); } /** * Signals a TimeoutException if the current Single doesn't signal a success value within the * specified timeout window. *
*
Scheduler:
*
{@code timeout} signals the TimeoutException on the {@link Scheduler} you specify.
*
* @param timeout the timeout amount * @param unit the time unit * @param scheduler the target scheduler where the timeout is awaited and the TimeoutException * signalled * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single timeout(long timeout, TimeUnit unit, Scheduler scheduler) { return timeout0(timeout, unit, scheduler, null); } /** * Runs the current Single and if it doesn't signal within the specified timeout window, it is * cancelled and the other SingleSource subscribed to. *
*
Scheduler:
*
{@code timeout} subscribes to the other SingleSource on the {@link Scheduler} you specify.
*
* @param timeout the timeout amount * @param unit the time unit * @param scheduler the scheduler where the timeout is awaited and the subscription to other happens * @param other the other SingleSource that gets subscribed to if the current Single times out * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Single timeout(long timeout, TimeUnit unit, Scheduler scheduler, SingleSource other) { ObjectHelper.requireNonNull(other, "other is null"); return timeout0(timeout, unit, scheduler, other); } /** * Runs the current Single and if it doesn't signal within the specified timeout window, it is * cancelled and the other SingleSource subscribed to. *
*
Scheduler:
*
{@code timeout} subscribes to the other SingleSource on * the {@code computation} {@link Scheduler}.
*
* @param timeout the timeout amount * @param unit the time unit * @param other the other SingleSource that gets subscribed to if the current Single times out * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single timeout(long timeout, TimeUnit unit, SingleSource other) { ObjectHelper.requireNonNull(other, "other is null"); return timeout0(timeout, unit, Schedulers.computation(), other); } private Single timeout0(final long timeout, final TimeUnit unit, final Scheduler scheduler, final SingleSource other) { ObjectHelper.requireNonNull(unit, "unit is null"); ObjectHelper.requireNonNull(scheduler, "scheduler is null"); return RxJavaObservablePlugins.onAssembly(new SingleTimeout(this, timeout, unit, scheduler, other)); } /** * Calls the specified converter function with the current Single instance * during assembly time and returns its result. *
*
Scheduler:
*
{@code to} does not operate by default on a particular {@link Scheduler}.
*
* @param the result type * @param convert the function that is called with the current Single instance during * assembly time that should return some value to be the result * * @return the value returned by the convert function */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final R to(Function, R> convert) { try { return ObjectHelper.requireNonNull(convert, "convert is null").apply(this); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); throw ExceptionHelper.wrapOrThrow(ex); } } /** * Returns a {@link Completable} that discards result of the {@link Single} * and calls {@code onComplete} when this source {@link Single} calls * {@code onSuccess}. Error terminal event is propagated. *

* *

*
Scheduler:
*
{@code toCompletable} does not operate by default on a particular {@link Scheduler}.
*
* * @return a {@link Completable} that calls {@code onComplete} on it's subscriber when the source {@link Single} * calls {@code onSuccess}. * @see ReactiveX documentation: Completable * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Completable toCompletable() { return RxJavaObservablePlugins.onAssembly(new CompletableFromSingle(this)); } /** * Returns a {@link Future} representing the single value emitted by this {@code Single}. *

* *

*
Scheduler:
*
{@code toFuture} does not operate by default on a particular {@link Scheduler}.
*
* * @return a {@link Future} that expects a single item to be emitted by this {@code Single} * @see ReactiveX documentation: To */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Future toFuture() { return subscribeWith(new FutureSingleObserver()); } /** * Converts this Single into a {@link Maybe}. *

* *

*
Scheduler:
*
{@code toMaybe} does not operate by default on a particular {@link Scheduler}.
*
* * @return a {@link Maybe} that emits a single item T or an error. */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Maybe toMaybe() { if (this instanceof FuseToMaybe) { return ((FuseToMaybe)this).fuseToMaybe(); } return RxJavaObservablePlugins.onAssembly(new MaybeFromSingle(this)); } /** * Converts this Single into an {@link Observable}. *

* *

*
Scheduler:
*
{@code toObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @return an {@link Observable} that emits a single item T or an error. */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Observable toObservable() { if (this instanceof FuseToObservable) { return ((FuseToObservable)this).fuseToObservable(); } return RxJavaObservablePlugins.onAssembly(new SingleToObservable(this)); } /** * Returns a Single which makes sure when a SingleObserver disposes the Disposable, * that call is propagated up on the specified scheduler *
*
Scheduler:
*
{@code unsubscribeOn} calls dispose() of the upstream on the {@link Scheduler} you specify.
*
*

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

* *

*
Scheduler:
*
{@code zipWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items emitted by the {@code other} Single * @param * the type of items emitted by the resulting Single * @param other * the other SingleSource * @param zipper * a function that combines the pairs of items from the two SingleSources to generate the items to * be emitted by the resulting Single * @return a Single that pairs up values from the source Single and the {@code other} SingleSource * and emits the results of {@code zipFunction} applied to these pairs * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Single zipWith(SingleSource other, BiFunction zipper) { return zip(this, other, zipper); } // ------------------------------------------------------------------------- // Fluent test support, super handy and reduces test preparation boilerplate // ------------------------------------------------------------------------- /** * Creates a TestObserver and subscribes * it to this Single. *
*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @return the new TestObserver instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final TestObserver test() { TestObserver ts = new TestObserver(); subscribe(ts); return ts; } /** * Creates a TestObserver optionally in cancelled state, then subscribes it to this Single. *
*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @param cancelled if true, the TestObserver will be cancelled before subscribing to this * Single. * @return the new TestObserver instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final TestObserver test(boolean cancelled) { TestObserver ts = new TestObserver(); if (cancelled) { ts.cancel(); } subscribe(ts); return ts; } private static Single toSingle(Observable source) { return RxJavaObservablePlugins.onAssembly(new ObservableSingleSingle(source, null)); } }