Top Description Inners Fields Constructors Methods
io.reactivex.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
io.reactivex.annotations.Nullable, .NonNull, .CheckReturnValue, io.reactivex.plugins.RxJavaPlugins, java.util.concurrent.atomic.*, io.reactivex.*, io.reactivex.disposables.Disposable, io.reactivex.internal.disposables.EmptyDisposable, io.reactivex.internal.functions.ObjectHelper, io.reactivex.internal.fuseable.SimpleQueue, io.reactivex.internal.observers.BasicIntQueueDisposable, io.reactivex.internal.queue.SpscLinkedArrayQueue

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 unsubscribes.

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.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.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.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
,
boolean
deliver pending onNext events before onError
delayError
)

Creates an UnicastSubject with the given capacity hint and delay error flag.

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
)

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

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
>
UnicastSubject<T>

Returns:

an UnicastSubject instance
create
()

Creates an UnicastSubject with an internal buffer capacity hint 16.

public static <
the value type
T
>
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
>
UnicastSubject<T>

Returns:

an UnicastSubject instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
,
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.

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

Returns:

an UnicastSubject instance
create
(int
the hint to size the internal unbounded buffer
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 internal buffer capacity hint, delay error flag and a callback for the case when the single Subscriber cancels its subscription.

public static <
the value type
T
>
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 Throwable
getThrowable()

Implements abstract io.reactivex.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.subjects.Subject.hasComplete.

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

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

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

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

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

Implements io.reactivex.Observer.onComplete.

Notifies the Observer that the Observable has finished sending push-based notifications.
public void
onError(Throwable
the exception encountered by the Observable
t
)

Implements io.reactivex.Observer.onError.

Notifies the Observer that the Observable has experienced an error condition.
public void
onNext(T
the item emitted by the Observable
t
)

Implements io.reactivex.Observer.onNext.

Provides the Observer with a new item to observe.
public void
onSubscribe(Disposable
the Disposable instance whose Disposable#dispose() can be called anytime to cancel the connection
d
)

Implements io.reactivex.Observer.onSubscribe.

Provides the Observer with the means of cancelling (disposing) the connection (channel) with the Observable in both synchronous (from within onNext(Object)) and asynchronous manner.
protected void
subscribeActual(Observer<? super T>
the incoming Observer, never null
observer
)

Implements abstract io.reactivex.Observable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Observers.
Inherited from io.reactivex.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, boolean delayError)

Creates an UnicastSubject with the given capacity hint and delay error flag.

History: 2.0.8 - experimental

Parameters
capacityHint:int

the capacity hint for the internal, unbounded queue

delayError:boolean

deliver pending onNext events before onError

Since
2.2
UnicastSubjectback to summary
pack-priv UnicastSubject(int capacityHint, Runnable onTerminate)

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

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

Since
2.0
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> UnicastSubject<T> create()

Creates an UnicastSubject with an internal buffer capacity hint 16.

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

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> 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:UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> UnicastSubject<T> create(int capacityHint, 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.

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:Runnable

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

Returns:UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> UnicastSubject<T> create(int capacityHint, 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 Subscriber cancels its subscription.

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:Runnable

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

delayError:boolean

deliver pending onNext events before onError

Returns:UnicastSubject<T>

an UnicastSubject instance

Annotations
@CheckReturnValue
@NonNull
Since
2.2
createback to summary
public static <T> 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: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 Throwable getThrowable()

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

Doc from io.reactivex.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:Throwable

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

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

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

Doc from io.reactivex.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
hasObserversback to summary
public boolean hasObservers()

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

Doc from io.reactivex.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
hasThrowableback to summary
public boolean hasThrowable()

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

Doc from io.reactivex.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
onCompleteback to summary
public void onComplete()

Implements io.reactivex.Observer.onComplete.

Doc from io.reactivex.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)

Implements io.reactivex.Observer.onError.

Doc from io.reactivex.Observer.onError.

Notifies the Observer that the Observable has experienced an error condition.

If the Observable calls this method, it will not thereafter call onNext or onComplete.

Parameters
t:Throwable

the exception encountered by the Observable

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

Implements io.reactivex.Observer.onNext.

Doc from io.reactivex.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)

Implements io.reactivex.Observer.onSubscribe.

Doc from io.reactivex.Observer.onSubscribe.

Provides the Observer with the means of cancelling (disposing) the connection (channel) with the Observable in both synchronous (from within onNext(Object)) and asynchronous manner.

Parameters
d:Disposable

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

Annotations
@Override
subscribeActualback to summary
protected void subscribeActual(Observer<? super T> observer)

Implements abstract io.reactivex.Observable.subscribeActual.

Doc from io.reactivex.Observable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Observers.

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

Parameters
observer:Observer<? super T>

the incoming Observer, never null

Annotations
@Override
io.reactivex.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.internal.fuseable.SimpleQueue.clear.

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

Implements io.reactivex.internal.fuseable.SimpleQueue.isEmpty.

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

Implements io.reactivex.internal.fuseable.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.internal.fuseable.QueueFuseable.requestFusion.

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

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

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

Constructor Detail

UnicastQueueDisposableback to summary
pack-priv UnicastQueueDisposable()

Method Detail

clearback to summary
public void clear()

Implements io.reactivex.internal.fuseable.SimpleQueue.clear.

Doc from io.reactivex.internal.fuseable.SimpleQueue.clear.

Removes all enqueued items from this queue.

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

Implements io.reactivex.internal.fuseable.SimpleQueue.isEmpty.

Doc from io.reactivex.internal.fuseable.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() throws Exception

Implements io.reactivex.internal.fuseable.SimpleQueue.poll.

Doc from io.reactivex.internal.fuseable.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
Exceptions
Exception:
if some pre-processing of the dequeued item (usually through fused functions) throws.
requestFusionback to summary
public int requestFusion(int mode)

Implements io.reactivex.internal.fuseable.QueueFuseable.requestFusion.

Doc from io.reactivex.internal.fuseable.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