Class SingleSubject<T>

  • Type Parameters:
    T - the value type received and emitted
    All Implemented Interfaces:
    SingleObserver<T>, SingleSource<T>

    public final class SingleSubject<T>
    extends Single<T>
    implements SingleObserver<T>
    Represents a hot Single-like source and consumer of events similar to Subjects.

    This subject does not have a public constructor by design; a new non-terminated instance of this SingleSubject can be created via the create() method.

    Since the SingleSubject is conceptionally derived from the Processor type in the Reactive Streams specification, nulls are not allowed (Rule 2.13) as parameters to onSuccess(Object) and onError(Throwable). Such calls will result in a NullPointerException being thrown and the subject's state is not changed.

    Since a SingleSubject is a Single, calling onSuccess or onError will move this SingleSubject into its terminal state atomically.

    All methods are thread safe. Calling onSuccess(Object) multiple times has no effect. Calling onError(Throwable) multiple times relays the Throwable to the RxJavaPlugins.onError(Throwable) global error handler.

    Even though SingleSubject implements the SingleObserver interface, calling onSubscribe is not required (Rule 2.12) if the subject is used as a standalone source. However, calling onSubscribe after the SingleSubject reached its terminal state will result in the given Disposable being disposed immediately.

    This SingleSubject supports the standard state-peeking methods hasThrowable(), getThrowable() and hasObservers() as well as means to read any success item in a non-blocking and thread-safe manner via hasValue() and getValue().

    The SingleSubject does not support clearing its cached onSuccess value.

    Scheduler:
    SingleSubject does not operate by default on a particular Scheduler and the SingleObservers get notified on the thread where the terminating onSuccess or onError methods were invoked.
    Error handling:
    When the onError(Throwable) is called, the SingleSubject enters into a terminal state and emits the same Throwable instance to the last set of SingleObservers. During this emission, if one or more SingleObservers dispose their respective Disposables, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) (multiple times if multiple SingleObservers cancel at once). If there were no SingleObservers subscribed to this SingleSubject when the onError() was called, the global error handler is not invoked.

    Example usage:

    
     SingleSubject<Integer> subject1 = SingleSubject.create();
     
     TestObserver<Integer> to1 = subject1.test();
     
     // SingleSubjects are empty by default
     to1.assertEmpty();
     
     subject1.onSuccess(1);
     
     // onSuccess is a terminal event with SingleSubjects
     // TestObserver converts onSuccess into onNext + onComplete
     to1.assertResult(1);
    
     TestObserver<Integer> to2 = subject1.test();
     
     // late Observers receive the terminal signal (onSuccess) too
     to2.assertResult(1);
     

    History: 2.0.5 - experimental

    Since:
    2.1
    • Constructor Detail

      • SingleSubject

        SingleSubject()
    • Method Detail

      • subscribeActual

        protected void subscribeActual​(@NonNull
                                       @NonNull SingleObserver<? super T> observer)
        Description copied from class: Single
        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 Single.subscribe(SingleObserver) before this method gets called.

        Specified by:
        subscribeActual in class Single<T>
        Parameters:
        observer - the SingleObserver to handle, not null
      • getValue

        @Nullable
        public T getValue()
        Returns the success value if this SingleSubject was terminated with a success value.
        Returns:
        the success value or null
      • hasValue

        public boolean hasValue()
        Returns true if this SingleSubject was terminated with a success value.
        Returns:
        true if this SingleSubject was terminated with a success value
      • getThrowable

        @Nullable
        public @Nullable java.lang.Throwable getThrowable()
        Returns the terminal error if this SingleSubject has been terminated with an error, null otherwise.
        Returns:
        the terminal error or null if not terminated or not with an error
      • hasThrowable

        public boolean hasThrowable()
        Returns true if this SingleSubject has been terminated with an error.
        Returns:
        true if this SingleSubject has been terminated with an error
      • hasObservers

        public boolean hasObservers()
        Returns true if this SingleSubject has observers.
        Returns:
        true if this SingleSubject has observers
      • observerCount

        int observerCount()
        Returns the number of current observers.
        Returns:
        the number of current observers