Class MaybeSubject<T>
- java.lang.Object
-
- io.reactivex.rxjava3.core.Maybe<T>
-
- io.reactivex.rxjava3.subjects.MaybeSubject<T>
-
- Type Parameters:
T
- the value type received and emitted
- All Implemented Interfaces:
MaybeObserver<T>
,MaybeSource<T>
public final class MaybeSubject<T> extends Maybe<T> implements MaybeObserver<T>
Represents a hot Maybe-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
MaybeSubject
can be created via thecreate()
method.Since the
MaybeSubject
is conceptionally derived from theProcessor
type in the Reactive Streams specification,null
s are not allowed (Rule 2.13) as parameters toonSuccess(Object)
andonError(Throwable)
. Such calls will result in aNullPointerException
being thrown and the subject's state is not changed.Since a
MaybeSubject
is aMaybe
, callingonSuccess
,onError
oronComplete
will move thisMaybeSubject
into its terminal state atomically.All methods are thread safe. Calling
onSuccess(Object)
oronComplete()
multiple times has no effect. CallingonError(Throwable)
multiple times relays theThrowable
to theRxJavaPlugins.onError(Throwable)
global error handler.Even though
MaybeSubject
implements theMaybeObserver
interface, callingonSubscribe
is not required (Rule 2.12) if the subject is used as a standalone source. However, callingonSubscribe
after theMaybeSubject
reached its terminal state will result in the givenDisposable
being disposed immediately.This
MaybeSubject
supports the standard state-peeking methodshasComplete()
,hasThrowable()
,getThrowable()
andhasObservers()
as well as means to read any success item in a non-blocking and thread-safe manner viahasValue()
andgetValue()
.The
MaybeSubject
does not support clearing its cachedonSuccess
value.- Scheduler:
MaybeSubject
does not operate by default on a particularScheduler
and theMaybeObserver
s get notified on the thread where the terminatingonSuccess
,onError
oronComplete
methods were invoked.- Error handling:
- When the
onError(Throwable)
is called, theMaybeSubject
enters into a terminal state and emits the sameThrowable
instance to the last set ofMaybeObserver
s. During this emission, if one or moreMaybeObserver
s dispose their respectiveDisposable
s, theThrowable
is delivered to the global error handler viaRxJavaPlugins.onError(Throwable)
(multiple times if multipleMaybeObserver
s cancel at once). If there were noMaybeObserver
s subscribed to thisMaybeSubject
when 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
MaybeSubject.MaybeDisposable<T>
-
Field Summary
Fields Modifier and Type Field Description (package private) static MaybeSubject.MaybeDisposable[]
EMPTY
(package private) java.lang.Throwable
error
(package private) java.util.concurrent.atomic.AtomicReference<MaybeSubject.MaybeDisposable<T>[]>
observers
(package private) java.util.concurrent.atomic.AtomicBoolean
once
(package private) static MaybeSubject.MaybeDisposable[]
TERMINATED
(package private) T
value
-
Constructor Summary
Constructors Constructor Description MaybeSubject()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
add(MaybeSubject.MaybeDisposable<T> inner)
static <T> @NonNull MaybeSubject<T>
create()
Creates a fresh MaybeSubject.@Nullable java.lang.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.(package private) int
observerCount()
Returns the number of current observers.void
onComplete()
Called once the deferred computation completes normally.void
onError(java.lang.Throwable e)
Notifies theMaybeObserver
that theMaybe
has experienced an error condition.void
onSubscribe(Disposable d)
Provides theMaybeObserver
with the means of cancelling (disposing) the connection (channel) with theMaybe
in both synchronous (from withinonSubscribe(Disposable)
itself) and asynchronous manner.void
onSuccess(T value)
Notifies theMaybeObserver
with one item and that theMaybe
has finished sending push-based notifications.(package private) void
remove(MaybeSubject.MaybeDisposable<T> inner)
protected void
subscribeActual(MaybeObserver<? super T> observer)
Implement this method in subclasses to handle the incomingMaybeObserver
s.-
Methods inherited from class io.reactivex.rxjava3.core.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
-
-
-
-
Field Detail
-
observers
final java.util.concurrent.atomic.AtomicReference<MaybeSubject.MaybeDisposable<T>[]> observers
-
EMPTY
static final MaybeSubject.MaybeDisposable[] EMPTY
-
TERMINATED
static final MaybeSubject.MaybeDisposable[] TERMINATED
-
once
final java.util.concurrent.atomic.AtomicBoolean once
-
value
T value
-
error
java.lang.Throwable error
-
-
Method Detail
-
create
@CheckReturnValue @NonNull public static <T> @NonNull MaybeSubject<T> create()
Creates a fresh MaybeSubject.- Type Parameters:
T
- the value type received and emitted- Returns:
- the new MaybeSubject instance
-
onSubscribe
public void onSubscribe(Disposable d)
Description copied from interface:MaybeObserver
Provides theMaybeObserver
with the means of cancelling (disposing) the connection (channel) with theMaybe
in both synchronous (from withinonSubscribe(Disposable)
itself) and asynchronous manner.- Specified by:
onSubscribe
in interfaceMaybeObserver<T>
- Parameters:
d
- theDisposable
instance whoseDisposable.dispose()
can be called anytime to cancel the connection
-
onSuccess
public void onSuccess(T value)
Description copied from interface:MaybeObserver
Notifies theMaybeObserver
with one item and that theMaybe
has finished sending push-based notifications.The
Maybe
will not call this method if it callsMaybeObserver.onError(java.lang.Throwable)
.- Specified by:
onSuccess
in interfaceMaybeObserver<T>
- Parameters:
value
- the item emitted by theMaybe
-
onError
public void onError(java.lang.Throwable e)
Description copied from interface:MaybeObserver
Notifies theMaybeObserver
that theMaybe
has experienced an error condition.If the
Maybe
calls this method, it will not thereafter callMaybeObserver.onSuccess(T)
.- Specified by:
onError
in interfaceMaybeObserver<T>
- Parameters:
e
- the exception encountered by theMaybe
-
onComplete
public void onComplete()
Description copied from interface:MaybeObserver
Called once the deferred computation completes normally.- Specified by:
onComplete
in interfaceMaybeObserver<T>
-
subscribeActual
protected void subscribeActual(MaybeObserver<? super T> observer)
Description copied from class:Maybe
Implement this method in subclasses to handle the incomingMaybeObserver
s.There is no need to call any of the plugin hooks on the current
Maybe
instance or theMaybeObserver
; all hooks and basic safeguards have been applied byMaybe.subscribe(MaybeObserver)
before this method gets called.- Specified by:
subscribeActual
in classMaybe<T>
- Parameters:
observer
- theMaybeObserver
to handle, notnull
-
add
boolean add(MaybeSubject.MaybeDisposable<T> inner)
-
remove
void remove(MaybeSubject.MaybeDisposable<T> inner)
-
getValue
@Nullable public T getValue()
Returns the success value if this MaybeSubject was terminated with a success value.- Returns:
- the success value or null
-
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
@Nullable public @Nullable java.lang.Throwable getThrowable()
Returns the terminal error if this MaybeSubject 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 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
-
observerCount
int observerCount()
Returns the number of current observers.- Returns:
- the number of current observers
-
-