Class MaybeSubject<T>
- Type Parameters:
T- the value type received and emitted
- All Implemented Interfaces:
MaybeObserver<T>, MaybeSource<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,
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 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.
- Scheduler:
MaybeSubjectdoes not operate by default on a particularSchedulerand theMaybeObservers get notified on the thread where the terminatingonSuccess,onErrororonCompletemethods were invoked.- Error handling:
- When the
onError(Throwable)is called, theMaybeSubjectenters into a terminal state and emits the sameThrowableinstance to the last set ofMaybeObservers. During this emission, if one or moreMaybeObservers dispose their respectiveDisposables, theThrowableis delivered to the global error handler viaRxJavaPlugins.onError(Throwable)(multiple times if multipleMaybeObservers cancel at once). If there were noMaybeObservers subscribed to thisMaybeSubjectwhen theonError()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
- Since:
- 2.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NonNull MaybeSubject<T> create()Creates a fresh MaybeSubject.Returns the terminal error if this MaybeSubject has been terminated with an error, null otherwise.getValue()Returns the success value if this MaybeSubject was terminated with a success value.booleanReturns true if this MaybeSubject has been completed.booleanReturns true if this MaybeSubject has observers.booleanReturns true if this MaybeSubject has been terminated with an error.booleanhasValue()Returns true if this MaybeSubject was terminated with a success value.voidCalled once the deferred computation completes normally.voidNotifies theMaybeObserverthat theMaybehas experienced an error condition.voidProvides theMaybeObserverwith the means of cancelling (disposing) the connection (channel) with theMaybein both synchronous (from withinonSubscribe(Disposable)itself) and asynchronous manner.voidNotifies theMaybeObserverwith one item and that theMaybehas finished sending push-based notifications.protected voidsubscribeActual(MaybeObserver<? super T> observer) Implement this method in subclasses to handle the incomingMaybeObservers.Methods inherited from class Maybe
amb, ambArray, ambWith, blockingGet, blockingGet, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, cache, cast, compose, concat, concat, concat, concat, concat, concat, concatArray, concatArrayDelayError, concatArrayEager, concatArrayEagerDelayError, concatDelayError, concatDelayError, concatDelayError, concatEager, concatEager, concatEager, concatEager, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatMap, concatMapCompletable, concatMapSingle, concatWith, contains, count, create, defaultIfEmpty, defer, delay, delay, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, dematerialize, doAfterSuccess, doAfterTerminate, doFinally, doOnComplete, doOnDispose, doOnError, doOnEvent, doOnLifecycle, doOnSubscribe, doOnSuccess, doOnTerminate, empty, error, error, filter, flatMap, flatMap, flatMap, flatMapCompletable, flatMapObservable, flatMapPublisher, flatMapSingle, flattenAsFlowable, flattenAsObservable, flattenStreamAsFlowable, flattenStreamAsObservable, fromAction, fromCallable, fromCompletable, fromCompletionStage, fromFuture, fromFuture, fromObservable, fromOptional, fromPublisher, fromRunnable, fromSingle, fromSupplier, hide, ignoreElement, isEmpty, just, lift, map, mapOptional, 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, onTerminateDetach, repeat, repeat, repeatUntil, repeatWhen, retry, retry, retry, retry, retry, retryUntil, retryWhen, safeSubscribe, sequenceEqual, sequenceEqual, startWith, startWith, startWith, startWith, startWith, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, switchIfEmpty, switchIfEmpty, switchOnNext, switchOnNextDelayError, takeUntil, takeUntil, test, test, timeInterval, timeInterval, timeInterval, timeInterval, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timer, timer, timestamp, timestamp, timestamp, timestamp, to, toCompletionStage, toCompletionStage, toFlowable, toFuture, toObservable, toSingle, unsafeCreate, unsubscribeOn, using, using, wrap, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipArray, zipWith
-
Method Details
-
create
Creates a fresh MaybeSubject.- Type Parameters:
T- the value type received and emitted- Returns:
- the new MaybeSubject instance
-
onSubscribe
Description copied from interface:MaybeObserverProvides theMaybeObserverwith the means of cancelling (disposing) the connection (channel) with theMaybein both synchronous (from withinonSubscribe(Disposable)itself) and asynchronous manner.- Specified by:
onSubscribein interfaceMaybeObserver<T>- Parameters:
d- theDisposableinstance whoseDisposable.dispose()can be called anytime to cancel the connection
-
onSuccess
Description copied from interface:MaybeObserverNotifies theMaybeObserverwith one item and that theMaybehas finished sending push-based notifications.The
Maybewill not call this method if it callsMaybeObserver.onError(Throwable).- Specified by:
onSuccessin interfaceMaybeObserver<T>- Parameters:
value- the item emitted by theMaybe
-
onError
Description copied from interface:MaybeObserverNotifies theMaybeObserverthat theMaybehas experienced an error condition.If the
Maybecalls this method, it will not thereafter callMaybeObserver.onSuccess(T).- Specified by:
onErrorin interfaceMaybeObserver<T>- Parameters:
e- the exception encountered by theMaybe
-
onComplete
public void onComplete()Description copied from interface:MaybeObserverCalled once the deferred computation completes normally.- Specified by:
onCompletein interfaceMaybeObserver<T>
-
subscribeActual
Description copied from class:MaybeImplement this method in subclasses to handle the incomingMaybeObservers.There is no need to call any of the plugin hooks on the current
Maybeinstance or theMaybeObserver; all hooks and basic safeguards have been applied byMaybe.subscribe(MaybeObserver)before this method gets called.- Specified by:
subscribeActualin classMaybe<T>- Parameters:
observer- theMaybeObserverto handle, notnull
-
getValue
-
hasValue
public boolean hasValue()Returns true if this MaybeSubject was terminated with a success value.- Returns:
- true if this MaybeSubject was terminated with a success value
-
getThrowable
-
hasThrowable
public boolean hasThrowable()Returns true if this MaybeSubject has been terminated with an error.- Returns:
- true if this MaybeSubject has been terminated with an error
-
hasComplete
public boolean hasComplete()Returns true if this MaybeSubject has been completed.- Returns:
- true if this MaybeSubject has been completed
-
hasObservers
public boolean hasObservers()Returns true if this MaybeSubject has observers.- Returns:
- true if this MaybeSubject has observers
-