Class SingleSubject<T>
- java.lang.Object
-
- io.reactivex.rxjava3.core.Single<T>
-
- io.reactivex.rxjava3.subjects.SingleSubject<T>
-
- Type Parameters:
T
- the value type received and emitted
- All Implemented Interfaces:
SingleObserver<T>
,SingleSource<T>
public final class SingleSubject<T> extends Single<T> implements SingleObserver<T>
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 thecreate()
method.Since the
SingleSubject
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
SingleSubject
is aSingle
, callingonSuccess
oronError
will move thisSingleSubject
into its terminal state atomically.All methods are thread safe. Calling
onSuccess(Object)
multiple times has no effect. CallingonError(Throwable)
multiple times relays theThrowable
to theRxJavaPlugins.onError(Throwable)
global error handler.Even though
SingleSubject
implements theSingleObserver
interface, callingonSubscribe
is not required (Rule 2.12) if the subject is used as a standalone source. However, callingonSubscribe
after theSingleSubject
reached its terminal state will result in the givenDisposable
being disposed immediately.This
SingleSubject
supports the standard state-peeking methodshasThrowable()
,getThrowable()
andhasObservers()
as well as means to read any success item in a non-blocking and thread-safe manner viahasValue()
andgetValue()
.The
SingleSubject
does not support clearing its cachedonSuccess
value.- Scheduler:
SingleSubject
does not operate by default on a particularScheduler
and theSingleObserver
s get notified on the thread where the terminatingonSuccess
oronError
methods were invoked.- Error handling:
- When the
onError(Throwable)
is called, theSingleSubject
enters into a terminal state and emits the sameThrowable
instance to the last set ofSingleObserver
s. During this emission, if one or moreSingleObserver
s dispose their respectiveDisposable
s, theThrowable
is delivered to the global error handler viaRxJavaPlugins.onError(Throwable)
(multiple times if multipleSingleObserver
s cancel at once). If there were noSingleObserver
s subscribed to thisSingleSubject
when theonError()
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 Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
SingleSubject.SingleDisposable<T>
-
Field Summary
Fields Modifier and Type Field Description (package private) static SingleSubject.SingleDisposable[]
EMPTY
(package private) java.lang.Throwable
error
(package private) java.util.concurrent.atomic.AtomicReference<SingleSubject.SingleDisposable<T>[]>
observers
(package private) java.util.concurrent.atomic.AtomicBoolean
once
(package private) static SingleSubject.SingleDisposable[]
TERMINATED
(package private) T
value
-
Constructor Summary
Constructors Constructor Description SingleSubject()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
add(@NonNull SingleSubject.SingleDisposable<T> inner)
static <T> @NonNull SingleSubject<T>
create()
Creates a fresh SingleSubject.@Nullable java.lang.Throwable
getThrowable()
Returns the terminal error if this SingleSubject has been terminated with an error, null otherwise.T
getValue()
Returns the success value if this SingleSubject was terminated with a success value.boolean
hasObservers()
Returns true if this SingleSubject has observers.boolean
hasThrowable()
Returns true if this SingleSubject has been terminated with an error.boolean
hasValue()
Returns true if this SingleSubject was terminated with a success value.(package private) int
observerCount()
Returns the number of current observers.void
onError(@NonNull java.lang.Throwable e)
Notifies theSingleObserver
that theSingle
has experienced an error condition.void
onSubscribe(@NonNull Disposable d)
Provides theSingleObserver
with the means of cancelling (disposing) the connection (channel) with the Single in both synchronous (from withinonSubscribe(Disposable)
itself) and asynchronous manner.void
onSuccess(T value)
Notifies theSingleObserver
with a single item and that theSingle
has finished sending push-based notifications.(package private) void
remove(@NonNull SingleSubject.SingleDisposable<T> inner)
protected void
subscribeActual(@NonNull SingleObserver<? super T> observer)
Implement this method in subclasses to handle the incomingSingleObserver
s.-
Methods inherited from class io.reactivex.rxjava3.core.Single
amb, ambArray, ambWith, blockingGet, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, cache, cast, compose, concat, concat, concat, concat, concat, concat, concat, concatArray, concatArrayDelayError, concatArrayEager, concatArrayEagerDelayError, concatDelayError, concatDelayError, concatDelayError, concatEager, concatEager, concatEager, concatEager, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatMap, concatMapCompletable, concatMapMaybe, concatWith, contains, contains, create, defer, delay, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, delaySubscription, delaySubscription, delaySubscription, dematerialize, doAfterSuccess, doAfterTerminate, doFinally, doOnDispose, doOnError, doOnEvent, doOnLifecycle, doOnSubscribe, doOnSuccess, doOnTerminate, error, error, filter, flatMap, flatMap, flatMap, flatMapCompletable, flatMapMaybe, flatMapObservable, flatMapPublisher, flattenAsFlowable, flattenAsObservable, flattenStreamAsFlowable, flattenStreamAsObservable, fromCallable, fromCompletionStage, fromFuture, fromFuture, fromMaybe, fromMaybe, fromObservable, fromPublisher, fromSupplier, hide, ignoreElement, just, lift, map, mapOptional, materialize, merge, merge, merge, merge, merge, merge, mergeArray, mergeArrayDelayError, 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, startWith, startWith, startWith, startWith, startWith, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, switchOnNext, switchOnNextDelayError, takeUntil, takeUntil, takeUntil, test, test, timeInterval, timeInterval, timeInterval, timeInterval, timeout, timeout, timeout, timeout, timer, timer, timestamp, timestamp, timestamp, timestamp, to, toCompletionStage, toFlowable, toFuture, toMaybe, toObservable, 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<SingleSubject.SingleDisposable<T>[]> observers
-
EMPTY
static final SingleSubject.SingleDisposable[] EMPTY
-
TERMINATED
static final SingleSubject.SingleDisposable[] 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 SingleSubject<T> create()
Creates a fresh SingleSubject.- Type Parameters:
T
- the value type received and emitted- Returns:
- the new SingleSubject instance
-
onSubscribe
public void onSubscribe(@NonNull @NonNull Disposable d)
Description copied from interface:SingleObserver
Provides theSingleObserver
with the means of cancelling (disposing) the connection (channel) with the Single in both synchronous (from withinonSubscribe(Disposable)
itself) and asynchronous manner.- Specified by:
onSubscribe
in interfaceSingleObserver<T>
- Parameters:
d
- the Disposable instance whoseDisposable.dispose()
can be called anytime to cancel the connection
-
onSuccess
public void onSuccess(@NonNull T value)
Description copied from interface:SingleObserver
Notifies theSingleObserver
with a single item and that theSingle
has finished sending push-based notifications.The
Single
will not call this method if it callsSingleObserver.onError(java.lang.Throwable)
.- Specified by:
onSuccess
in interfaceSingleObserver<T>
- Parameters:
value
- the item emitted by theSingle
-
onError
public void onError(@NonNull @NonNull java.lang.Throwable e)
Description copied from interface:SingleObserver
Notifies theSingleObserver
that theSingle
has experienced an error condition.If the
Single
calls this method, it will not thereafter callSingleObserver.onSuccess(T)
.- Specified by:
onError
in interfaceSingleObserver<T>
- Parameters:
e
- the exception encountered by theSingle
-
subscribeActual
protected void subscribeActual(@NonNull @NonNull SingleObserver<? super T> observer)
Description copied from class:Single
Implement this method in subclasses to handle the incomingSingleObserver
s.There is no need to call any of the plugin hooks on the current
Single
instance or theSingleObserver
; all hooks and basic safeguards have been applied bySingle.subscribe(SingleObserver)
before this method gets called.- Specified by:
subscribeActual
in classSingle<T>
- Parameters:
observer
- theSingleObserver
to handle, notnull
-
add
boolean add(@NonNull @NonNull SingleSubject.SingleDisposable<T> inner)
-
remove
void remove(@NonNull @NonNull SingleSubject.SingleDisposable<T> inner)
-
getValue
@Nullable public T getValue()
Returns the success value if this SingleSubject was terminated with a success value.- Returns:
- the success value or null
-
hasValue
public boolean hasValue()
Returns true if this SingleSubject was terminated with a success value.- Returns:
- true if this SingleSubject was terminated with a success value
-
getThrowable
@Nullable public @Nullable java.lang.Throwable getThrowable()
Returns the terminal error if this SingleSubject 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 SingleSubject has been terminated with an error.- Returns:
- true if this SingleSubject has been terminated with an error
-
hasObservers
public boolean hasObservers()
Returns true if this SingleSubject has observers.- Returns:
- true if this SingleSubject has observers
-
observerCount
int observerCount()
Returns the number of current observers.- Returns:
- the number of current observers
-
-