Top Description Inners Fields Constructors Methods
io.reactivex.rxjava3.subjects

public final Class MaybeSubject<T>

extends Maybe<T>
implements MaybeObserver<T>
Class Inheritance
All Implemented Interfaces
io.reactivex.rxjava3.core.MaybeObserver
Type Parameters
<T>
the value type received and emitted
Imports
java.util.concurrent.atomic.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.*, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.internal.util.ExceptionHelper, io.reactivex.rxjava3.plugins.RxJavaPlugins

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 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 io.reactivex.rxjava3.core.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 io.reactivex.rxjava3.plugins.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:
MaybeSubject does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the MaybeObservers get notified on the thread where the terminating onSuccess, onError or onComplete methods were invoked.
Error handling:
When the onError(Throwable) is called, the MaybeSubject enters into a terminal state and emits the same Throwable instance to the last set of MaybeObservers. During this emission, if one or more MaybeObservers dispose their respective Disposables, the Throwable is delivered to the global error handler via io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) (multiple times if multiple MaybeObservers cancel at once). If there were no MaybeObservers 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

Since
2.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final MaybeSubject.MaybeDisposable<T>[]
pack-priv Throwable
pack-priv final AtomicReference<MaybeSubject.MaybeDisposable<T>[]>
pack-priv final AtomicBoolean
pack-priv static final MaybeSubject.MaybeDisposable<T>[]
pack-priv T

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
public static <
the value type received and emitted
T
>
@NonNull MaybeSubject<T>

Returns:

the new MaybeSubject instance
create
()

Creates a fresh MaybeSubject.

public @Nullable Throwable

Returns:

the terminal error or null if not terminated or not with an error
getThrowable
()

Returns the terminal error if this MaybeSubject has been terminated with an error, null otherwise.

public T

Returns:

the success value or null
getValue
()

Returns the success value if this MaybeSubject was terminated with a success value.

public boolean

Returns:

true if this MaybeSubject has been completed
hasComplete
()

Returns true if this MaybeSubject has been completed.

public boolean

Returns:

true if this MaybeSubject has observers
hasObservers
()

Returns true if this MaybeSubject has observers.

public boolean

Returns:

true if this MaybeSubject has been terminated with an error
hasThrowable
()

Returns true if this MaybeSubject has been terminated with an error.

public boolean

Returns:

true if this MaybeSubject was terminated with a success value
hasValue
()

Returns true if this MaybeSubject was terminated with a success value.

pack-priv int

Returns:

the number of current observers
observerCount
()

Returns the number of current observers.

public void
onComplete()

Implements io.reactivex.rxjava3.core.MaybeObserver.onComplete.

Called once the deferred computation completes normally.
public void
public void
public void
onSuccess(T
the item emitted by the Maybe
value
)

Implements io.reactivex.rxjava3.core.MaybeObserver.onSuccess.

Notifies the MaybeObserver with one item and that the Maybe has finished sending push-based notifications.
pack-priv void
protected void
subscribeActual(MaybeObserver<? super T> observer)

Inherited from io.reactivex.rxjava3.core.Maybe:
ambambArrayambWithblockingGetblockingGetblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribecachecastcomposeconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayDelayErrorconcatArrayEagerconcatArrayEagerDelayErrorconcatDelayErrorconcatDelayErrorconcatDelayErrorconcatEagerconcatEagerconcatEagerconcatEagerconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatMapconcatMapCompletableconcatMapSingleconcatWithcontainscountcreatedefaultIfEmptydeferdelaydelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedoAfterSuccessdoAfterTerminatedoFinallydoOnCompletedoOnDisposedoOnErrordoOnEventdoOnLifecycledoOnSubscribedoOnSuccessdoOnTerminateemptyerrorerrorfilterflatMapflatMapflatMapflatMapCompletableflatMapObservableflatMapPublisherflatMapSingleflattenAsFlowableflattenAsObservableflattenStreamAsFlowableflattenStreamAsObservablefromActionfromCallablefromCompletablefromCompletionStagefromFuturefromFuturefromObservablefromOptionalfromPublisherfromRunnablefromSinglefromSupplierhideignoreElementisEmptyjustliftmapmapOptionalmaterializemergemergemergemergemergemergemergemergeArraymergeArrayDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithneverobserveOnofTypeonErrorCompleteonErrorCompleteonErrorResumeNextonErrorResumeWithonErrorReturnonErrorReturnItemonTerminateDetachrepeatrepeatrepeatUntilrepeatWhenretryretryretryretryretryretryUntilretryWhensafeSubscribesequenceEqualsequenceEqualstartWithstartWithstartWithstartWithstartWithsubscribesubscribesubscribesubscribesubscribesubscribesubscribeActualsubscribeOnsubscribeWithswitchIfEmptyswitchIfEmptyswitchOnNextswitchOnNextDelayErrortakeUntiltakeUntiltesttesttimeIntervaltimeIntervaltimeIntervaltimeIntervaltimeouttimeouttimeouttimeouttimeouttimeouttimeouttimeouttimertimertimestamptimestamptimestamptimestamptotoCompletionStagetoCompletionStagetoFlowabletoFuturetoObservabletoSingleunsafeCreateunsubscribeOnusingusingwrapzipzipzipzipzipzipzipzipzipzipArrayzipWith

Field Detail

EMPTYback to summary
pack-priv static final MaybeSubject.MaybeDisposable<T>[] EMPTY
Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error
observersback to summary
pack-priv final AtomicReference<MaybeSubject.MaybeDisposable<T>[]> observers
onceback to summary
pack-priv final AtomicBoolean once
TERMINATEDback to summary
pack-priv static final MaybeSubject.MaybeDisposable<T>[] TERMINATED
Annotations
@SuppressWarnings:rawtypes
valueback to summary
pack-priv T value

Constructor Detail

MaybeSubjectback to summary
pack-priv MaybeSubject()
Annotations
@SuppressWarnings:unchecked

Method Detail

addback to summary
pack-priv boolean add(MaybeSubject.MaybeDisposable<T> inner)
createback to summary
public static <T> @NonNull MaybeSubject<T> create()

Creates a fresh MaybeSubject.

Parameters
<T>
the value type received and emitted
Returns:@NonNull MaybeSubject<T>

the new MaybeSubject instance

Annotations
@CheckReturnValue
@NonNull
getThrowableback to summary
public @Nullable Throwable getThrowable()

Returns the terminal error if this MaybeSubject has been terminated with an error, null otherwise.

Returns:@Nullable Throwable

the terminal error or null if not terminated or not with an error

Annotations
@Nullable
getValueback to summary
public T getValue()

Returns the success value if this MaybeSubject was terminated with a success value.

Returns:T

the success value or null

Annotations
@Nullable
hasCompleteback to summary
public boolean hasComplete()

Returns true if this MaybeSubject has been completed.

Returns:boolean

true if this MaybeSubject has been completed

hasObserversback to summary
public boolean hasObservers()

Returns true if this MaybeSubject has observers.

Returns:boolean

true if this MaybeSubject has observers

hasThrowableback to summary
public boolean hasThrowable()

Returns true if this MaybeSubject has been terminated with an error.

Returns:boolean

true if this MaybeSubject has been terminated with an error

hasValueback to summary
public boolean hasValue()

Returns true if this MaybeSubject was terminated with a success value.

Returns:boolean

true if this MaybeSubject was terminated with a success value

observerCountback to summary
pack-priv int observerCount()

Returns the number of current observers.

Returns:int

the number of current observers

onCompleteback to summary
public void onComplete()

Implements io.reactivex.rxjava3.core.MaybeObserver.onComplete.

Doc from io.reactivex.rxjava3.core.MaybeObserver.onComplete.

Called once the deferred computation completes normally.

Annotations
@SuppressWarnings:unchecked
@Override
onErrorback to summary
public void onError(Throwable e)
Annotations
@SuppressWarnings:unchecked
@Override
onSubscribeback to summary
public void onSubscribe(Disposable d)
Annotations
@Override
onSuccessback to summary
public void onSuccess(T value)

Implements io.reactivex.rxjava3.core.MaybeObserver.onSuccess.

Doc from io.reactivex.rxjava3.core.MaybeObserver.onSuccess.

Notifies the MaybeObserver with one item and that the Maybe has finished sending push-based notifications.

The Maybe will not call this method if it calls onError.

Parameters
value:T

the item emitted by the Maybe

Annotations
@SuppressWarnings:unchecked
@Override
removeback to summary
pack-priv void remove(MaybeSubject.MaybeDisposable<T> inner)
Annotations
@SuppressWarnings:unchecked
subscribeActualback to summary
protected void subscribeActual(MaybeObserver<? super T> observer)
Annotations
@Override
io.reactivex.rxjava3.subjects back to summary

pack-priv final Class MaybeSubject.MaybeDisposable<T>

extends AtomicReference<MaybeSubject<T>>
implements Disposable
Class Inheritance
All Implemented Interfaces
io.reactivex.rxjava3.disposables.Disposable

Field Summary

Modifier and TypeField and Description
pack-priv final MaybeObserver<? super T>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
MaybeDisposable(MaybeObserver<? super T> actual, MaybeSubject<T> parent)

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.
public boolean
isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.
Inherited from java.util.concurrent.atomic.AtomicReference:
accumulateAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetgetgetAcquiregetAndAccumulategetAndSetgetAndUpdategetOpaquegetPlainlazySetsetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

downstreamback to summary
pack-priv final MaybeObserver<? super T> downstream
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.atomic.AtomicReference.serialVersionUID.

Constructor Detail

MaybeDisposableback to summary
pack-priv MaybeDisposable(MaybeObserver<? super T> actual, MaybeSubject<T> parent)

Method Detail

disposeback to summary
public void dispose()

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Doc from io.reactivex.rxjava3.disposables.Disposable.isDisposed.

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override