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 io., 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 io. 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.
SingleSubject does not operate by default on a particular io.reactivex.Scheduler and
the SingleObservers get notified on the thread where the terminating onSuccess or onError
methods were invoked.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
io.reactivex.plugins.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
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final SingleSubject. | |
| pack-priv Throwable | |
| pack-priv final AtomicReference | |
| pack-priv final AtomicBoolean | |
| pack-priv static final SingleSubject. | |
| pack-priv T |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | |
| public static < the value type received and emitted T> SingleSubject | |
| public Throwable | Returns: the terminal error or null if not terminated or not with an errorReturns the terminal error if this SingleSubject has been terminated with an error, null otherwise. |
| public T | Returns: the success value or nullReturns the success value if this SingleSubject was terminated with a success value. |
| public boolean | Returns: true if this SingleSubject has observersReturns true if this SingleSubject has observers. |
| public boolean | Returns: true if this SingleSubject has been terminated with an errorReturns true if this SingleSubject has been terminated with an error. |
| public boolean | Returns: true if this SingleSubject was terminated with a success valueReturns true if this SingleSubject was terminated with a success value. |
| pack-priv int | |
| public void | onError(Throwable
the exception encountered by the Single e)Implements io. Single has experienced an error condition.
|
| public void | onSubscribe(Disposable
the Disposable instance whose d)Disposable#dispose() can
be called anytime to cancel the connectionImplements io. onSubscribe(Disposable) itself) and asynchronous manner.
|
| public void | onSuccess(T
the item emitted by the Single value)Implements io. Single has finished sending
push-based notifications.
|
| pack-priv void | |
| protected void | subscribeActual(SingleObserver<? super T>
the SingleObserver to handle, not null observer)Implements abstract io. SingleObservers.
|
| EMPTY | back to summary |
|---|---|
pack-priv static final SingleSubject.
| |
| error | back to summary |
|---|---|
| pack-priv Throwable error | |
| observers | back to summary |
|---|---|
| pack-priv final AtomicReference<SingleSubject. | |
| once | back to summary |
|---|---|
| pack-priv final AtomicBoolean once | |
| TERMINATED | back to summary |
|---|---|
pack-priv static final SingleSubject.
| |
| value | back to summary |
|---|---|
| pack-priv T value | |
| SingleSubject | back to summary |
|---|---|
pack-priv SingleSubject()
| |
| add | back to summary |
|---|---|
| pack-priv boolean add(SingleSubject. | |
| create | back to summary |
|---|---|
| public static <T> SingleSubject Creates a fresh SingleSubject.
| |
| getThrowable | back to summary |
|---|---|
| public Throwable getThrowable() Returns the terminal error if this SingleSubject has been terminated with an error, null otherwise. | |
| getValue | back to summary |
|---|---|
| public T getValue() Returns the success value if this SingleSubject was terminated with a success value.
| |
| hasObservers | back to summary |
|---|---|
| public boolean hasObservers() Returns true if this SingleSubject has observers.
| |
| hasThrowable | back to summary |
|---|---|
| public boolean hasThrowable() Returns true if this SingleSubject has been terminated with an error.
| |
| hasValue | back to summary |
|---|---|
| public boolean hasValue() Returns true if this SingleSubject was terminated with a success value.
| |
| observerCount | back to summary |
|---|---|
| pack-priv int observerCount() Returns the number of current observers.
| |
| onError | back to summary |
|---|---|
| public void onError(Throwable e) Implements io. Doc from io. Notifies the SingleObserver that the
If the
| |
| onSubscribe | back to summary |
|---|---|
| public void onSubscribe(Disposable d) Implements io. Doc from io. Provides the SingleObserver with the means of cancelling (disposing) the
connection (channel) with the Single in both
synchronous (from within
| |
| onSuccess | back to summary |
|---|---|
| public void onSuccess(T value) Implements io. Doc from io. Notifies the SingleObserver with a single item and that the
The
| |
| remove | back to summary |
|---|---|
pack-priv void remove(SingleSubject.
| |
| subscribeActual | back to summary |
|---|---|
| protected void subscribeActual(SingleObserver<? super T> observer) Implements abstract io. Doc from io. Implement this method in subclasses to handle the incoming There is no need to call any of the plugin hooks on the current
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final SingleObserver | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | dispose()
Implements io. |
| public boolean | isDisposed()
Implements io. |
| downstream | back to summary |
|---|---|
| pack-priv final SingleObserver<? super T> downstream | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. | |
| SingleDisposable | back to summary |
|---|---|
| pack-priv SingleDisposable(SingleObserver<? super T> actual, SingleSubject<T> parent) | |
| dispose | back to summary |
|---|---|
| public void dispose() Implements io. Doc from io. Dispose the resource, the operation should be idempotent.
| |
| isDisposed | back to summary |
|---|---|
| public boolean isDisposed() Implements io. Doc from io. Returns true if this resource has been disposed.
| |