Top Description Inners Fields Constructors Methods
io.reactivex.subjects

public final Class SingleSubject<T>

extends Single<T>
implements SingleObserver<T>
Class Inheritance
All Implemented Interfaces
io.reactivex.SingleObserver
Type Parameters
<T>
the value type received and emitted
Imports
java.util.concurrent.atomic.*, io.reactivex.*, io.reactivex.annotations.*, io.reactivex.disposables.Disposable, io.reactivex.internal.functions.ObjectHelper, io.reactivex.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.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.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.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.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
>
SingleSubject<T>

Returns:

the new SingleSubject instance
create
()

Creates a fresh SingleSubject.

public 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
onError(Throwable
the exception encountered by the Single
e
)

Implements io.reactivex.SingleObserver.onError.

Notifies the SingleObserver that the Single has experienced an error condition.
public void
onSubscribe(Disposable
the Disposable instance whose Disposable#dispose() can be called anytime to cancel the connection
d
)

Implements io.reactivex.SingleObserver.onSubscribe.

Provides the SingleObserver with the means of cancelling (disposing) the connection (channel) with the Single in both synchronous (from within onSubscribe(Disposable) itself) and asynchronous manner.
public void
onSuccess(T
the item emitted by the Single
value
)

Implements io.reactivex.SingleObserver.onSuccess.

Notifies the SingleObserver with a single item and that the Single has finished sending push-based notifications.
pack-priv void
protected void
subscribeActual(SingleObserver<? super T>
the SingleObserver to handle, not null
observer
)

Implements abstract io.reactivex.Single.subscribeActual.

Implement this method in subclasses to handle the incoming SingleObservers.
Inherited from io.reactivex.Single:
ambambArrayambWithasblockingGetcachecastcomposeconcatconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayEagerconcatEagerconcatEagerconcatWithcontainscontainscreatedeferdelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedoAfterSuccessdoAfterTerminatedoFinallydoOnDisposedoOnErrordoOnEventdoOnSubscribedoOnSuccessdoOnTerminateequalserrorerrorfilterflatMapflatMapCompletableflatMapMaybeflatMapObservableflatMapPublisherflattenAsFlowableflattenAsObservablefromCallablefromFuturefromFuturefromFuturefromFuturefromObservablefromPublisherhideignoreElementjustliftmapmaterializemergemergemergemergemergemergemergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithneverobserveOnonErrorResumeNextonErrorResumeNextonErrorReturnonErrorReturnItemonTerminateDetachrepeatrepeatrepeatUntilrepeatWhenretryretryretryretryretryretryWhensubscribesubscribesubscribesubscribesubscribesubscribeOnsubscribeWithtakeUntiltakeUntiltakeUntiltesttesttimeouttimeouttimeouttimeouttimertimertotoCompletabletoFlowabletoFuturetoMaybetoObservableunsafeCreateunsubscribeOnusingusingwrapzipzipzipzipzipzipzipzipzipzipArrayzipWith

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(SingleSubject.SingleDisposable<T> inner)
createback to summary
public static <T> SingleSubject<T> create()

Creates a fresh SingleSubject.

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

the new SingleSubject instance

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

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

Returns: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(Throwable e)

Implements io.reactivex.SingleObserver.onError.

Doc from io.reactivex.SingleObserver.onError.

Notifies the SingleObserver that the Single has experienced an error condition.

If the Single calls this method, it will not thereafter call onSuccess.

Parameters
e:Throwable

the exception encountered by the Single

Annotations
@SuppressWarnings:unchecked
@Override
onSubscribeback to summary
public void onSubscribe(Disposable d)

Implements io.reactivex.SingleObserver.onSubscribe.

Doc from io.reactivex.SingleObserver.onSubscribe.

Provides the SingleObserver with the means of cancelling (disposing) the connection (channel) with the Single in both synchronous (from within onSubscribe(Disposable) itself) and asynchronous manner.

Parameters
d:Disposable

the Disposable instance whose Disposable#dispose() can be called anytime to cancel the connection

Annotations
@Override
onSuccessback to summary
public void onSuccess(T value)

Implements io.reactivex.SingleObserver.onSuccess.

Doc from io.reactivex.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(SingleSubject.SingleDisposable<T> inner)
Annotations
@SuppressWarnings:unchecked
subscribeActualback to summary
protected void subscribeActual(SingleObserver<? super T> observer)

Implements abstract io.reactivex.Single.subscribeActual.

Doc from io.reactivex.Single.subscribeActual.

Implement this method in subclasses to handle the incoming SingleObservers.

There is no need to call any of the plugin hooks on the current Single instance or the SingleObserver; all hooks and basic safeguards have been applied by subscribe(SingleObserver) before this method gets called.

Parameters
observer:SingleObserver<? super T>

the SingleObserver to handle, not null

Annotations
@Override
io.reactivex.subjects back to summary

pack-priv final Class SingleSubject.SingleDisposable<T>

extends AtomicReference<SingleSubject<T>>
implements Disposable
Class Inheritance
All Implemented Interfaces
io.reactivex.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.disposables.Disposable.dispose.

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

Implements io.reactivex.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.disposables.Disposable.dispose.

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

Dispose the resource, the operation should be idempotent.

Annotations
@Override
isDisposedback to summary
public boolean isDisposed()

Implements io.reactivex.disposables.Disposable.isDisposed.

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

Returns true if this resource has been disposed.

Returns:boolean

true if this resource has been disposed

Annotations
@Override