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

public final Class UnicastSubject<T>

extends Subject<T>
Class Inheritance
Type Parameters
<T>
the value type received and emitted by this Subject subclass
Imports
java.util.Objects, java.util.concurrent.atomic.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.Observer, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.internal.disposables.EmptyDisposable, io.reactivex.rxjava3.internal.functions.*, io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable, io.reactivex.rxjava3.internal.util.ExceptionHelper, io.reactivex.rxjava3.operators.SimpleQueue, .SpscLinkedArrayQueue, io.reactivex.rxjava3.plugins.RxJavaPlugins

A Subject that queues up events until a single Observer subscribes to it, replays those events to it until the Observer catches up and then switches to relaying events live to this single Observer until this UnicastSubject terminates or the Observer disposes.

Note that UnicastSubject holds an unbounded internal buffer.

This subject does not have a public constructor by design; a new empty instance of this UnicastSubject can be created via the following create methods that allow specifying the retention policy for items:

If more than one Observer attempts to subscribe to this UnicastSubject, they will receive an IllegalStateException indicating the single-use-only nature of this UnicastSubject, even if the UnicastSubject already terminated with an error.

Since a Subject is conceptionally derived from the Processor type in the Reactive Streams specification, nulls are not allowed (Rule 2.13) as parameters to onNext(Object) and onError(Throwable). Such calls will result in a NullPointerException being thrown and the subject's state is not changed.

Since a UnicastSubject is an io.reactivex.rxjava3.core.Observable, it does not support backpressure.

When this UnicastSubject is terminated via onError(Throwable) the current or late single Observer may receive the Throwable before any available items could be emitted. To make sure an onError event is delivered to the Observer after the normal items, create a UnicastSubject with the create(boolean) or create(int, Runnable, boolean) factory methods.

Even though UnicastSubject implements the Observer interface, calling onSubscribe is not required (Rule 2.12) if the subject is used as a standalone source. However, calling onSubscribe after the UnicastSubject reached its terminal state will result in the given Disposable being disposed immediately.

Calling onNext(Object), onError(Throwable) and onComplete() is required to be serialized (called from the same thread or called non-overlappingly from different threads through external means of serialization). The toSerialized() method available to all Subjects provides such serialization and also protects against reentrance (i.e., when a downstream Observer consuming this subject also wants to call onNext(Object) on this subject recursively).

This UnicastSubject supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasObservers().

Scheduler:
UnicastSubject does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the single Observer gets notified on the thread the respective onXXX methods were invoked.
Error handling:
When the onError(Throwable) is called, the UnicastSubject enters into a terminal state and emits the same Throwable instance to the current single Observer. During this emission, if the single Observers disposes its respective Disposable, the Throwable is delivered to the global error handler via io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable). If there were no Observers subscribed to this UnicastSubject when the onError() was called, the global error handler is not invoked.

Example usage:


UnicastSubject<Integer> subject = UnicastSubject.create();

TestObserver<Integer> to1 = subject.test();

// fresh UnicastSubjects are empty
to1.assertEmpty();

TestObserver<Integer> to2 = subject.test();

// A UnicastSubject only allows one Observer during its lifetime
to2.assertFailure(IllegalStateException.class);

subject.onNext(1);
to1.assertValue(1);

subject.onNext(2);
to1.assertValues(1, 2);

subject.onComplete();
to1.assertResult(1, 2);

// ----------------------------------------------------

UnicastSubject<Integer> subject2 = UnicastSubject.create();

// a UnicastSubject caches events until its single Observer subscribes
subject2.onNext(1);
subject2.onNext(2);
subject2.onComplete();

TestObserver<Integer> to3 = subject2.test();

// the cached events are emitted in order
to3.assertResult(1, 2);
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
delayError

deliver onNext events before error event.

pack-priv volatile boolean
disposed

Indicates the single observer has cancelled.

pack-priv volatile boolean
done

Indicates the source has terminated.

pack-priv final AtomicReference<Observer<? super T>>
downstream

The single Observer.

pack-priv boolean
pack-priv Throwable
error

The terminal error if not null.

pack-priv final AtomicBoolean
once

Set to 1 atomically for the first and only Subscriber.

pack-priv final AtomicReference<Runnable>
onTerminate

The optional callback when the Subject gets cancelled or terminates.

pack-priv final SpscLinkedArrayQueue<T>
queue

The queue that buffers the source events.

pack-priv final BasicIntQueueDisposable<T>
wip

The wip counter and QueueDisposable surface.

Constructor Summary

AccessConstructor and Description
pack-priv
UnicastSubject(int
the capacity hint for the internal, unbounded queue
capacityHint
,
Runnable
the callback to run when the Subject is terminated or cancelled, null not allowed
onTerminate
,
boolean
deliver pending onNext events before onError
delayError
)

Creates an UnicastSubject with the given capacity hint, delay error flag and callback for when the Subject is terminated normally or its single Subscriber cancels.

Method Summary

Modifier and TypeMethod and Description
public static <
the value type
T
>
@NonNull UnicastSubject<T>

Returns:

an UnicastSubject instance
create
()

Creates an UnicastSubject with an internal buffer capacity hint 16.

public static <
the value type
T
>
@NonNull UnicastSubject<T>

Returns:

an UnicastSubject instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
)

Creates an UnicastSubject with the given internal buffer capacity hint.

public static <
the value type
T
>
@NonNull UnicastSubject<T>

Returns:

an UnicastSubject instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
,
@NonNull Runnable
the callback to run when the Subject is terminated or cancelled, null not allowed
onTerminate
)

Creates an UnicastSubject with the given internal buffer capacity hint and a callback for the case when the single Subscriber cancels its subscription or the subject is terminated.

public static <
the value type
T
>
@NonNull UnicastSubject<T>

Returns:

an UnicastSubject instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
,
@NonNull Runnable
the callback to run when the Subject is terminated or cancelled, null not allowed
onTerminate
,
boolean
deliver pending onNext events before onError
delayError
)

Creates an UnicastSubject with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Observer disposes its Disposable or the subject is terminated.

public static <
the value type
T
>
@NonNull UnicastSubject<T>

Returns:

an UnicastSubject instance
create
(boolean
deliver pending onNext events before onError
delayError
)

Creates an UnicastSubject with an internal buffer capacity hint 16 and given delay error flag.

pack-priv void
pack-priv void
pack-priv void
drainFused(Observer<? super T> a)

pack-priv void
drainNormal(Observer<? super T> a)

pack-priv void
errorOrComplete(Observer<? super T> a)

pack-priv boolean
failedFast(final SimpleQueue<T> q, Observer<? super T> a)

public @Nullable Throwable
getThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.getThrowable.

Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.
public boolean
hasComplete()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasComplete.

Returns true if the subject has reached a terminal state through a complete event.
public boolean
hasObservers()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasObservers.

Returns true if the subject has any Observers.
public boolean
hasThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Returns true if the subject has reached a terminal state through an error event.
public void
onComplete()

Implements io.reactivex.rxjava3.core.Observer.onComplete.

Notifies the Observer that the Observable has finished sending push-based notifications.
public void
public void
onNext(T
the item emitted by the Observable
t
)

Implements io.reactivex.rxjava3.core.Observer.onNext.

Provides the Observer with a new item to observe.
public void
protected void
subscribeActual(Observer<? super T> observer)

Inherited from io.reactivex.rxjava3.subjects.Subject:
toSerialized

Field Detail

delayErrorback to summary
pack-priv final boolean delayError

deliver onNext events before error event.

disposedback to summary
pack-priv volatile boolean disposed

Indicates the single observer has cancelled.

doneback to summary
pack-priv volatile boolean done

Indicates the source has terminated.

downstreamback to summary
pack-priv final AtomicReference<Observer<? super T>> downstream

The single Observer.

enableOperatorFusionback to summary
pack-priv boolean enableOperatorFusion
errorback to summary
pack-priv Throwable error

The terminal error if not null. Must be set before writing to done and read after done == true.

onceback to summary
pack-priv final AtomicBoolean once

Set to 1 atomically for the first and only Subscriber.

onTerminateback to summary
pack-priv final AtomicReference<Runnable> onTerminate

The optional callback when the Subject gets cancelled or terminates.

queueback to summary
pack-priv final SpscLinkedArrayQueue<T> queue

The queue that buffers the source events.

wipback to summary
pack-priv final BasicIntQueueDisposable<T> wip

The wip counter and QueueDisposable surface.

Constructor Detail

UnicastSubjectback to summary
pack-priv UnicastSubject(int capacityHint, Runnable onTerminate, boolean delayError)

Creates an UnicastSubject with the given capacity hint, delay error flag and callback for when the Subject is terminated normally or its single Subscriber cancels.

History: 2.0.8 - experimental

Parameters
capacityHint:int

the capacity hint for the internal, unbounded queue

onTerminate:Runnable

the callback to run when the Subject is terminated or cancelled, null not allowed

delayError:boolean

deliver pending onNext events before onError

Since
2.2

Method Detail

createback to summary
public static <T> @NonNull UnicastSubject<T> create()

Creates an UnicastSubject with an internal buffer capacity hint 16.

Parameters
<T>
the value type
Returns:@NonNull UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> @NonNull UnicastSubject<T> create(int capacityHint)

Creates an UnicastSubject with the given internal buffer capacity hint.

Parameters
<T>
the value type
capacityHint:int

the hint to size the internal unbounded buffer

Returns:@NonNull UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
IllegalArgumentException:
if capacityHint is non-positive
createback to summary
public static <T> @NonNull UnicastSubject<T> create(int capacityHint, @NonNull Runnable onTerminate)

Creates an UnicastSubject with the given internal buffer capacity hint and a callback for the case when the single Subscriber cancels its subscription or the subject is terminated.

The callback, if not null, is called exactly once and non-overlapped with any active replay.

Parameters
<T>
the value type
capacityHint:int

the hint to size the internal unbounded buffer

onTerminate:@NonNull Runnable

the callback to run when the Subject is terminated or cancelled, null not allowed

Returns:@NonNull UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
NullPointerException:
if onTerminate is null
IllegalArgumentException:
if capacityHint is non-positive
createback to summary
public static <T> @NonNull UnicastSubject<T> create(int capacityHint, @NonNull Runnable onTerminate, boolean delayError)

Creates an UnicastSubject with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Observer disposes its Disposable or the subject is terminated.

The callback, if not null, is called exactly once and non-overlapped with any active replay.

History: 2.0.8 - experimental

Parameters
<T>
the value type
capacityHint:int

the hint to size the internal unbounded buffer

onTerminate:@NonNull Runnable

the callback to run when the Subject is terminated or cancelled, null not allowed

delayError:boolean

deliver pending onNext events before onError

Returns:@NonNull UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
NullPointerException:
if onTerminate is null
IllegalArgumentException:
if capacityHint is non-positive
Since
2.2
createback to summary
public static <T> @NonNull UnicastSubject<T> create(boolean delayError)

Creates an UnicastSubject with an internal buffer capacity hint 16 and given delay error flag.

The callback, if not null, is called exactly once and non-overlapped with any active replay.

History: 2.0.8 - experimental

Parameters
<T>
the value type
delayError:boolean

deliver pending onNext events before onError

Returns:@NonNull UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
Since
2.2
doTerminateback to summary
pack-priv void doTerminate()
drainback to summary
pack-priv void drain()
drainFusedback to summary
pack-priv void drainFused(Observer<? super T> a)
drainNormalback to summary
pack-priv void drainNormal(Observer<? super T> a)
errorOrCompleteback to summary
pack-priv void errorOrComplete(Observer<? super T> a)
failedFastback to summary
pack-priv boolean failedFast(final SimpleQueue<T> q, Observer<? super T> a)
getThrowableback to summary
public @Nullable Throwable getThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.getThrowable.

Doc from io.reactivex.rxjava3.subjects.Subject.getThrowable.

Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.

The method is thread-safe.

Returns:@Nullable Throwable

the error that caused the Subject to terminate or null if the Subject hasn't terminated yet

Annotations
@Override
@Nullable
@CheckReturnValue
hasCompleteback to summary
public boolean hasComplete()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasComplete.

Doc from io.reactivex.rxjava3.subjects.Subject.hasComplete.

Returns true if the subject has reached a terminal state through a complete event.

The method is thread-safe.

Returns:boolean

true if the subject has reached a terminal state through a complete event

Annotations
@Override
@CheckReturnValue
hasObserversback to summary
public boolean hasObservers()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasObservers.

Doc from io.reactivex.rxjava3.subjects.Subject.hasObservers.

Returns true if the subject has any Observers.

The method is thread-safe.

Returns:boolean

true if the subject has any Observers

Annotations
@Override
@CheckReturnValue
hasThrowableback to summary
public boolean hasThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Doc from io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Returns true if the subject has reached a terminal state through an error event.

The method is thread-safe.

Returns:boolean

true if the subject has reached a terminal state through an error event

Annotations
@Override
@CheckReturnValue
onCompleteback to summary
public void onComplete()

Implements io.reactivex.rxjava3.core.Observer.onComplete.

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

Notifies the Observer that the Observable has finished sending push-based notifications.

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

Annotations
@Override
onErrorback to summary
public void onError(Throwable t)
Annotations
@Override
onNextback to summary
public void onNext(T t)

Implements io.reactivex.rxjava3.core.Observer.onNext.

Doc from io.reactivex.rxjava3.core.Observer.onNext.

Provides the Observer with a new item to observe.

The Observable may call this method 0 or more times.

The Observable will not call this method again after it calls either onComplete or onError.

Parameters
t:T

the item emitted by the Observable

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Disposable d)
Annotations
@Override
subscribeActualback to summary
protected void subscribeActual(Observer<? super T> observer)
Annotations
@Override
io.reactivex.rxjava3.subjects back to summary

pack-priv final Class UnicastSubject.UnicastQueueDisposable

extends BasicIntQueueDisposable<T>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements io.reactivex.rxjava3.operators.SimpleQueue.clear.

Removes all enqueued items from this queue.
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.
public boolean
isEmpty()

Implements io.reactivex.rxjava3.operators.SimpleQueue.isEmpty.

Returns true if the queue is empty.
public T
poll()

Implements io.reactivex.rxjava3.operators.SimpleQueue.poll.

Tries to dequeue a value (non-null) or returns null if the queue is empty.
public int
requestFusion(int
the requested fusion mode, allowed values are SYNC, ASYNC, ANY combined with BOUNDARY (e.g., requestFusion(SYNC | BOUNDARY)).
mode
)

Implements io.reactivex.rxjava3.operators.QueueFuseable.requestFusion.

Request a fusion mode from the upstream.
Inherited from io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable:
offeroffer

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable.serialVersionUID.

Constructor Detail

UnicastQueueDisposableback to summary
pack-priv UnicastQueueDisposable()

Method Detail

clearback to summary
public void clear()

Implements io.reactivex.rxjava3.operators.SimpleQueue.clear.

Doc from io.reactivex.rxjava3.operators.SimpleQueue.clear.

Removes all enqueued items from this queue.

Annotations
@Override
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
isEmptyback to summary
public boolean isEmpty()

Implements io.reactivex.rxjava3.operators.SimpleQueue.isEmpty.

Doc from io.reactivex.rxjava3.operators.SimpleQueue.isEmpty.

Returns true if the queue is empty.

Note however that due to potential fused functions in poll() it is possible this method returns false but then poll() returns null because the fused function swallowed the available item(s).

Returns:boolean

true if the queue is empty

Annotations
@Override
pollback to summary
public T poll()

Implements io.reactivex.rxjava3.operators.SimpleQueue.poll.

Doc from io.reactivex.rxjava3.operators.SimpleQueue.poll.

Tries to dequeue a value (non-null) or returns null if the queue is empty.

If the producer uses offer(Object, Object) and when polling in pairs, if the first poll() returns a non-null item, the second poll() is guaranteed to return a non-null item as well.

Returns:T

the item or null to indicate an empty queue

Annotations
@Nullable
@Override
requestFusionback to summary
public int requestFusion(int mode)

Implements io.reactivex.rxjava3.operators.QueueFuseable.requestFusion.

Doc from io.reactivex.rxjava3.operators.QueueFuseable.requestFusion.

Request a fusion mode from the upstream.

This should be called before onSubscribe returns.

Calling this method multiple times or after onSubscribe finished is not allowed and may result in undefined behavior.

Parameters
mode:int

the requested fusion mode, allowed values are SYNC, ASYNC, ANY combined with BOUNDARY (e.g., requestFusion(SYNC | BOUNDARY)).

Returns:int

the established fusion mode: NONE, SYNC, ASYNC.

Annotations
@Override