T
- the value type received and emittedpublic final class SingleSubject<T> extends Single<T> implements SingleObserver<T>
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,
null
s 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.
SingleSubject
does not operate by default on a particular Scheduler
and
the SingleObserver
s 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 SingleObserver
s. During this emission,
if one or more SingleObserver
s dispose their respective Disposable
s, the
Throwable
is delivered to the global error handler via
RxJavaPlugins.onError(Throwable)
(multiple times if multiple SingleObserver
s
cancel at once).
If there were no SingleObserver
s 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 | Method and Description |
---|---|
static <T> SingleSubject<T> |
create()
Creates a fresh SingleSubject.
|
Throwable |
getThrowable()
Returns the terminal error if this SingleSubject has been terminated with an error, null otherwise.
|
T |
getValue()
Returns the success value if this SingleSubject was terminated with a success value.
|
boolean |
hasObservers()
Returns true if this SingleSubject has observers.
|
boolean |
hasThrowable()
Returns true if this SingleSubject has been terminated with an error.
|
boolean |
hasValue()
Returns true if this SingleSubject was terminated with a success value.
|
void |
onError(Throwable e)
Notifies the SingleObserver that the
Single has experienced an error condition. |
void |
onSubscribe(Disposable d)
Provides the SingleObserver with the means of cancelling (disposing) the
connection (channel) with the Single in both
synchronous (from within
onSubscribe(Disposable) itself) and asynchronous manner. |
void |
onSuccess(T value)
Notifies the SingleObserver with a single item and that the
Single has finished sending
push-based notifications. |
protected void |
subscribeActual(SingleObserver<? super T> observer)
Implement this method in subclasses to handle the incoming
SingleObserver s. |
amb, ambArray, ambWith, as, blockingGet, cache, cast, compose, concat, concat, concat, concat, concat, concat, concat, concatArray, concatArrayEager, concatEager, concatEager, concatWith, contains, contains, create, defer, delay, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, delaySubscription, delaySubscription, delaySubscription, dematerialize, doAfterSuccess, doAfterTerminate, doFinally, doOnDispose, doOnError, doOnEvent, doOnSubscribe, doOnSuccess, doOnTerminate, equals, error, error, filter, flatMap, flatMapCompletable, flatMapMaybe, flatMapObservable, flatMapPublisher, flattenAsFlowable, flattenAsObservable, fromCallable, fromFuture, fromFuture, fromFuture, fromFuture, fromObservable, fromPublisher, hide, ignoreElement, just, lift, map, materialize, merge, merge, merge, merge, merge, merge, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, never, observeOn, onErrorResumeNext, onErrorResumeNext, onErrorReturn, onErrorReturnItem, onTerminateDetach, repeat, repeat, repeatUntil, repeatWhen, retry, retry, retry, retry, retry, retryWhen, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, takeUntil, takeUntil, takeUntil, test, test, timeout, timeout, timeout, timeout, timer, timer, to, toCompletable, toFlowable, toFuture, toMaybe, toObservable, unsafeCreate, unsubscribeOn, using, using, wrap, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipArray, zipWith
@CheckReturnValue @NonNull public static <T> SingleSubject<T> create()
T
- the value type received and emittedpublic void onSubscribe(@NonNull Disposable d)
SingleObserver
onSubscribe(Disposable)
itself) and asynchronous manner.onSubscribe
in interface SingleObserver<T>
d
- the Disposable instance whose Disposable.dispose()
can
be called anytime to cancel the connectionpublic void onSuccess(@NonNull T value)
SingleObserver
Single
has finished sending
push-based notifications.
The Single
will not call this method if it calls SingleObserver.onError(java.lang.Throwable)
.
onSuccess
in interface SingleObserver<T>
value
- the item emitted by the Singlepublic void onError(@NonNull Throwable e)
SingleObserver
Single
has experienced an error condition.
If the Single
calls this method, it will not thereafter call SingleObserver.onSuccess(T)
.
onError
in interface SingleObserver<T>
e
- the exception encountered by the Singleprotected void subscribeActual(@NonNull SingleObserver<? super T> observer)
Single
SingleObserver
s.
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.
subscribeActual
in class Single<T>
observer
- the SingleObserver to handle, not null@Nullable public T getValue()
public boolean hasValue()
@Nullable public Throwable getThrowable()
public boolean hasThrowable()
public boolean hasObservers()