Class Single<T>

    • Constructor Detail

      • Single

        public Single()
    • Method Detail

      • amb

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> amb​(@NonNull
                                                          @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Runs multiple SingleSources and signals the events of the first one that signals (disposing the rest).

        Scheduler:
        amb does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - the Iterable sequence of sources. A subscription to each source will occur in the same order as in this Iterable.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.0
      • ambArray

        @CheckReturnValue
        @SchedulerSupport("none")
        @SafeVarargs
        @NonNull
        public static <@NonNull T> @NonNull Single<T> ambArray​(@NonNull
                                                               @NonNull SingleSource<? extends @NonNull T>... sources)
        Runs multiple SingleSources and signals the events of the first one that signals (disposing the rest).

        Scheduler:
        ambArray does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - the array of sources. A subscription to each source will occur in the same order as in this array.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.0
      • concat

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concat​(@NonNull
                                                               @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenate the single values, in a non-overlapping fashion, of the SingleSources provided by a Publisher sequence.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer and the sources Publisher is expected to honor it as well.
        Scheduler:
        concat does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - the Publisher of SingleSource instances
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.0
      • concat

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concat​(@NonNull
                                                               @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources,
                                                               int prefetch)
        Concatenate the single values, in a non-overlapping fashion, of the SingleSources provided by a Publisher sequence and prefetched by the specified amount.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer and the sources Publisher is expected to honor it as well.
        Scheduler:
        concat does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - the Publisher of SingleSource instances
        prefetch - the number of SingleSources to prefetch from the Publisher
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if prefetch is non-positive
        Since:
        2.0
      • concatArrayEager

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        @SafeVarargs
        public static <@NonNull T> @NonNull Flowable<T> concatArrayEager​(@NonNull
                                                                         @NonNull SingleSource<? extends @NonNull T>... sources)
        Concatenates a sequence of SingleSource eagerly into a single stream of values.

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

        Backpressure:
        The operator honors backpressure from downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
      • concatArrayEagerDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        @SafeVarargs
        public static <@NonNull T> @NonNull Flowable<T> concatArrayEagerDelayError​(@NonNull
                                                                                   @NonNull SingleSource<? extends @NonNull T>... sources)
        Concatenates a sequence of SingleSource eagerly into a single stream of values.

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

        Backpressure:
        The operator honors backpressure from downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
      • concatDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatDelayError​(@NonNull
                                                                         @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates the Iterable sequence of SingleSources into a single sequence by subscribing to each SingleSource, one after the other, one at a time and delays any errors till the all inner SingleSources terminate as a Flowable sequence.

        Backpressure:
        The operator honors backpressure from downstream.
        Scheduler:
        concatDelayError does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common element base type
        Parameters:
        sources - the Iterable sequence of SingleSources
        Returns:
        the new Flowable with the concatenating behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
      • concatDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T> @NonNull Flowable<T> concatDelayError​(@NonNull
                                                                         @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates the Publisher sequence of SingleSources into a single sequence by subscribing to each inner SingleSource, one after the other, one at a time and delays any errors till the all inner and the outer Publisher terminate as a Flowable sequence.

        Backpressure:
        concatDelayError fully supports backpressure.
        Scheduler:
        concatDelayError does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common element base type
        Parameters:
        sources - the Publisher sequence of SingleSources
        Returns:
        the new Flowable with the concatenating behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
      • concatDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T> @NonNull Flowable<T> concatDelayError​(@NonNull
                                                                         @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources,
                                                                         int prefetch)
        Concatenates the Publisher sequence of SingleSources into a single sequence by subscribing to each inner SingleSource, one after the other, one at a time and delays any errors till the all inner and the outer Publisher terminate as a Flowable sequence.

        Backpressure:
        concatDelayError fully supports backpressure.
        Scheduler:
        concatDelayError does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common element base type
        Parameters:
        sources - the Publisher sequence of SingleSources
        prefetch - The number of upstream items to prefetch so that fresh items are ready to be mapped when a previous SingleSource terminates. The operator replenishes after half of the prefetch amount has been consumed and turned into SingleSources.
        Returns:
        the new Flowable with the concatenating behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if prefetch is non-positive
        Since:
        3.0.0
      • concatEager

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEager​(@NonNull
                                                                    @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates an Iterable sequence of SingleSources eagerly into a single stream of values.

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

        Backpressure:
        Backpressure is honored towards the downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - an Iterable sequence of SingleSource that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
      • concatEager

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEager​(@NonNull
                                                                    @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources,
                                                                    int maxConcurrency)
        Concatenates an Iterable sequence of SingleSources eagerly into a single stream of values and runs a limited number of the inner sources at once.

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

        Backpressure:
        Backpressure is honored towards the downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - an Iterable sequence of SingleSource that need to be eagerly concatenated
        maxConcurrency - the maximum number of concurrently running inner SingleSources; Integer.MAX_VALUE is interpreted as all inner SingleSources can be active at the same time
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if maxConcurrency is non-positive
        Since:
        3.0.0
      • concatEager

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEager​(@NonNull
                                                                    @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates a Publisher sequence of SingleSources eagerly into a single stream of values.

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

        Backpressure:
        Backpressure is honored towards the downstream and the outer Publisher is expected to support backpressure. Violating this assumption, the operator will signal MissingBackpressureException.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
      • concatEager

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEager​(@NonNull
                                                                    @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources,
                                                                    int maxConcurrency)
        Concatenates a Publisher sequence of SingleSources eagerly into a single stream of values and runs a limited number of those inner SingleSources at once.

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

        Backpressure:
        Backpressure is honored towards the downstream and the outer Publisher is expected to support backpressure. Violating this assumption, the operator will signal MissingBackpressureException.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        maxConcurrency - the maximum number of concurrently running inner SingleSources; Integer.MAX_VALUE is interpreted as all inner SingleSources can be active at the same time
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if maxConcurrency is non-positive
        Since:
        3.0.0
      • concatEagerDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEagerDelayError​(@NonNull
                                                                              @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates an Iterable sequence of SingleSources eagerly into a single stream of values, delaying errors until all the inner sources terminate.

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

        Backpressure:
        Backpressure is honored towards the downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - an Iterable sequence of SingleSource that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
      • concatEagerDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEagerDelayError​(@NonNull
                                                                              @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources,
                                                                              int maxConcurrency)
        Concatenates an Iterable sequence of SingleSources eagerly into a single stream of values, delaying errors until all the inner sources terminate.

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

        Backpressure:
        Backpressure is honored towards the downstream.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - an Iterable sequence of SingleSource that need to be eagerly concatenated
        maxConcurrency - the maximum number of concurrently running inner SingleSources; Integer.MAX_VALUE is interpreted as all inner SingleSources can be active at the same time
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if maxConcurrency is non-positive
        Since:
        3.0.0
      • concatEagerDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEagerDelayError​(@NonNull
                                                                              @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Concatenates a Publisher sequence of SingleSources eagerly into a single stream of values, delaying errors until all the inner and the outer sequence terminate.

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

        Backpressure:
        Backpressure is honored towards the downstream and the outer Publisher is expected to support backpressure. Violating this assumption, the operator will signal MissingBackpressureException.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
      • concatEagerDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> concatEagerDelayError​(@NonNull
                                                                              @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources,
                                                                              int maxConcurrency)
        Concatenates a Publisher sequence of SingleSources eagerly into a single stream of values, running at most the specified number of those inner SingleSources at once and delaying errors until all the inner and the outer sequence terminate.

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

        Backpressure:
        Backpressure is honored towards the downstream and the outer Publisher is expected to support backpressure. Violating this assumption, the operator will signal MissingBackpressureException.
        Scheduler:
        This method does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        sources - a sequence of SingleSources that need to be eagerly concatenated
        maxConcurrency - the number of inner SingleSources to run at once
        Returns:
        the new Flowable instance with the specified concatenation behavior
        Throws:
        java.lang.NullPointerException - if sources is null
        java.lang.IllegalArgumentException - if maxConcurrency is non-positive
        Since:
        3.0.0
      • create

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> create​(@NonNull
                                                             @NonNull SingleOnSubscribe<@NonNull T> source)
        Provides an API (via a cold Single) that bridges the reactive world with the callback-style world.

        Example:

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

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

        Scheduler:
        create does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        source - the emitter that is called when a SingleObserver subscribes to the returned Single
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if source is null
        See Also:
        SingleOnSubscribe, Cancellable
      • fromCallable

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromCallable​(@NonNull
                                                                   @NonNull java.util.concurrent.Callable<? extends @NonNull T> callable)
        Returns a Single that invokes the given Callable for each incoming SingleObserver and emits its value or exception to them.

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

        Scheduler:
        fromCallable does not operate by default on a particular Scheduler.
        Error handling:
        If the Callable throws an exception, the respective Throwable is delivered to the downstream via SingleObserver.onError(Throwable), except when the downstream has disposed this Single source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
        Type Parameters:
        T - the type of the item emitted by the Single.
        Parameters:
        callable - function which execution should be deferred, it will be invoked when SingleObserver will subscribe to the Single.
        Returns:
        the new Single whose SingleObservers' subscriptions trigger an invocation of the given function.
        Throws:
        java.lang.NullPointerException - if callable is null
        See Also:
        defer(Supplier), fromSupplier(Supplier)
      • fromFuture

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T> @NonNull Single<T> fromFuture​(@NonNull
                                                                 @NonNull java.util.concurrent.Future<? extends @NonNull T> future)
        Converts a Future into a Single and awaits its outcome in a blocking fashion.

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

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

        Scheduler:
        fromFuture does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the type of object that the Future returns, and also the type of item to be emitted by the resulting Single
        Parameters:
        future - the source Future
        Returns:
        the new Single that emits the item from the source Future
        Throws:
        java.lang.NullPointerException - if future is null
        See Also:
        ReactiveX operators documentation: From, fromFuture(Future, long, TimeUnit), fromCompletionStage(CompletionStage)
      • fromFuture

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T> @NonNull Single<T> fromFuture​(@NonNull
                                                                 @NonNull java.util.concurrent.Future<? extends @NonNull T> future,
                                                                 long timeout,
                                                                 @NonNull
                                                                 @NonNull java.util.concurrent.TimeUnit unit)
        Converts a Future into a Single and awaits its outcome, or timeout, in a blocking fashion.

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

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

        Scheduler:
        fromFuture does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the type of object that the Future returns, and also the type of item to be emitted by the resulting Single
        Parameters:
        future - the source Future
        timeout - the maximum time to wait before calling get
        unit - the TimeUnit of the timeout argument
        Returns:
        the new Single that emits the item from the source Future
        Throws:
        java.lang.NullPointerException - if future or unit is null
        See Also:
        ReactiveX operators documentation: From
      • fromMaybe

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromMaybe​(@NonNull
                                                                @NonNull MaybeSource<@NonNull T> maybe)
        Returns a Single instance that when subscribed to, subscribes to the MaybeSource instance and emits onSuccess as a single item, turns an onComplete into NoSuchElementException error signal or forwards the onError signal.

        Scheduler:
        fromMaybe does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type of the MaybeSource element
        Parameters:
        maybe - the MaybeSource instance to subscribe to, not null
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if maybe is null
        Since:
        3.0.0
      • fromMaybe

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromMaybe​(@NonNull
                                                                @NonNull MaybeSource<@NonNull T> maybe,
                                                                @NonNull
                                                                @NonNull T defaultItem)
        Returns a Single instance that when subscribed to, subscribes to the MaybeSource instance and emits onSuccess as a single item, emits the defaultItem for an onComplete signal or forwards the onError signal.

        Scheduler:
        fromMaybe does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type of the MaybeSource element
        Parameters:
        maybe - the MaybeSource instance to subscribe to, not null
        defaultItem - the item to signal if the current MaybeSource is empty
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if maybe or defaultItem is null
        Since:
        3.0.0
      • fromPublisher

        @BackpressureSupport(UNBOUNDED_IN)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromPublisher​(@NonNull
                                                                    @NonNull org.reactivestreams.Publisher<? extends @NonNull T> publisher)
        Wraps a specific Publisher into a Single and signals its single element or error.

        If the source Publisher is empty, a NoSuchElementException is signaled. If the source has more than one element, an IndexOutOfBoundsException is signaled.

        The Publisher must follow the Reactive Streams specification. Violating the specification may result in undefined behavior.

        If possible, use create(SingleOnSubscribe) to create a source-like Single instead.

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

        Backpressure:
        The publisher is consumed in an unbounded fashion but will be cancelled if it produced more than one item.
        Scheduler:
        fromPublisher does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        publisher - the source Publisher instance, not null
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if publisher is null
        See Also:
        create(SingleOnSubscribe)
      • fromObservable

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromObservable​(@NonNull
                                                                     @NonNull ObservableSource<? extends @NonNull T> observable)
        Wraps a specific ObservableSource into a Single and signals its single element or error.

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

        Scheduler:
        fromObservable does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the type of the item emitted by the Single.
        Parameters:
        observable - the source sequence to wrap, not null
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if observable is null
      • fromSupplier

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> fromSupplier​(@NonNull
                                                                   @NonNull Supplier<? extends @NonNull T> supplier)
        Returns a Single that invokes passed supplier and emits its result for each individual SingleObserver that subscribes.

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

        Scheduler:
        fromSupplier does not operate by default on a particular Scheduler.
        Error handling:
        If the Supplier throws an exception, the respective Throwable is delivered to the downstream via SingleObserver.onError(Throwable), except when the downstream has disposed this Single source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
        Type Parameters:
        T - the type of the item emitted by the Single.
        Parameters:
        supplier - function which execution should be deferred, it will be invoked when SingleObserver subscribes to the Single.
        Returns:
        the new Single whose SingleObservers' subscriptions trigger an invocation of the given function.
        Throws:
        java.lang.NullPointerException - if supplier is null
        Since:
        3.0.0
        See Also:
        defer(Supplier), fromCallable(Callable)
      • merge

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> merge​(@NonNull
                                                              @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Merges an Iterable sequence of SingleSource instances into a single Flowable sequence, running all SingleSources at once.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        merge does not operate by default on a particular Scheduler.
        Error handling:
        If any of the source SingleSources signal a Throwable via onError, the resulting Flowable terminates with that Throwable and all other source SingleSources are disposed. If more than one SingleSource signals an error, the resulting Flowable may terminate with the first one's error or, depending on the concurrency of the sources, may terminate with a CompositeException containing two or more of the various error signals. Throwables that didn't make into the composite will be sent (individually) to the global error handler via RxJavaPlugins.onError(Throwable) method as UndeliverableException errors. Similarly, Throwables signaled by source(s) after the returned Flowable has been cancelled or terminated with a (composite) error will be sent to the same global error handler. Use mergeDelayError(Iterable) to merge sources and terminate only when all source SingleSources have completed or failed with an error.
        Type Parameters:
        T - the common and resulting value type
        Parameters:
        sources - the Iterable sequence of SingleSource sources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.0
        See Also:
        mergeDelayError(Iterable)
      • merge

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> merge​(@NonNull
                                                              @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Merges a sequence of SingleSource instances emitted by a Publisher into a single Flowable sequence, running all SingleSources at once.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        merge does not operate by default on a particular Scheduler.
        Error handling:
        If any of the source SingleSources signal a Throwable via onError, the resulting Flowable terminates with that Throwable and all other source SingleSources are disposed. If more than one SingleSource signals an error, the resulting Flowable may terminate with the first one's error or, depending on the concurrency of the sources, may terminate with a CompositeException containing two or more of the various error signals. Throwables that didn't make into the composite will be sent (individually) to the global error handler via RxJavaPlugins.onError(Throwable) method as UndeliverableException errors. Similarly, Throwables signaled by source(s) after the returned Flowable has been cancelled or terminated with a (composite) error will be sent to the same global error handler. Use mergeDelayError(Publisher) to merge sources and terminate only when all source SingleSources have completed or failed with an error.
        Type Parameters:
        T - the common and resulting value type
        Parameters:
        sources - the Publisher emitting a sequence of SingleSources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.0
        See Also:
        mergeDelayError(Publisher)
      • merge

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> merge​(@NonNull
                                                            @NonNull SingleSource<? extends SingleSource<? extends @NonNull T>> source)
        Flattens a SingleSource that emits a SingleSingle into a single Single that emits the item emitted by the nested SingleSource, without any transformation.

        Scheduler:
        merge does not operate by default on a particular Scheduler.
        The resulting Single emits the outer source's or the inner SingleSource's Throwable as is. Unlike the other merge() operators, this operator won't and can't produce a CompositeException because there is only one possibility for the outer or the inner SingleSource to emit an onError signal. Therefore, there is no need for a mergeDelayError(SingleSource<SingleSource<T>>) operator.
        Type Parameters:
        T - the value type of the sources and the output
        Parameters:
        source - a Single that emits a Single
        Returns:
        the new Single that emits the item that is the result of flattening the Single emitted by source
        Throws:
        java.lang.NullPointerException - if source is null
        See Also:
        ReactiveX operators documentation: Merge
      • merge

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> merge​(@NonNull
                                                              @NonNull SingleSource<? extends @NonNull T> source1,
                                                              @NonNull
                                                              @NonNull SingleSource<? extends @NonNull T> source2)
        Flattens two SingleSources into one Flowable sequence, without any transformation.

        You can combine items emitted by multiple SingleSources so that they appear as a single Flowable, by using the merge method.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        merge does not operate by default on a particular Scheduler.
        Error handling:
        If any of the source SingleSources signal a Throwable via onError, the resulting Flowable terminates with that Throwable and all other source SingleSources are disposed. If more than one SingleSource signals an error, the resulting Flowable may terminate with the first one's error or, depending on the concurrency of the sources, may terminate with a CompositeException containing two or more of the various error signals. Throwables that didn't make into the composite will be sent (individually) to the global error handler via RxJavaPlugins.onError(Throwable) method as UndeliverableException errors. Similarly, Throwables signaled by source(s) after the returned Flowable has been cancelled or terminated with a (composite) error will be sent to the same global error handler. Use mergeDelayError(SingleSource, SingleSource) to merge sources and terminate only when all source SingleSources have completed or failed with an error.
        Type Parameters:
        T - the common value type
        Parameters:
        source1 - a SingleSource to be merged
        source2 - a SingleSource to be merged
        Returns:
        the new Flowable that emits all of the items emitted by the source SingleSources
        Throws:
        java.lang.NullPointerException - if source1 or source2 is null
        See Also:
        ReactiveX operators documentation: Merge, mergeDelayError(SingleSource, SingleSource)
      • mergeArray

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        @SafeVarargs
        public static <@NonNull T> @NonNull Flowable<T> mergeArray​(SingleSource<? extends @NonNull T>... sources)
        Merges an array of SingleSource instances into a single Flowable sequence, running all SingleSources at once.

        Backpressure:
        The operator honors backpressure from downstream.
        Scheduler:
        mergeArray does not operate by default on a particular Scheduler.
        Error handling:
        If any of the source SingleSources signal a Throwable via onError, the resulting Flowable terminates with that Throwable and all other source SingleSources are disposed. If more than one SingleSource signals an error, the resulting Flowable may terminate with the first one's error or, depending on the concurrency of the sources, may terminate with a CompositeException containing two or more of the various error signals. Throwables that didn't make into the composite will be sent (individually) to the global error handler via RxJavaPlugins.onError(Throwable) method as UndeliverableException errors. Similarly, Throwables signaled by source(s) after the returned Flowable has been cancelled or terminated with a (composite) error will be sent to the same global error handler. Use mergeArrayDelayError(SingleSource...) to merge sources and terminate only when all source SingleSources have completed or failed with an error.
        Type Parameters:
        T - the common and resulting value type
        Parameters:
        sources - the array sequence of SingleSource sources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        See Also:
        mergeArrayDelayError(SingleSource...)
      • mergeArrayDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @SafeVarargs
        @NonNull
        public static <@NonNull T> @NonNull Flowable<T> mergeArrayDelayError​(@NonNull
                                                                             @NonNull SingleSource<? extends @NonNull T>... sources)
        Flattens an array of SingleSources into one Flowable, in a way that allows a subscriber to receive all successfully emitted items from each of the source SingleSources without being interrupted by an error notification from one of them.

        This behaves like merge(Publisher) except that if any of the merged SingleSources notify of an error via onError, mergeArrayDelayError will refrain from propagating that error notification until all of the merged SingleSources have finished emitting items.

        Even if multiple merged SingleSources send onError notifications, mergeArrayDelayError will only invoke the onError method of its subscribers once.

        Backpressure:
        The operator honors backpressure from downstream.
        Scheduler:
        mergeArrayDelayError does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common element base type
        Parameters:
        sources - the array of SingleSources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        See Also:
        ReactiveX operators documentation: Merge
      • mergeDelayError

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> mergeDelayError​(@NonNull
                                                                        @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources)
        Merges an Iterable sequence of SingleSource instances into one Flowable sequence, running all SingleSources at once and delaying any error(s) until all sources succeed or fail.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        mergeDelayError does not operate by default on a particular Scheduler.

        History: 2.1.9 - experimental

        Type Parameters:
        T - the common and resulting value type
        Parameters:
        sources - the Iterable sequence of SingleSources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.2
        See Also:
        merge(Iterable)
      • mergeDelayError

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> mergeDelayError​(@NonNull
                                                                        @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Merges a sequence of SingleSource instances emitted by a Publisher into a Flowable sequence, running all SingleSources at once and delaying any error(s) until all sources succeed or fail.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        mergeDelayError does not operate by default on a particular Scheduler.

        History: 2.1.9 - experimental

        Type Parameters:
        T - the common and resulting value type
        Parameters:
        sources - the Flowable sequence of SingleSources
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        2.2
        See Also:
        merge(Publisher)
      • timer

        @CheckReturnValue
        @SchedulerSupport("io.reactivex:computation")
        @NonNull
        public static @NonNull Single<java.lang.Long> timer​(long delay,
                                                            @NonNull
                                                            @NonNull java.util.concurrent.TimeUnit unit)
        Signals success with 0L value after the given delay when a SingleObserver subscribes.

        Scheduler:
        timer operates by default on the computation Scheduler.
        Parameters:
        delay - the delay amount
        unit - the time unit of the delay
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        2.0
      • timer

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public static @NonNull Single<java.lang.Long> timer​(long delay,
                                                            @NonNull
                                                            @NonNull java.util.concurrent.TimeUnit unit,
                                                            @NonNull
                                                            @NonNull Scheduler scheduler)
        Signals success with 0L value on the specified Scheduler after the given delay when a SingleObserver subscribes.

        Scheduler:
        you specify the Scheduler to signal on.
        Parameters:
        delay - the delay amount
        unit - the time unit of the delay
        scheduler - the Scheduler where the single 0L will be emitted
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null, or if scheduler is null
        Since:
        2.0
      • switchOnNextDelayError

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Flowable<T> switchOnNextDelayError​(@NonNull
                                                                               @NonNull org.reactivestreams.Publisher<? extends SingleSource<? extends @NonNull T>> sources)
        Switches between SingleSources emitted by the source Publisher whenever a new SingleSource is emitted, disposing the previously running SingleSource, exposing the success items as a Flowable sequence and delaying all errors from all of them until all terminate.

        Backpressure:
        The sources Publisher is consumed in an unbounded manner (requesting Long.MAX_VALUE). The returned Flowable respects the backpressure from the downstream.
        Scheduler:
        switchOnNextDelayError does not operate by default on a particular Scheduler.
        Error handling:
        The returned Flowable collects all errors emitted by either the sources Publisher or any inner SingleSource and emits them as a CompositeException when all sources terminate. If only one source ever failed, its error is emitted as-is at the end.
        Type Parameters:
        T - the element type of the SingleSources
        Parameters:
        sources - the Publisher sequence of inner SingleSources to switch between
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if sources is null
        Since:
        3.0.0
        See Also:
        switchOnNext(Publisher), ReactiveX operators documentation: Switch
      • unsafeCreate

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T> @NonNull Single<T> unsafeCreate​(@NonNull
                                                                   @NonNull SingleSource<@NonNull T> onSubscribe)
        Advanced use only: creates a Single instance without any safeguards by using a callback that is called with a SingleObserver.

        Scheduler:
        unsafeCreate does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type
        Parameters:
        onSubscribe - the function that is called with the subscribing SingleObserver
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onSubscribe is null
        java.lang.IllegalArgumentException - if source is a subclass of Single; such instances don't need conversion and is possibly a port remnant from 1.x or one should use hide() instead.
        Since:
        2.0
      • using

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T,​@NonNull U> @NonNull Single<T> using​(@NonNull
                                                                             @NonNull Supplier<@NonNull U> resourceSupplier,
                                                                             @NonNull
                                                                             @NonNull Function<? super @NonNull U,​? extends SingleSource<? extends @NonNull T>> sourceSupplier,
                                                                             @NonNull
                                                                             @NonNull Consumer<? super @NonNull U> resourceCleanup)
        Allows using and disposing a resource while running a SingleSource instance generated from that resource (similar to a try-with-resources).

        Scheduler:
        using does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type of the SingleSource generated
        U - the resource type
        Parameters:
        resourceSupplier - the Supplier called for each SingleObserver to generate a resource object
        sourceSupplier - the function called with the returned resource object from resourceSupplier and should return a SingleSource instance to be run by the operator
        resourceCleanup - 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 disposed.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if resourceSupplier, sourceSupplier and resourceCleanup is null
        Since:
        2.0
      • using

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T,​@NonNull U> @NonNull Single<T> using​(@NonNull
                                                                             @NonNull Supplier<@NonNull U> resourceSupplier,
                                                                             @NonNull
                                                                             @NonNull Function<? super @NonNull U,​? extends SingleSource<? extends @NonNull T>> sourceSupplier,
                                                                             @NonNull
                                                                             @NonNull Consumer<? super @NonNull U> resourceCleanup,
                                                                             boolean eager)
        Allows using and disposing a resource while running a SingleSource instance generated from that resource (similar to a try-with-resources).

        Scheduler:
        using does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the value type of the SingleSource generated
        U - the resource type
        Parameters:
        resourceSupplier - the Supplier called for each SingleObserver to generate a resource object
        sourceSupplier - the function called with the returned resource object from resourceSupplier and should return a SingleSource instance to be run by the operator
        resourceCleanup - 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 disposed.
        eager - If true then resource disposal will happen either on a dispose() call before the upstream is disposed or just before the emission of a terminal event (onSuccess or onError). If false the resource disposal will happen either on a dispose() call after the upstream is disposed or just after the emission of a terminal event (onSuccess or onError).
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if resourceSupplier, sourceSupplier or resourceCleanup is null
        Since:
        2.0
      • zip

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public static <@NonNull T,​@NonNull R> @NonNull Single<R> zip​(@NonNull
                                                                           @NonNull java.lang.Iterable<? extends SingleSource<? extends @NonNull T>> sources,
                                                                           @NonNull
                                                                           @NonNull Function<? super java.lang.Object[],​? extends @NonNull R> zipper)
        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 the downstream.

        If the Iterable of SingleSources is empty a NoSuchElementException error is signaled after subscription.

        Note on method signature: since Java doesn't allow creating a generic array with new T[], the implementation of this operator has to create an Object[] instead. Unfortunately, a Function<Integer[], R> passed to the method would trigger a ClassCastException.

        If any of the SingleSources signal an error, all other SingleSources get disposed and the error emitted to downstream immediately.

        Scheduler:
        zip does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common value type
        R - the result value type
        Parameters:
        sources - the Iterable sequence of SingleSource instances. An empty sequence will result in an onError signal of NoSuchElementException.
        zipper - the function that receives an array with values from each SingleSource and should return a value to be emitted to downstream
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if zipper or sources is null
        Since:
        2.0
      • zipArray

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        @SafeVarargs
        public static <@NonNull T,​@NonNull R> @NonNull Single<R> zipArray​(@NonNull
                                                                                @NonNull Function<? super java.lang.Object[],​? extends @NonNull R> zipper,
                                                                                @NonNull
                                                                                @NonNull SingleSource<? extends @NonNull T>... sources)
        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.

        If the array of SingleSources is empty a NoSuchElementException error is signaled immediately.

        Note on method signature: since Java doesn't allow creating a generic array with new T[], the implementation of this operator has to create an Object[] instead. Unfortunately, a Function<Integer[], R> passed to the method would trigger a ClassCastException.

        If any of the SingleSources signal an error, all other SingleSources get disposed and the error emitted to downstream immediately.

        Scheduler:
        zipArray does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the common value type
        R - the result value type
        Parameters:
        sources - the array of SingleSource instances. An empty sequence will result in an onError signal of NoSuchElementException.
        zipper - the function that receives an array with values from each SingleSource and should return a value to be emitted to downstream
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if zipper or sources is null
        Since:
        2.0
      • ambWith

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> ambWith​(@NonNull
                                                @NonNull SingleSource<? extends @NonNull T> other)
        Signals the event of this or the other SingleSource whichever signals first.

        Scheduler:
        ambWith does not operate by default on a particular Scheduler.
        Parameters:
        other - the other SingleSource to race for the first emission of success or error
        Returns:
        the new Single instance. A subscription to this provided source will occur after subscribing to the current source.
        Throws:
        java.lang.NullPointerException - if other is null
        Since:
        2.0
      • cache

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> cache()
        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:
        cache does not operate by default on a particular Scheduler.
        Returns:
        the new Single instance
        Since:
        2.0
      • cast

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull U> @NonNull Single<U> cast​(@NonNull
                                                          @NonNull java.lang.Class<? extends @NonNull U> clazz)
        Casts the success value of the current Single into the target type or signals a ClassCastException if not compatible.

        Scheduler:
        cast does not operate by default on a particular Scheduler.
        Type Parameters:
        U - the target type
        Parameters:
        clazz - the type token to use for casting the success result from the current Single
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if clazz is null
        Since:
        2.0
      • delay

        @CheckReturnValue
        @SchedulerSupport("io.reactivex:computation")
        @NonNull
        public final @NonNull Single<T> delay​(long time,
                                              @NonNull
                                              @NonNull java.util.concurrent.TimeUnit unit)
        Delays the emission of the success signal from the current Single by the specified amount. An error signal will not be delayed.

        Scheduler:
        delay operates by default on the computation Scheduler.
        Parameters:
        time - the amount of time the success signal should be delayed for
        unit - the time unit
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        2.0
        See Also:
        delay(long, TimeUnit, boolean)
      • delay

        @CheckReturnValue
        @SchedulerSupport("io.reactivex:computation")
        @NonNull
        public final @NonNull Single<T> delay​(long time,
                                              @NonNull
                                              @NonNull java.util.concurrent.TimeUnit unit,
                                              boolean delayError)
        Delays the emission of the success or error signal from the current Single by the specified amount.

        Scheduler:
        delay operates by default on the computation Scheduler.

        History: 2.1.5 - experimental

        Parameters:
        time - the amount of time the success or error signal should be delayed for
        unit - the time unit
        delayError - if true, both success and error signals are delayed. if false, only success signals are delayed.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        2.2
      • delay

        @CheckReturnValue
        @SchedulerSupport("custom")
        @NonNull
        public final @NonNull Single<T> delay​(long time,
                                              @NonNull
                                              @NonNull java.util.concurrent.TimeUnit unit,
                                              @NonNull
                                              @NonNull Scheduler scheduler)
        Delays the emission of the success signal from the current Single by the specified amount. An error signal will not be delayed.

        Scheduler:
        you specify the Scheduler where the non-blocking wait and emission happens
        Parameters:
        time - the amount of time the success signal should be delayed for
        unit - the time unit
        scheduler - the target scheduler to use for the non-blocking wait and emission
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null, or if scheduler is null
        Since:
        2.0
        See Also:
        delay(long, TimeUnit, Scheduler, boolean)
      • delay

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<T> delay​(long time,
                                              @NonNull
                                              @NonNull java.util.concurrent.TimeUnit unit,
                                              @NonNull
                                              @NonNull Scheduler scheduler,
                                              boolean delayError)
        Delays the emission of the success or error signal from the current Single by the specified amount.

        Scheduler:
        you specify the Scheduler where the non-blocking wait and emission happens

        History: 2.1.5 - experimental

        Parameters:
        time - the amount of time the success or error signal should be delayed for
        unit - the time unit
        scheduler - the target scheduler to use for the non-blocking wait and emission
        delayError - if true, both success and error signals are delayed. if false, only success signals are delayed.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null, or if scheduler is null
        Since:
        2.2
      • delaySubscription

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> delaySubscription​(@NonNull
                                                          @NonNull CompletableSource subscriptionIndicator)
        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:
        delaySubscription does not operate by default on a particular Scheduler.
        Parameters:
        subscriptionIndicator - the CompletableSource that has to complete before the subscription to the current Single happens
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if subscriptionIndicator is null
        Since:
        2.0
      • delaySubscription

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull U> @NonNull Single<T> delaySubscription​(@NonNull
                                                                       @NonNull SingleSource<@NonNull U> subscriptionIndicator)
        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:
        delaySubscription does not operate by default on a particular Scheduler.
        Type Parameters:
        U - the element type of the other source
        Parameters:
        subscriptionIndicator - the SingleSource that has to complete before the subscription to the current Single happens
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if subscriptionIndicator is null
        Since:
        2.0
      • delaySubscription

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull U> @NonNull Single<T> delaySubscription​(@NonNull
                                                                       @NonNull ObservableSource<@NonNull U> subscriptionIndicator)
        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:
        delaySubscription does not operate by default on a particular Scheduler.
        Type Parameters:
        U - the element type of the other source
        Parameters:
        subscriptionIndicator - the ObservableSource that has to signal a value or complete before the subscription to the current Single happens
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if subscriptionIndicator is null
        Since:
        2.0
      • delaySubscription

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull U> @NonNull Single<T> delaySubscription​(@NonNull
                                                                       @NonNull org.reactivestreams.Publisher<@NonNull U> subscriptionIndicator)
        Delays the actual subscription to the current Single until the given other Publisher signals its first value or completes.

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

        The other source is consumed in an unbounded manner (requesting Long.MAX_VALUE from it).

        Backpressure:
        The other publisher is consumed in an unbounded fashion but will be cancelled after the first item it produced.
        Scheduler:
        delaySubscription does not operate by default on a particular Scheduler.
        Type Parameters:
        U - the element type of the other source
        Parameters:
        subscriptionIndicator - the Publisher that has to signal a value or complete before the subscription to the current Single happens
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if subscriptionIndicator is null
        Since:
        2.0
      • delaySubscription

        @CheckReturnValue
        @SchedulerSupport("io.reactivex:computation")
        @NonNull
        public final @NonNull Single<T> delaySubscription​(long time,
                                                          @NonNull
                                                          @NonNull java.util.concurrent.TimeUnit unit)
        Delays the actual subscription to the current Single until the given time delay elapsed.

        Scheduler:
        delaySubscription does by default subscribe to the current Single on the computation Scheduler after the delay.
        Parameters:
        time - the time amount to wait with the subscription
        unit - the time unit of the waiting
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        2.0
      • delaySubscription

        @CheckReturnValue
        @SchedulerSupport("custom")
        @NonNull
        public final @NonNull Single<T> delaySubscription​(long time,
                                                          @NonNull
                                                          @NonNull java.util.concurrent.TimeUnit unit,
                                                          @NonNull
                                                          @NonNull Scheduler scheduler)
        Delays the actual subscription to the current Single until the given time delay elapsed.

        Scheduler:
        delaySubscription does by default subscribe to the current Single on the Scheduler you provided, after the delay.
        Parameters:
        time - the time amount to wait with the subscription
        unit - the time unit of the waiting
        scheduler - the Scheduler to wait on and subscribe on to the current Single
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit or scheduler is null
        Since:
        2.0
      • dematerialize

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull R> @NonNull Maybe<R> dematerialize​(@NonNull
                                                                  @NonNull Function<? super @NonNull T,​@NonNull Notification<@NonNull R>> selector)
        Maps the Notification success value of the current Single back into normal onSuccess, onError or onComplete signals as a Maybe source.

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

        Scheduler:
        dematerialize does not operate by default on a particular Scheduler.

        Example:

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

        History: 2.2.4 - experimental

        Type Parameters:
        R - the result type
        Parameters:
        selector - the function called with the success item and should return a Notification instance.
        Returns:
        the new Maybe instance
        Throws:
        java.lang.NullPointerException - if selector is null
        Since:
        3.0.0
        See Also:
        materialize()
      • doAfterSuccess

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doAfterSuccess​(@NonNull
                                                       @NonNull Consumer<? super @NonNull T> onAfterSuccess)
        Calls the specified consumer with the success item after this item has been emitted to the downstream.

        Note that the doAfterSuccess action is shared between subscriptions and as such should be thread-safe.

        Scheduler:
        doAfterSuccess does not operate by default on a particular Scheduler.

        History: 2.0.1 - experimental

        Parameters:
        onAfterSuccess - the Consumer that will be called after emitting an item from upstream to the downstream
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onAfterSuccess is null
        Since:
        2.1
      • doAfterTerminate

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doAfterTerminate​(@NonNull
                                                         @NonNull Action onAfterTerminate)
        Registers an Action to be called after this Single invokes either onSuccess or onError.

        Note that the doAfterTerminate action is shared between subscriptions and as such should be thread-safe.

        Scheduler:
        doAfterTerminate does not operate by default on a particular Scheduler.

        History: 2.0.6 - experimental

        Parameters:
        onAfterTerminate - an Action to be invoked when the current Single finishes
        Returns:
        the new Single that emits the same items as the current Single, then invokes the Action
        Throws:
        java.lang.NullPointerException - if onAfterTerminate is null
        Since:
        2.1
        See Also:
        ReactiveX operators documentation: Do
      • doFinally

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doFinally​(@NonNull
                                                  @NonNull Action onFinally)
        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 onFinally action is executed once per subscription.

        Note that the onFinally action is shared between subscriptions and as such should be thread-safe.

        Scheduler:
        doFinally does not operate by default on a particular Scheduler.

        History: 2.0.1 - experimental

        Parameters:
        onFinally - the action called when this Single terminates or gets disposed
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onFinally is null
        Since:
        2.1
      • doOnTerminate

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doOnTerminate​(@NonNull
                                                      @NonNull Action onTerminate)
        Returns a Single instance that calls the given onTerminate callback just before this Single completes normally or with an exception.

        This differs from doAfterTerminate in that this happens before the onSuccess or onError notification.

        Scheduler:
        doOnTerminate does not operate by default on a particular Scheduler.

        History: 2.2.7 - experimental

        Parameters:
        onTerminate - the action to invoke when the consumer calls onSuccess or onError
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onTerminate is null
        Since:
        3.0.0
        See Also:
        ReactiveX operators documentation: Do, doOnTerminate(Action)
      • doOnSuccess

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doOnSuccess​(@NonNull
                                                    @NonNull Consumer<? super @NonNull T> onSuccess)
        Calls the shared consumer with the success value sent via onSuccess for each SingleObserver that subscribes to the current Single.

        Scheduler:
        doOnSuccess does not operate by default on a particular Scheduler.
        Parameters:
        onSuccess - the consumer called with the success value of onSuccess
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onSuccess is null
        Since:
        2.0
      • doOnEvent

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doOnEvent​(@NonNull
                                                  @NonNull BiConsumer<? super @NonNull T,​? super java.lang.Throwable> onEvent)
        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:
        doOnEvent does not operate by default on a particular Scheduler.
        Parameters:
        onEvent - the consumer called with the success value of onEvent
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onEvent is null
        Since:
        2.0
      • doOnError

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> doOnError​(@NonNull
                                                  @NonNull Consumer<? super java.lang.Throwable> onError)
        Calls the shared consumer with the error sent via onError for each SingleObserver that subscribes to the current Single.

        Scheduler:
        doOnError does not operate by default on a particular Scheduler.
        Parameters:
        onError - the consumer called with the success value of onError
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if onError is null
        Since:
        2.0
      • flatMapPublisher

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull R> @NonNull Flowable<R> flatMapPublisher​(@NonNull
                                                                        @NonNull Function<? super @NonNull T,​? extends org.reactivestreams.Publisher<? extends @NonNull R>> mapper)
        Returns a Flowable that emits items based on applying a specified function to the item emitted by the current Single, where that function returns a Publisher.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer and the Publisher returned by the mapper function is expected to honor it as well.
        Scheduler:
        flatMapPublisher does not operate by default on a particular Scheduler.
        Type Parameters:
        R - the result value type
        Parameters:
        mapper - a function that, when applied to the item emitted by the current Single, returns a Publisher
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if mapper is null
        See Also:
        ReactiveX operators documentation: FlatMap
      • blockingGet

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final T blockingGet()
        Waits in a blocking fashion until the current Single signals a success value (which is returned) or an exception (which is propagated).

        Scheduler:
        blockingGet does not operate by default on a particular Scheduler.
        Error handling:
        If the source signals an error, the operator wraps a checked Exception into RuntimeException and throws that. Otherwise, RuntimeExceptions and Errors are rethrown as they are.
        Returns:
        the success value
      • blockingSubscribe

        @SchedulerSupport("none")
        public final void blockingSubscribe​(@NonNull
                                            @NonNull Consumer<? super @NonNull T> onSuccess)
        Subscribes to the current Single and calls given onSuccess callback on the current thread when it completes normally.

        Scheduler:
        blockingSubscribe does not operate by default on a particular Scheduler.
        Error handling:
        If either the current Single signals an error or onSuccess throws, the respective Throwable is routed to the global error handler via RxJavaPlugins.onError(Throwable). If the current thread is interrupted, an InterruptedException is routed to the same global error handler.
        Parameters:
        onSuccess - the Consumer to call if the current Single succeeds
        Throws:
        java.lang.NullPointerException - if onSuccess is null
        Since:
        3.0.0
        See Also:
        blockingSubscribe(Consumer, Consumer)
      • blockingSubscribe

        @SchedulerSupport("none")
        public final void blockingSubscribe​(@NonNull
                                            @NonNull Consumer<? super @NonNull T> onSuccess,
                                            @NonNull
                                            @NonNull Consumer<? super java.lang.Throwable> onError)
        Subscribes to the current Single and calls the appropriate callback on the current thread when it terminates.

        Scheduler:
        blockingSubscribe does not operate by default on a particular Scheduler.
        Error handling:
        If either onSuccess or onError throw, the Throwable is routed to the global error handler via RxJavaPlugins.onError(Throwable). If the current thread is interrupted, the onError consumer is called with an InterruptedException.
        Parameters:
        onSuccess - the Consumer to call if the current Single succeeds
        onError - the Consumer to call if the current Single signals an error
        Throws:
        java.lang.NullPointerException - if onSuccess or onError is null
        Since:
        3.0.0
      • blockingSubscribe

        @SchedulerSupport("none")
        public final void blockingSubscribe​(@NonNull
                                            @NonNull SingleObserver<? super @NonNull T> observer)
        Subscribes to the current Single and calls the appropriate SingleObserver method on the current thread.

        Scheduler:
        blockingSubscribe does not operate by default on a particular Scheduler.
        Error handling:
        An onError signal is delivered to the SingleObserver.onError(Throwable) method. If any of the SingleObserver's methods throw, the RuntimeException is propagated to the caller of this method. If the current thread is interrupted, an InterruptedException is delivered to observer.onError.
        Parameters:
        observer - the SingleObserver to call methods on the current thread
        Throws:
        java.lang.NullPointerException - if observer is null
        Since:
        3.0.0
      • lift

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull R> @NonNull Single<R> lift​(@NonNull
                                                          @NonNull SingleOperator<? extends @NonNull R,​? super @NonNull T> lift)
        This method requires advanced knowledge about building operators, please consider other standard composition methods first; Returns a Single which, when subscribed to, invokes the apply(SingleObserver) method of the provided SingleOperator for each individual downstream Single and allows the insertion of a custom operator by accessing the downstream's SingleObserver during this subscription phase and providing a new SingleObserver, containing the custom operator's intended business logic, that will be used in the subscription process going further upstream.

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

        Example:

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

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

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

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

        Scheduler:
        lift does not operate by default on a particular Scheduler, however, the SingleOperator may use a Scheduler to support its own asynchronous behavior.
        Type Parameters:
        R - the output value type
        Parameters:
        lift - the SingleOperator that receives the downstream's SingleObserver and should return a SingleObserver with custom behavior to be used as the consumer for the current Single.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if lift is null
        See Also:
        RxJava wiki: Writing operators, compose(SingleTransformer)
      • contains

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<java.lang.Boolean> contains​(@NonNull
                                                                 @NonNull java.lang.Object item)
        Signals true if the current Single signals a success value that is Object.equals(Object) with the value provided.

        Scheduler:
        contains does not operate by default on a particular Scheduler.
        Parameters:
        item - the value to compare against the success value of this Single
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if item is null
        Since:
        2.0
      • contains

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<java.lang.Boolean> contains​(@NonNull
                                                                 @NonNull java.lang.Object item,
                                                                 @NonNull
                                                                 @NonNull BiPredicate<java.lang.Object,​java.lang.Object> comparer)
        Signals true if the current Single signals a success value that is equal with the value provided by calling a BiPredicate.

        Scheduler:
        contains does not operate by default on a particular Scheduler.
        Parameters:
        item - the value to compare against the success value of this Single
        comparer - the function that receives the success value of this Single, the value provided and should return true if they are considered equal
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if item or comparer is null
        Since:
        2.0
      • onErrorReturn

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> onErrorReturn​(@NonNull
                                                      @NonNull Function<java.lang.Throwable,​? extends @NonNull T> itemSupplier)
        Ends the flow with a success item returned by a function for the Throwable error signaled by the current Single instead of signaling the error via onError.

        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 SingleObserver.onError(java.lang.Throwable) method, and then quits without invoking any more of its observer's methods. The onErrorReturn method changes this behavior. If you pass a function (resumeFunction) to a Single's onErrorReturn method, if the original Single encounters an error, instead of invoking its observer's SingleObserver.onError(java.lang.Throwable) method, it will instead emit the return value of resumeFunction.

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

        Scheduler:
        onErrorReturn does not operate by default on a particular Scheduler.
        Parameters:
        itemSupplier - a function that returns an item that the new Single will emit if the current Single encounters an error
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if itemSupplier is null
        See Also:
        ReactiveX operators documentation: Catch
      • onErrorReturnItem

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> onErrorReturnItem​(@NonNull
                                                          @NonNull T item)
        Signals the specified value as success in case the current Single signals an error.

        Scheduler:
        onErrorReturnItem does not operate by default on a particular Scheduler.
        Parameters:
        item - the value to signal if the current Single fails
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if item is null
        Since:
        2.0
      • onErrorResumeWith

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> onErrorResumeWith​(@NonNull
                                                          @NonNull SingleSource<? extends @NonNull T> fallback)
        Resumes the flow with the given SingleSource when the current Single fails instead of signaling the error via onError.

        By default, when a Single encounters an error that prevents it from emitting the expected item to its SingleObserver, the Single invokes its SingleObserver's onError method, and then quits without invoking any more of its SingleObserver's methods. The onErrorResumeWith method changes this behavior. If you pass another Single (resumeSingleInCaseOfError) to a Single's onErrorResumeWith method, if the original Single encounters an error, instead of invoking its SingleObserver's onError method, it will instead relinquish control to resumeSingleInCaseOfError which will invoke the SingleObserver's onSuccess method if it is able to do so. In such a case, because no Single necessarily invokes 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:
        onErrorResumeWith does not operate by default on a particular Scheduler.
        Parameters:
        fallback - a Single that will take control if source Single encounters an error.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if fallback is null
        See Also:
        ReactiveX operators documentation: Catch
      • onErrorComplete

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Maybe<T> onErrorComplete()
        Returns a Maybe instance that if the current Single emits an error, it will emit an onComplete and swallow the throwable.

        Scheduler:
        onErrorComplete does not operate by default on a particular Scheduler.
        Returns:
        the new Maybe instance
        Since:
        3.0.0
      • onErrorComplete

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Maybe<T> onErrorComplete​(@NonNull
                                                       @NonNull Predicate<? super java.lang.Throwable> predicate)
        Returns a Maybe instance that if this Single emits an error and the predicate returns true, it will emit an onComplete and swallow the throwable.

        Scheduler:
        onErrorComplete does not operate by default on a particular Scheduler.
        Parameters:
        predicate - the predicate to call when an Throwable is emitted which should return true if the Throwable should be swallowed and replaced with an onComplete.
        Returns:
        the new Maybe instance
        Throws:
        java.lang.NullPointerException - if predicate is null
        Since:
        3.0.0
      • onErrorResumeNext

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final @NonNull Single<T> onErrorResumeNext​(@NonNull
                                                          @NonNull Function<? super java.lang.Throwable,​? extends SingleSource<? extends @NonNull T>> fallbackSupplier)
        Resumes the flow with a SingleSource returned for the failure Throwable of the current Single by a function instead of signaling the error via onError.

        By default, when a Single encounters an error that prevents it from emitting the expected item to its SingleObserver, the Single invokes its SingleObserver's onError method, and then quits without invoking any more of its SingleObserver's methods. The onErrorResumeNext method changes this behavior. If you pass a function that will return another Single (resumeFunctionInCaseOfError) to a Single's onErrorResumeNext method, if the original Single encounters an error, instead of invoking its SingleObserver's onError method, it will instead relinquish control to resumeSingleInCaseOfError which will invoke the SingleObserver's onSuccess method if it is able to do so. In such a case, because no Single necessarily invokes 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:
        onErrorResumeNext does not operate by default on a particular Scheduler.
        Parameters:
        fallbackSupplier - a function that returns a SingleSource that will take control if source Single encounters an error.
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if fallbackSupplier is null
        Since:
        .20
        See Also:
        ReactiveX operators documentation: Catch
      • onTerminateDetach

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> onTerminateDetach()
        Nulls out references to the upstream producer and downstream SingleObserver if the sequence is terminated or downstream calls dispose().

        Scheduler:
        onTerminateDetach does not operate by default on a particular Scheduler.

        History: 2.1.5 - experimental

        Returns:
        the new Single which nulls out references to the upstream producer and downstream SingleObserver if the sequence is terminated or downstream calls dispose()
        Since:
        2.2
      • repeat

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Flowable<T> repeat​(long times)
        Re-subscribes to the current Single at most the given number of times and emits each success value as a Flowable sequence.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        repeat does not operate by default on a particular Scheduler.
        Parameters:
        times - the number of times to re-subscribe to the current Single
        Returns:
        the new Flowable instance
        Throws:
        java.lang.IllegalArgumentException - if times is negative
        Since:
        2.0
      • repeatWhen

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Flowable<T> repeatWhen​(@NonNull
                                                     @NonNull Function<? super Flowable<java.lang.Object>,​? extends org.reactivestreams.Publisher<?>> handler)
        Re-subscribes to the current Single if the Publisher returned by the handler function signals a value in response to a value signaled through the Flowable the handler receives.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer. The Publisher returned by the handler function is expected to honor backpressure as well.
        Scheduler:
        repeatWhen does not operate by default on a particular Scheduler.
        Parameters:
        handler - the function that is called with a Flowable that signals a value when the Single signaled a success value and returns a Publisher that has to signal a value to trigger a resubscription to the current Single, otherwise the terminal signal of the Publisher will be the terminal signal of the sequence as well.
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if handler is null
        Since:
        2.0
      • repeatUntil

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Flowable<T> repeatUntil​(@NonNull
                                                      @NonNull BooleanSupplier stop)
        Re-subscribes to the current Single until the given BooleanSupplier returns true and emits the success items as a Flowable sequence.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer.
        Scheduler:
        repeatUntil does not operate by default on a particular Scheduler.
        Parameters:
        stop - the BooleanSupplier called after the current Single succeeds and if returns false, the Single is re-subscribed; otherwise the sequence completes.
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if stop is null
        Since:
        2.0
      • retry

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> retry​(long times)
        Repeatedly re-subscribe at most the specified times to the current Single if it fails with an onError.

        Scheduler:
        retry does not operate by default on a particular Scheduler.
        Parameters:
        times - the number of times to resubscribe if the current Single fails
        Returns:
        the new Single instance
        Throws:
        java.lang.IllegalArgumentException - if times is negative
        Since:
        2.0
      • retry

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> retry​(@NonNull
                                              @NonNull BiPredicate<? super java.lang.Integer,​? super java.lang.Throwable> predicate)
        Re-subscribe to the current Single if the given predicate returns true when the Single fails with an onError.

        Scheduler:
        retry does not operate by default on a particular Scheduler.
        Parameters:
        predicate - the predicate called with the resubscription count and the failure Throwable and should return true if a resubscription should happen
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if predicate is null
        Since:
        2.0
      • retry

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> retry​(long times,
                                              @NonNull
                                              @NonNull Predicate<? super java.lang.Throwable> predicate)
        Repeatedly re-subscribe at most times or until the predicate returns false, whichever happens first if it fails with an onError.

        Scheduler:
        retry does not operate by default on a particular Scheduler.

        History: 2.1.8 - experimental

        Parameters:
        times - the number of times to resubscribe if the current Single fails
        predicate - the predicate called with the failure Throwable and should return true if a resubscription should happen
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if predicate is null
        java.lang.IllegalArgumentException - if times is negative
        Since:
        2.2
      • retry

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> retry​(@NonNull
                                              @NonNull Predicate<? super java.lang.Throwable> predicate)
        Re-subscribe to the current Single if the given predicate returns true when the Single fails with an onError.

        Scheduler:
        retry does not operate by default on a particular Scheduler.
        Parameters:
        predicate - the predicate called with the failure Throwable and should return true if a resubscription should happen
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if predicate is null
        Since:
        2.0
      • retryWhen

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Single<T> retryWhen​(@NonNull
                                                  @NonNull Function<? super Flowable<java.lang.Throwable>,​? extends org.reactivestreams.Publisher<?>> handler)
        Re-subscribes to the current Single if and when the Publisher returned by the handler function signals a value.

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

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

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

        
         Single.timer(1, TimeUnit.SECONDS)
             .doOnSubscribe(s -> System.out.println("subscribing"))
             .map(v -> { throw new RuntimeException(); })
             .retryWhen(errors -> {
                 AtomicInteger counter = new AtomicInteger();
                 return errors
                           .takeWhile(e -> counter.getAndIncrement() != 3)
                           .flatMap(e -> {
                               System.out.println("delay retry by " + counter.get() + " second(s)");
                               return Flowable.timer(counter.get(), TimeUnit.SECONDS);
                           });
             })
             .blockingGet();
         
        Scheduler:
        retryWhen does not operate by default on a particular Scheduler.
        Parameters:
        handler - the function that receives a Flowable of the error the Single emits and should return a Publisher that should signal a normal value (in response to the throwable the Flowable emits) to trigger a resubscription or signal an error to be the output of the resulting Single
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if handler is null
      • startWith

        @CheckReturnValue
        @NonNull
        @BackpressureSupport(FULL)
        @SchedulerSupport("none")
        public final @NonNull Flowable<T> startWith​(@NonNull
                                                    @NonNull org.reactivestreams.Publisher<@NonNull T> other)
        Returns a Flowable which first delivers the events of the other Publisher then runs the current Single.

        Backpressure:
        The returned Flowable honors the backpressure of the downstream consumer and expects the other Publisher to honor it as well.
        Scheduler:
        startWith does not operate by default on a particular Scheduler.
        Parameters:
        other - the other Publisher to run first
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if other is null
        Since:
        3.0.0
      • subscribe

        @SchedulerSupport("none")
        @NonNull
        public final @NonNull Disposable subscribe​(@NonNull
                                                   @NonNull Consumer<? super @NonNull T> onSuccess,
                                                   @NonNull
                                                   @NonNull Consumer<? super java.lang.Throwable> onError,
                                                   @NonNull
                                                   @NonNull DisposableContainer container)
        Wraps the given onXXX callbacks into a Disposable SingleObserver, adds it to the given DisposableContainer and ensures, that if the upstream terminates or this particular Disposable is disposed, the SingleObserver is removed from the given container.

        The SingleObserver will be removed after the callback for the terminal event has been invoked.

        Scheduler:
        subscribe does not operate by default on a particular Scheduler.
        Parameters:
        onSuccess - the callback for upstream items
        onError - the callback for an upstream error if any
        container - the DisposableContainer (such as CompositeDisposable) to add and remove the created Disposable SingleObserver
        Returns:
        the Disposable that allows disposing the particular subscription.
        Throws:
        java.lang.NullPointerException - if onSuccess, onError or container is null
        Since:
        3.1.0
      • subscribeActual

        protected abstract void subscribeActual​(@NonNull
                                                @NonNull SingleObserver<? super @NonNull T> observer)
        Implement this method in subclasses to handle the incoming SingleObservers.

        There is no need to call any of the plugin hooks on the current Single instance or the SingleObserver; all hooks and basic safeguards have been applied by subscribe(SingleObserver) before this method gets called.

        Parameters:
        observer - the SingleObserver to handle, not null
      • subscribeWith

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final <@NonNull E extends SingleObserver<? super @NonNull T>> E subscribeWith​(@NonNull E 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();
        
         DisposableSingleObserver<Integer> ds = new DisposableSingleObserver<>() {
             // ...
         };
        
         composite.add(source.subscribeWith(ds));
         
        Scheduler:
        subscribeWith does not operate by default on a particular Scheduler.
        Type Parameters:
        E - the type of the SingleObserver to use and return
        Parameters:
        observer - the SingleObserver (subclass) to use and return, not null
        Returns:
        the input observer
        Throws:
        java.lang.NullPointerException - if observer is null
        Since:
        2.0
      • timeInterval

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("io.reactivex:computation")
        public final @NonNull Single<Timed<T>> timeInterval()
        Measures the time (in milliseconds) between the subscription and success item emission of the current Single and signals it as a tuple (Timed) success value.

        If the current Single fails, the resulting Single will pass along the signal to the downstream. To measure the time to error, use materialize() and apply timeInterval().

        Scheduler:
        timeInterval uses the computation Scheduler for determining the current time upon subscription and upon receiving the success item from the current Single.
        Returns:
        the new Single instance
        Since:
        3.0.0
      • timeInterval

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<Timed<T>> timeInterval​(@NonNull
                                                            @NonNull Scheduler scheduler)
        Measures the time (in milliseconds) between the subscription and success item emission of the current Single and signals it as a tuple (Timed) success value.

        If the current Single fails, the resulting Single will pass along the signal to the downstream. To measure the time to error, use materialize() and apply timeInterval(Scheduler).

        Scheduler:
        timeInterval uses the provided Scheduler for determining the current time upon subscription and upon receiving the success item from the current Single.
        Parameters:
        scheduler - the Scheduler used for providing the current time
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if scheduler is null
        Since:
        3.0.0
      • timeInterval

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("io.reactivex:computation")
        public final @NonNull Single<Timed<T>> timeInterval​(@NonNull
                                                            @NonNull java.util.concurrent.TimeUnit unit)
        Measures the time between the subscription and success item emission of the current Single and signals it as a tuple (Timed) success value.

        If the current Single fails, the resulting Single will pass along the signals to the downstream. To measure the time to error, use materialize() and apply timeInterval(TimeUnit, Scheduler).

        Scheduler:
        timeInterval uses the computation Scheduler for determining the current time upon subscription and upon receiving the success item from the current Single.
        Parameters:
        unit - the time unit for measurement
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        3.0.0
      • timeInterval

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<Timed<T>> timeInterval​(@NonNull
                                                            @NonNull java.util.concurrent.TimeUnit unit,
                                                            @NonNull
                                                            @NonNull Scheduler scheduler)
        Measures the time between the subscription and success item emission of the current Single and signals it as a tuple (Timed) success value.

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

        Scheduler:
        timeInterval uses the provided Scheduler for determining the current time upon subscription and upon receiving the success item from the current Single.
        Parameters:
        unit - the time unit for measurement
        scheduler - the Scheduler used for providing the current time
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit or scheduler is null
        Since:
        3.0.0
      • timestamp

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("io.reactivex:computation")
        public final @NonNull Single<Timed<T>> timestamp()
        Combines the success value from the current Single with the current time (in milliseconds) of its reception, using the computation Scheduler as time source, then signals them as a Timed instance.

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

        Scheduler:
        timestamp uses the computation Scheduler for determining the current time upon receiving the success item from the current Single.
        Returns:
        the new Single instance
        Since:
        3.0.0
      • timestamp

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<Timed<T>> timestamp​(@NonNull
                                                         @NonNull Scheduler scheduler)
        Combines the success value from the current Single with the current time (in milliseconds) of its reception, using the given Scheduler as time source, then signals them as a Timed instance.

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

        Scheduler:
        timestamp uses the provided Scheduler for determining the current time upon receiving the success item from the current Single.
        Parameters:
        scheduler - the Scheduler used for providing the current time
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if scheduler is null
        Since:
        3.0.0
      • timestamp

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("io.reactivex:computation")
        public final @NonNull Single<Timed<T>> timestamp​(@NonNull
                                                         @NonNull java.util.concurrent.TimeUnit unit)
        Combines the success value from the current Single with the current time of its reception, using the computation Scheduler as time source, then signals it as a Timed instance.

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

        Scheduler:
        timestamp uses the computation Scheduler, for determining the current time upon receiving the success item from the current Single.
        Parameters:
        unit - the time unit for measurement
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        3.0.0
      • timestamp

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<Timed<T>> timestamp​(@NonNull
                                                         @NonNull java.util.concurrent.TimeUnit unit,
                                                         @NonNull
                                                         @NonNull Scheduler scheduler)
        Combines the success value from the current Single with the current time of its reception, using the given Scheduler as time source, then signals it as a Timed instance.

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

        Scheduler:
        timestamp uses the provided Scheduler, which is used for determining the current time upon receiving the success item from the current Single.
        Parameters:
        unit - the time unit for measurement
        scheduler - the Scheduler used for providing the current time
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit or scheduler is null
        Since:
        3.0.0
      • takeUntil

        @BackpressureSupport(FULL)
        @CheckReturnValue
        @NonNull
        @SchedulerSupport("none")
        public final <@NonNull E> @NonNull Single<T> takeUntil​(@NonNull
                                                               @NonNull org.reactivestreams.Publisher<@NonNull E> other)
        Returns a Single that emits the item emitted by the current Single until a Publisher emits an item or completes. Upon emission of an item from other, this will emit a CancellationException rather than go to SingleObserver.onSuccess(Object).

        Backpressure:
        The other publisher is consumed in an unbounded fashion but will be cancelled after the first item it produced.
        Scheduler:
        takeUntil does not operate by default on a particular Scheduler.
        Type Parameters:
        E - the type of items emitted by other
        Parameters:
        other - the Publisher whose first emitted item or completion will cause takeUntil to emit CancellationException if the current Single hasn't completed till then
        Returns:
        the new Single that emits the item emitted by the current Single until such time as other emits its first item
        Throws:
        java.lang.NullPointerException - if other is null
        See Also:
        ReactiveX operators documentation: TakeUntil
      • timeout

        @CheckReturnValue
        @SchedulerSupport("io.reactivex:computation")
        @NonNull
        public final @NonNull Single<T> timeout​(long timeout,
                                                @NonNull
                                                @NonNull java.util.concurrent.TimeUnit unit)
        Signals a TimeoutException if the current Single doesn't signal a success value within the specified timeout window.

        Scheduler:
        timeout signals the TimeoutException on the computation Scheduler.
        Parameters:
        timeout - the timeout amount
        unit - the time unit
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit is null
        Since:
        2.0
      • timeout

        @CheckReturnValue
        @SchedulerSupport("custom")
        @NonNull
        public final @NonNull Single<T> timeout​(long timeout,
                                                @NonNull
                                                @NonNull java.util.concurrent.TimeUnit unit,
                                                @NonNull
                                                @NonNull Scheduler scheduler)
        Signals a TimeoutException if the current Single doesn't signal a success value within the specified timeout window.

        Scheduler:
        timeout signals the TimeoutException on the Scheduler you specify.
        Parameters:
        timeout - the timeout amount
        unit - the time unit
        scheduler - the target Scheduler where the timeout is awaited and the TimeoutException signaled
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit or scheduler is null
        Since:
        2.0
      • timeout

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<T> timeout​(long timeout,
                                                @NonNull
                                                @NonNull java.util.concurrent.TimeUnit unit,
                                                @NonNull
                                                @NonNull Scheduler scheduler,
                                                @NonNull
                                                @NonNull SingleSource<? extends @NonNull T> fallback)
        Runs the current Single and if it doesn't signal within the specified timeout window, it is disposed and the other SingleSource subscribed to.

        Scheduler:
        timeout subscribes to the other SingleSource on the Scheduler you specify.
        Parameters:
        timeout - the timeout amount
        unit - the time unit
        scheduler - the Scheduler where the timeout is awaited and the subscription to other happens
        fallback - the other SingleSource that gets subscribed to if the current Single times out
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if unit, scheduler or fallback is null
        Since:
        2.0
      • timeout

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("io.reactivex:computation")
        public final @NonNull Single<T> timeout​(long timeout,
                                                @NonNull
                                                @NonNull java.util.concurrent.TimeUnit unit,
                                                @NonNull
                                                @NonNull SingleSource<? extends @NonNull T> fallback)
        Runs the current Single and if it doesn't signal within the specified timeout window, it is disposed and the other SingleSource subscribed to.

        Scheduler:
        timeout subscribes to the other SingleSource on the computation Scheduler.
        Parameters:
        timeout - the timeout amount
        unit - the time unit
        fallback - the other SingleSource that gets subscribed to if the current Single times out
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if fallback or unit is null
        Since:
        2.0
      • to

        @CheckReturnValue
        @SchedulerSupport("none")
        public final <R> R to​(@NonNull
                              @NonNull SingleConverter<@NonNull T,​? extends R> converter)
        Calls the specified converter function during assembly time and returns its resulting value.

        This allows fluent conversion to any other type.

        Scheduler:
        to does not operate by default on a particular Scheduler.

        History: 2.1.7 - experimental

        Type Parameters:
        R - the resulting object type
        Parameters:
        converter - the function that receives the current Single instance and returns a value
        Returns:
        the converted value
        Throws:
        java.lang.NullPointerException - if converter is null
        Since:
        2.2
      • unsubscribeOn

        @CheckReturnValue
        @NonNull
        @SchedulerSupport("custom")
        public final @NonNull Single<T> unsubscribeOn​(@NonNull
                                                      @NonNull Scheduler scheduler)
        Returns a Single which makes sure when a SingleObserver disposes the Disposable, that call is propagated up on the specified Scheduler.

        Scheduler:
        unsubscribeOn calls dispose() of the upstream on the Scheduler you specify.

        History: 2.0.9 - experimental

        Parameters:
        scheduler - the target scheduler where to execute the disposal
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if scheduler is null
        Since:
        2.2
      • zipWith

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final <@NonNull U,​@NonNull R> @NonNull Single<R> zipWith​(@NonNull
                                                                              @NonNull SingleSource<@NonNull U> other,
                                                                              @NonNull
                                                                              @NonNull BiFunction<? super @NonNull T,​? super @NonNull U,​? extends @NonNull R> zipper)
        Returns a Single that emits the result of applying a specified function to the pair of items emitted by the current Single and another specified SingleSource.

        Scheduler:
        zipWith does not operate by default on a particular Scheduler.
        Type Parameters:
        U - the type of items emitted by the other Single
        R - the type of items emitted by the resulting Single
        Parameters:
        other - the other SingleSource
        zipper - a function that combines the pairs of items from the two SingleSources to generate the items to be emitted by the resulting Single
        Returns:
        the new Single that pairs up values from the current Single and the other SingleSource and emits the results of zipFunction applied to these pairs
        Throws:
        java.lang.NullPointerException - if other or zipper is null
        See Also:
        ReactiveX operators documentation: Zip
      • test

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull TestObserver<T> test​(boolean dispose)
        Creates a TestObserver optionally in cancelled state, then subscribes it to this Single.

        Scheduler:
        test does not operate by default on a particular Scheduler.
        Parameters:
        dispose - if true, the TestObserver will be cancelled before subscribing to this Single.
        Returns:
        the new TestObserver instance
        Since:
        2.0
      • fromCompletionStage

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public static <@NonNull T> @NonNull Single<@NonNull T> fromCompletionStage​(@NonNull
                                                                                   @NonNull java.util.concurrent.CompletionStage<@NonNull T> stage)
        Signals the completion value or error of the given (hot) CompletionStage-based asynchronous calculation.

        Note that the operator takes an already instantiated, running or terminated CompletionStage. If the CompletionStage is to be created per consumer upon subscription, use defer(Supplier) around fromCompletionStage:

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

        If the CompletionStage completes with null, the resulting Single is terminated with a NullPointerException.

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

        Scheduler:
        fromCompletionStage does not operate by default on a particular Scheduler.
        Type Parameters:
        T - the element type of the CompletionStage
        Parameters:
        stage - the CompletionStage to convert to Single and signal its success value or error
        Returns:
        the new Single instance
        Throws:
        java.lang.NullPointerException - if stage is null
        Since:
        3.0.0
      • mapOptional

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final <@NonNull R> @NonNull Maybe<R> mapOptional​(@NonNull
                                                                @NonNull Function<? super @NonNull T,​@NonNull java.util.Optional<? extends @NonNull R>> mapper)
        Maps the upstream success value into an Optional and emits the contained item if not empty as a Maybe.

        Scheduler:
        mapOptional does not operate by default on a particular Scheduler.
        Type Parameters:
        R - the non-null output type
        Parameters:
        mapper - the function that receives the upstream success item and should return a non-empty Optional to emit as the success output or an empty Optional to complete the Maybe
        Returns:
        the new Maybe instance
        Throws:
        java.lang.NullPointerException - if mapper is null
        Since:
        3.0.0
        See Also:
        map(Function), filter(Predicate)
      • toCompletionStage

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final @NonNull java.util.concurrent.CompletionStage<T> toCompletionStage()
        Signals the upstream success item (or error) via a CompletionStage.

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

        Scheduler:
        toCompletionStage does not operate by default on a particular Scheduler.
        Returns:
        the new CompletionStage instance
        Since:
        3.0.0
      • flattenStreamAsFlowable

        @CheckReturnValue
        @SchedulerSupport("none")
        @BackpressureSupport(FULL)
        @NonNull
        public final <@NonNull R> @NonNull Flowable<R> flattenStreamAsFlowable​(@NonNull
                                                                               @NonNull Function<? super @NonNull T,​? extends java.util.stream.Stream<? extends @NonNull R>> mapper)
        Maps the upstream succecss value into a Java Stream and emits its items to the downstream consumer as a Flowable.

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

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

        Primitive streams are not supported and items have to be boxed manually (e.g., via IntStream.boxed()):

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

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

        Backpressure:
        The operator honors backpressure from downstream and iterates the given Stream on demand (i.e., when requested).
        Scheduler:
        flattenStreamAsFlowable does not operate by default on a particular Scheduler.
        Type Parameters:
        R - the element type of the Stream and the output Flowable
        Parameters:
        mapper - the function that receives the upstream success item and should return a Stream of values to emit.
        Returns:
        the new Flowable instance
        Throws:
        java.lang.NullPointerException - if mapper is null
        Since:
        3.0.0
        See Also:
        flattenAsFlowable(Function), flattenStreamAsObservable(Function)
      • flattenStreamAsObservable

        @CheckReturnValue
        @SchedulerSupport("none")
        @NonNull
        public final <@NonNull R> @NonNull Observable<R> flattenStreamAsObservable​(@NonNull
                                                                                   @NonNull Function<? super @NonNull T,​? extends java.util.stream.Stream<? extends @NonNull R>> mapper)
        Maps the upstream succecss value into a Java Stream and emits its items to the downstream consumer as an Observable.

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

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

        Primitive streams are not supported and items have to be boxed manually (e.g., via IntStream.boxed()):

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

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

        Scheduler:
        flattenStreamAsObservable does not operate by default on a particular Scheduler.
        Type Parameters:
        R - the element type of the Stream and the output Observable
        Parameters:
        mapper - the function that receives the upstream success item and should return a Stream of values to emit.
        Returns:
        the new Observable instance
        Throws:
        java.lang.NullPointerException - if mapper is null
        Since:
        3.0.0
        See Also:
        flattenAsObservable(Function), flattenStreamAsFlowable(Function)