T
- the value type received and emittedpublic final class MaybeSubject<T> extends Maybe<T> implements MaybeObserver<T>
This subject does not have a public constructor by design; a new non-terminated instance of this
MaybeSubject
can be created via the create()
method.
Since the MaybeSubject
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 MaybeSubject
is a Maybe
, calling onSuccess
, onError
or onComplete
will move this MaybeSubject
into its terminal state atomically.
All methods are thread safe. Calling onSuccess(Object)
or onComplete()
multiple
times has no effect. Calling onError(Throwable)
multiple times relays the Throwable
to
the RxJavaPlugins.onError(Throwable)
global error handler.
Even though MaybeSubject
implements the MaybeObserver
interface, calling
onSubscribe
is not required (Rule 2.12)
if the subject is used as a standalone source. However, calling onSubscribe
after the MaybeSubject
reached its terminal state will result in the
given Disposable
being disposed immediately.
This MaybeSubject
supports the standard state-peeking methods hasComplete()
, 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 MaybeSubject
does not support clearing its cached onSuccess
value.
MaybeSubject
does not operate by default on a particular Scheduler
and
the MaybeObserver
s get notified on the thread where the terminating onSuccess
, onError
or onComplete
methods were invoked.onError(Throwable)
is called, the MaybeSubject
enters into a terminal state
and emits the same Throwable
instance to the last set of MaybeObserver
s. During this emission,
if one or more MaybeObserver
s dispose their respective Disposable
s, the
Throwable
is delivered to the global error handler via
RxJavaPlugins.onError(Throwable)
(multiple times if multiple MaybeObserver
s
cancel at once).
If there were no MaybeObserver
s subscribed to this MaybeSubject
when the onError()
was called, the global error handler is not invoked.
Example usage:
MaybeSubject<Integer> subject1 = MaybeSubject.create();
TestObserver<Integer> to1 = subject1.test();
// MaybeSubjects are empty by default
to1.assertEmpty();
subject1.onSuccess(1);
// onSuccess is a terminal event with MaybeSubjects
// 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);
// -----------------------------------------------------
MaybeSubject<Integer> subject2 = MaybeSubject.create();
TestObserver<Integer> to3 = subject2.test();
subject2.onComplete();
// a completed MaybeSubject completes its MaybeObservers
to3.assertResult();
TestObserver<Integer> to4 = subject1.test();
// late Observers receive the terminal signal (onComplete) too
to4.assertResult();
History: 2.0.5 - experimental
Modifier and Type | Method and Description |
---|---|
static <T> MaybeSubject<T> |
create()
Creates a fresh MaybeSubject.
|
Throwable |
getThrowable()
Returns the terminal error if this MaybeSubject has been terminated with an error, null otherwise.
|
T |
getValue()
Returns the success value if this MaybeSubject was terminated with a success value.
|
boolean |
hasComplete()
Returns true if this MaybeSubject has been completed.
|
boolean |
hasObservers()
Returns true if this MaybeSubject has observers.
|
boolean |
hasThrowable()
Returns true if this MaybeSubject has been terminated with an error.
|
boolean |
hasValue()
Returns true if this MaybeSubject was terminated with a success value.
|
void |
onComplete()
Called once the deferred computation completes normally.
|
void |
onError(Throwable e)
Notifies the MaybeObserver that the
Maybe has experienced an error condition. |
void |
onSubscribe(Disposable d)
Provides the MaybeObserver with the means of cancelling (disposing) the
connection (channel) with the Maybe in both
synchronous (from within
onSubscribe(Disposable) itself) and asynchronous manner. |
void |
onSuccess(T value)
Notifies the MaybeObserver with one item and that the
Maybe has finished sending
push-based notifications. |
protected void |
subscribeActual(MaybeObserver<? super T> observer)
Implement this method in subclasses to handle the incoming
MaybeObserver s. |
amb, ambArray, ambWith, blockingGet, blockingGet, cache, cast, compose, concat, concat, concat, concat, concat, concat, concatArray, concatArrayDelayError, concatArrayEager, concatDelayError, concatDelayError, concatEager, concatEager, concatMap, concatWith, contains, count, create, defaultIfEmpty, defer, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, doAfterSuccess, doAfterTerminate, doFinally, doOnComplete, doOnDispose, doOnError, doOnEvent, doOnSubscribe, doOnSuccess, doOnTerminate, empty, error, error, filter, flatMap, flatMap, flatMap, flatMapCompletable, flatMapObservable, flatMapPublisher, flatMapSingle, flatMapSingleElement, flattenAsFlowable, flattenAsObservable, fromAction, fromCallable, fromCompletable, fromFuture, fromFuture, fromRunnable, fromSingle, fromSupplier, hide, ignoreElement, isEmpty, just, lift, map, materialize, merge, merge, merge, merge, merge, merge, merge, mergeArray, mergeArrayDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, never, observeOn, ofType, onErrorComplete, onErrorComplete, onErrorResumeNext, onErrorResumeWith, onErrorReturn, onErrorReturnItem, onExceptionResumeNext, onTerminateDetach, repeat, repeat, repeatUntil, repeatWhen, retry, retry, retry, retry, retry, retryUntil, retryWhen, sequenceEqual, sequenceEqual, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, switchIfEmpty, switchIfEmpty, takeUntil, takeUntil, test, test, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timer, timer, to, toFlowable, toObservable, toSingle, unsafeCreate, unsubscribeOn, using, using, wrap, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipArray, zipWith
@CheckReturnValue @NonNull public static <T> MaybeSubject<T> create()
T
- the value type received and emittedpublic void onSubscribe(Disposable d)
MaybeObserver
onSubscribe(Disposable)
itself) and asynchronous manner.onSubscribe
in interface MaybeObserver<T>
d
- the Disposable instance whose Disposable.dispose()
can
be called anytime to cancel the connectionpublic void onSuccess(T value)
MaybeObserver
Maybe
has finished sending
push-based notifications.
The Maybe
will not call this method if it calls MaybeObserver.onError(java.lang.Throwable)
.
onSuccess
in interface MaybeObserver<T>
value
- the item emitted by the Maybepublic void onError(Throwable e)
MaybeObserver
Maybe
has experienced an error condition.
If the Maybe
calls this method, it will not thereafter call MaybeObserver.onSuccess(T)
.
onError
in interface MaybeObserver<T>
e
- the exception encountered by the Maybepublic void onComplete()
MaybeObserver
onComplete
in interface MaybeObserver<T>
protected void subscribeActual(MaybeObserver<? super T> observer)
Maybe
MaybeObserver
s.
There is no need to call any of the plugin hooks on the current Maybe
instance or
the MaybeObserver
; all hooks and basic safeguards have been
applied by Maybe.subscribe(MaybeObserver)
before this method gets called.
subscribeActual
in class Maybe<T>
observer
- the MaybeObserver to handle, not null@Nullable public T getValue()
public boolean hasValue()
@Nullable public Throwable getThrowable()
public boolean hasThrowable()
public boolean hasComplete()
public boolean hasObservers()