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

public final Class UnicastProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Type Parameters
<T>
the value type received and emitted by this Processor subclass
Imports
java.util.Objects, java.util.concurrent.atomic.*, org.reactivestreams.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.internal.functions.*, io.reactivex.rxjava3.internal.subscriptions.*, io.reactivex.rxjava3.internal.util.*, io.reactivex.rxjava3.operators.QueueSubscription, .SpscLinkedArrayQueue, io.reactivex.rxjava3.plugins.RxJavaPlugins

A FlowableProcessor variant that queues up events until a single Subscriber subscribes to it, replays those events to it until the Subscriber catches up and then switches to relaying events live to this single Subscriber until this UnicastProcessor terminates or the Subscriber cancels its subscription.

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

If more than one Subscriber attempts to subscribe to this Processor, they will receive an IllegalStateException if this UnicastProcessor hasn't terminated yet, or the Subscribers receive the terminal event (error or completion) if this Processor has terminated.

The UnicastProcessor buffers notifications and replays them to the single Subscriber as requested, for which it holds upstream items an unbounded internal buffer until they can be emitted.

Since a UnicastProcessor is a Reactive Streams Processor, 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 processor's state is not changed.

Since a UnicastProcessor is a io.reactivex.rxjava3.core.Flowable as well as a FlowableProcessor, it honors the downstream backpressure but consumes an upstream source in an unbounded manner (requesting Long#MAX_VALUE).

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

Even though UnicastProcessor implements the Subscriber interface, calling onSubscribe is not required (Rule 2.12) if the processor is used as a standalone source. However, calling onSubscribe after the UnicastProcessor reached its terminal state will result in the given Subscription being canceled 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 FlowableProcessors provides such serialization and also protects against reentrance (i.e., when a downstream Subscriber consuming this processor also wants to call onNext(Object) on this processor recursively).

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

Backpressure:
UnicastProcessor honors the downstream backpressure but consumes an upstream source (if any) in an unbounded manner (requesting Long#MAX_VALUE).
Scheduler:
UnicastProcessor does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the single Subscriber gets notified on the thread the respective onXXX methods were invoked.
Error handling:
When the onError(Throwable) is called, the UnicastProcessor enters into a terminal state and emits the same Throwable instance to the current single Subscriber. During this emission, if the single Subscribers cancels its respective Subscriptions, the Throwable is delivered to the global error handler via io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable). If there were no Subscribers subscribed to this UnicastProcessor when the onError() was called, the global error handler is not invoked.

Example usage:


UnicastProcessor<Integer> processor = UnicastProcessor.create();

TestSubscriber<Integer> ts1 = processor.test();

// fresh UnicastProcessors are empty
ts1.assertEmpty();

TestSubscriber<Integer> ts2 = processor.test();

// A UnicastProcessor only allows one Subscriber during its lifetime
ts2.assertFailure(IllegalStateException.class);

processor.onNext(1);
ts1.assertValue(1);

processor.onNext(2);
ts1.assertValues(1, 2);

processor.onComplete();
ts1.assertResult(1, 2);

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

UnicastProcessor<Integer> processor2 = UnicastProcessor.create();

// a UnicastProcessor caches events until its single Subscriber subscribes
processor2.onNext(1);
processor2.onNext(2);
processor2.onComplete();

TestSubscriber<Integer> ts3 = processor2.test();

// the cached events are emitted in order
ts3.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 volatile boolean
pack-priv final boolean
pack-priv volatile boolean
pack-priv final AtomicReference<Subscriber<? super T>>
pack-priv boolean
pack-priv Throwable
pack-priv final AtomicBoolean
pack-priv final AtomicReference<Runnable>
pack-priv final SpscLinkedArrayQueue<T>
pack-priv final AtomicLong
pack-priv final BasicIntQueueSubscription<T>

Constructor Summary

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

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

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
checkTerminated(boolean failFast, boolean d, boolean empty, Subscriber<? super T> a, SpscLinkedArrayQueue<T> q)

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

Returns:

an UnicastSubject instance
create
()

Creates an UnicastSubject with an internal buffer capacity hint 16.

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

Returns:

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

Creates an UnicastProcessor with the given internal buffer capacity hint.

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

Returns:

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

Creates an UnicastProcessor with default internal buffer capacity hint and delay error flag.

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

Returns:

an UnicastProcessor instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
,
@NonNull Runnable
the non null callback
onTerminate
)

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

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

Returns:

an UnicastProcessor instance
create
(int
the hint to size the internal unbounded buffer
capacityHint
,
@NonNull Runnable
the non null callback
onTerminate
,
boolean
deliver pending onNext events before onError
delayError
)

Creates an UnicastProcessor with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Subscriber cancels its subscription or the processor is terminated.

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

pack-priv void
drainRegular(Subscriber<? super T> a)

public @Nullable Throwable
getThrowable()

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.getThrowable.

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

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasComplete.

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

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasSubscribers.

Returns true if the FlowableProcessor has subscribers.
public boolean
hasThrowable()

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasThrowable.

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

Implements org.reactivestreams.Subscriber.onComplete.

Successful terminal state.
public void
onError(Throwable
the throwable signaled
t
)

Implements org.reactivestreams.Subscriber.onError.

Failed terminal state.
public void
onNext(T
the element signaled
t
)

Implements org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).
public void
protected void
Inherited from io.reactivex.rxjava3.processors.FlowableProcessor:
toSerialized

Field Detail

cancelledback to summary
pack-priv volatile boolean cancelled
delayErrorback to summary
pack-priv final boolean delayError
doneback to summary
pack-priv volatile boolean done
downstreamback to summary
pack-priv final AtomicReference<Subscriber<? super T>> downstream
enableOperatorFusionback to summary
pack-priv boolean enableOperatorFusion
errorback to summary
pack-priv Throwable error
onceback to summary
pack-priv final AtomicBoolean once
onTerminateback to summary
pack-priv final AtomicReference<Runnable> onTerminate
queueback to summary
pack-priv final SpscLinkedArrayQueue<T> queue
requestedback to summary
pack-priv final AtomicLong requested
wipback to summary
pack-priv final BasicIntQueueSubscription<T> wip

Constructor Detail

UnicastProcessorback to summary
pack-priv UnicastProcessor(int capacityHint, Runnable onTerminate, boolean delayError)

Creates an UnicastProcessor with the given capacity hint and callback for when the Processor 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 Processor is terminated or cancelled, null not allowed

delayError:boolean

deliver pending onNext events before onError

Since
2.2

Method Detail

checkTerminatedback to summary
pack-priv boolean checkTerminated(boolean failFast, boolean d, boolean empty, Subscriber<? super T> a, SpscLinkedArrayQueue<T> q)
createback to summary
public static <T> @NonNull UnicastProcessor<T> create()

Creates an UnicastSubject with an internal buffer capacity hint 16.

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

an UnicastSubject instance

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

Creates an UnicastProcessor with the given internal buffer capacity hint.

Parameters
<T>
the value type
capacityHint:int

the hint to size the internal unbounded buffer

Returns:@NonNull UnicastProcessor<T>

an UnicastProcessor instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
IllegalArgumentException:
if capacityHint is non-positive
createback to summary
public static <T> @NonNull UnicastProcessor<T> create(boolean delayError)

Creates an UnicastProcessor with default internal buffer capacity hint and delay error flag.

History: 2.0.8 - experimental

Parameters
<T>
the value type
delayError:boolean

deliver pending onNext events before onError

Returns:@NonNull UnicastProcessor<T>

an UnicastProcessor instance

Annotations
@CheckReturnValue
@NonNull
Since
2.2
createback to summary
public static <T> @NonNull UnicastProcessor<T> create(int capacityHint, @NonNull Runnable onTerminate)

Creates an UnicastProcessor with the given internal buffer capacity hint and a callback for the case when the single Subscriber cancels its subscription or the processor 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 non null callback

Returns:@NonNull UnicastProcessor<T>

an UnicastProcessor instance

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

Creates an UnicastProcessor with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Subscriber cancels its subscription or the processor 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 non null callback

delayError:boolean

deliver pending onNext events before onError

Returns:@NonNull UnicastProcessor<T>

an UnicastProcessor instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
NullPointerException:
if onTerminate is null
IllegalArgumentException:
if capacityHint is non-positive
Since
2.2
doTerminateback to summary
pack-priv void doTerminate()
drainback to summary
pack-priv void drain()
drainFusedback to summary
pack-priv void drainFused(Subscriber<? super T> a)
drainRegularback to summary
pack-priv void drainRegular(Subscriber<? super T> a)
getThrowableback to summary
public @Nullable Throwable getThrowable()

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.getThrowable.

Doc from io.reactivex.rxjava3.processors.FlowableProcessor.getThrowable.

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

The method is thread-safe.

Returns:@Nullable Throwable

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

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

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasComplete.

Doc from io.reactivex.rxjava3.processors.FlowableProcessor.hasComplete.

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

The method is thread-safe.

Returns:boolean

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

Annotations
@Override
@CheckReturnValue
hasSubscribersback to summary
public boolean hasSubscribers()

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasSubscribers.

Doc from io.reactivex.rxjava3.processors.FlowableProcessor.hasSubscribers.

Returns true if the FlowableProcessor has subscribers.

The method is thread-safe.

Returns:boolean

true if the FlowableProcessor has subscribers

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

Implements abstract io.reactivex.rxjava3.processors.FlowableProcessor.hasThrowable.

Doc from io.reactivex.rxjava3.processors.FlowableProcessor.hasThrowable.

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

The method is thread-safe.

Returns:boolean

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

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

Implements org.reactivestreams.Subscriber.onComplete.

Doc from org.reactivestreams.Subscriber.onComplete.

Successful terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

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

Implements org.reactivestreams.Subscriber.onError.

Doc from org.reactivestreams.Subscriber.onError.

Failed terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Parameters
t:Throwable

the throwable signaled

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

Implements org.reactivestreams.Subscriber.onNext.

Doc from org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).

Parameters
t:T

the element signaled

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Subscription s)

Implements org.reactivestreams.Subscriber.onSubscribe.

Doc from org.reactivestreams.Subscriber.onSubscribe.

Invoked after calling Publisher#subscribe(Subscriber).

No data will start flowing until Subscription#request(long) is invoked.

It is the responsibility of this Subscriber instance to call Subscription#request(long) whenever more data is wanted.

The Publisher will send notifications only in response to Subscription#request(long).

Parameters
s:Subscription

the Subscription that allows requesting data via Subscription#request(long)

Annotations
@Override
subscribeActualback to summary
protected void subscribeActual(Subscriber<? super T> s)
Annotations
@Override
io.reactivex.rxjava3.processors back to summary

pack-priv final Class UnicastProcessor.UnicastQueueSubscription

extends BasicIntQueueSubscription<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
cancel()

Implements org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.
public void
clear()

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

Removes all enqueued items from this queue.
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 void
request(long
the strictly positive number of elements to requests to the upstream Publisher
n
)

Implements org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.
public int
requestFusion(int
the requested fusion mode, allowed values are SYNC, ASYNC, ANY combined with BOUNDARY (e.g., requestFusion(SYNC | BOUNDARY)).
requestedMode
)

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

Request a fusion mode from the upstream.
Inherited from io.reactivex.rxjava3.internal.subscriptions.BasicIntQueueSubscription:
offeroffer

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides io.reactivex.rxjava3.internal.subscriptions.BasicIntQueueSubscription.serialVersionUID.

Constructor Detail

UnicastQueueSubscriptionback to summary
pack-priv UnicastQueueSubscription()

Method Detail

cancelback to summary
public void cancel()

Implements org.reactivestreams.Subscription.cancel.

Doc from org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.

Data may still be sent to meet previously signalled demand after calling cancel.

Annotations
@Override
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
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
requestback to summary
public void request(long n)

Implements org.reactivestreams.Subscription.request.

Doc from org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.

It can be called however often and whenever needed—but if the outstanding cumulative demand ever becomes Long.MAX_VALUE or more, it may be treated by the Publisher as "effectively unbounded".

Whatever has been requested can be sent by the Publisher so only signal demand for what can be safely handled.

A Publisher can send less than is requested if the stream ends but then must emit either Subscriber#onError(Throwable) or Subscriber#onComplete().

Parameters
n:long

the strictly positive number of elements to requests to the upstream Publisher

Annotations
@Override
requestFusionback to summary
public int requestFusion(int requestedMode)

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