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

public final Class SingleSubject<T>

extends Single<T>
implements SingleObserver<T>
Class Inheritance
All Implemented Interfaces
io.reactivex.rxjava3.core.SingleObserver
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 Single-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 SingleSubject can be created via the create() method.

Since the SingleSubject 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 SingleSubject is a io.reactivex.rxjava3.core.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 io.reactivex.rxjava3.plugins.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.

Scheduler:
SingleSubject does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the SingleObservers get notified on the thread where the terminating onSuccess or onError methods were invoked.
Error handling:
When the onError(Throwable) is called, the SingleSubject enters into a terminal state and emits the same Throwable instance to the last set of SingleObservers. During this emission, if one or more SingleObservers 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 SingleObservers cancel at once). If there were no SingleObservers 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

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 SingleSubject.SingleDisposable<T>[]
pack-priv Throwable
pack-priv final AtomicReference<SingleSubject.SingleDisposable<T>[]>
pack-priv final AtomicBoolean
pack-priv static final SingleSubject.SingleDisposable<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 SingleSubject<T>

Returns:

the new SingleSubject instance
create
()

Creates a fresh SingleSubject.

public @Nullable Throwable

Returns:

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

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

public T

Returns:

the success value or null
getValue
()

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

public boolean

Returns:

true if this SingleSubject has observers
hasObservers
()

Returns true if this SingleSubject has observers.

public boolean

Returns:

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

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

public boolean

Returns:

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

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

pack-priv int

Returns:

the number of current observers
observerCount
()

Returns the number of current observers.

public void
public void
public void
onSuccess(T
the item emitted by the Single
value
)

Implements io.reactivex.rxjava3.core.SingleObserver.onSuccess.

Notifies the SingleObserver with a single item and that the Single has finished sending push-based notifications.
pack-priv void
protected void
Inherited from io.reactivex.rxjava3.core.Single:
ambambArrayambWithblockingGetblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribecachecastcomposeconcatconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayDelayErrorconcatArrayEagerconcatArrayEagerDelayErrorconcatDelayErrorconcatDelayErrorconcatDelayErrorconcatEagerconcatEagerconcatEagerconcatEagerconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatMapconcatMapCompletableconcatMapMaybeconcatWithcontainscontainscreatedeferdelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedoAfterSuccessdoAfterTerminatedoFinallydoOnDisposedoOnErrordoOnEventdoOnLifecycledoOnSubscribedoOnSuccessdoOnTerminateerrorerrorfilterflatMapflatMapflatMapflatMapCompletableflatMapMaybeflatMapObservableflatMapPublisherflattenAsFlowableflattenAsObservableflattenStreamAsFlowableflattenStreamAsObservablefromCallablefromCompletionStagefromFuturefromFuturefromMaybefromMaybefromObservablefromPublisherfromSupplierhideignoreElementjustliftmapmapOptionalmaterializemergemergemergemergemergemergemergeArraymergeArrayDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithneverobserveOnofTypeonErrorCompleteonErrorCompleteonErrorResumeNextonErrorResumeWithonErrorReturnonErrorReturnItemonTerminateDetachrepeatrepeatrepeatUntilrepeatWhenretryretryretryretryretryretryUntilretryWhensafeSubscribesequenceEqualstartWithstartWithstartWithstartWithstartWithsubscribesubscribesubscribesubscribesubscribesubscribesubscribeActualsubscribeOnsubscribeWithswitchOnNextswitchOnNextDelayErrortakeUntiltakeUntiltakeUntiltesttesttimeIntervaltimeIntervaltimeIntervaltimeIntervaltimeouttimeouttimeouttimeouttimertimertimestamptimestamptimestamptimestamptotoCompletionStagetoFlowabletoFuturetoMaybetoObservableunsafeCreateunsubscribeOnusingusingwrapzipzipzipzipzipzipzipzipzipzipArrayzipWith

Field Detail

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

Constructor Detail

SingleSubjectback to summary
pack-priv SingleSubject()
Annotations
@SuppressWarnings:unchecked

Method Detail

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

Creates a fresh SingleSubject.

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

the new SingleSubject instance

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

Returns the terminal error if this SingleSubject 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 SingleSubject was terminated with a success value.

Returns:T

the success value or null

Annotations
@Nullable
hasObserversback to summary
public boolean hasObservers()

Returns true if this SingleSubject has observers.

Returns:boolean

true if this SingleSubject has observers

hasThrowableback to summary
public boolean hasThrowable()

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

Returns:boolean

true if this SingleSubject has been terminated with an error

hasValueback to summary
public boolean hasValue()

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

Returns:boolean

true if this SingleSubject 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

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

Implements io.reactivex.rxjava3.core.SingleObserver.onSuccess.

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

Notifies the SingleObserver with a single item and that the Single has finished sending push-based notifications.

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

Parameters
value:T

the item emitted by the Single

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

pack-priv final Class SingleSubject.SingleDisposable<T>

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

Field Summary

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

Constructor Summary

AccessConstructor and Description
pack-priv
SingleDisposable(SingleObserver<? super T> actual, SingleSubject<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 SingleObserver<? super T> downstream
serialVersionUIDback to summary
private static final long serialVersionUID

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

Constructor Detail

SingleDisposableback to summary
pack-priv SingleDisposable(SingleObserver<? super T> actual, SingleSubject<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