Top Description Inners Fields Constructors Methods
io.reactivex.subjects

public final Class PublishSubject<T>

extends Subject<T>
Class Inheritance
Type Parameters
<T>
the type of items observed and emitted by the Subject
Imports
io.reactivex.annotations.CheckReturnValue, .Nullable, .NonNull, java.util.concurrent.atomic.*, io.reactivex.Observer, io.reactivex.disposables.Disposable, io.reactivex.internal.functions.ObjectHelper, io.reactivex.plugins.RxJavaPlugins

A Subject that emits (multicasts) items to currently subscribed Observers and terminal events to current or late Observers.

This subject does not have a public constructor by design; a new empty instance of this PublishSubject can be created via the create() method.

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 PublishSubject is an io.reactivex.Observable, it does not support backpressure.

When this PublishSubject is terminated via onError(Throwable) or onComplete(), late io.reactivex.Observers only receive the respective terminal event.

Unlike a BehaviorSubject, a PublishSubject doesn't retain/cache items, therefore, a new Observer won't receive any past items.

Even though PublishSubject 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 PublishSubject 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 PublishSubject supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasObservers().

Scheduler:
PublishSubject does not operate by default on a particular io.reactivex.Scheduler and the Observers get notified on the thread the respective onXXX methods were invoked.
Error handling:
When the onError(Throwable) is called, the PublishSubject enters into a terminal state and emits the same Throwable instance to the last set of Observers. During this emission, if one or more Observers dispose their respective Disposables, the Throwable is delivered to the global error handler via io.reactivex.plugins.RxJavaPlugins#onError(Throwable) (multiple times if multiple Observers cancel at once). If there were no Observers subscribed to this PublishSubject when the onError() was called, the global error handler is not invoked.

Example usage:

 PublishSubject<Object> subject = PublishSubject.create();
 // observer1 will receive all onNext and onComplete events
 subject.subscribe(observer1);
 subject.onNext("one");
 subject.onNext("two");
 // observer2 will only receive "three" and onComplete
 subject.subscribe(observer2);
 subject.onNext("three");
 subject.onComplete();

 // late Observers only receive the terminal event
 subject.test().assertEmpty();
  

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
PublishSubject.PublishDisposable<
the value type
T
>

Wraps the actual subscriber, tracks its requests and makes cancellation to remove itself from the current subscribers array.

Field Summary

Modifier and TypeField and Description
pack-priv static final PublishSubject.PublishDisposable<T>[]
EMPTY

An empty subscribers array to avoid allocating it all the time.

pack-priv Throwable
error

The error, write before terminating and read after checking subscribers.

pack-priv final AtomicReference<PublishSubject.PublishDisposable<T>[]>
subscribers

The array of currently subscribed subscribers.

pack-priv static final PublishSubject.PublishDisposable<T>[]
TERMINATED

The terminated indicator for the subscribers array.

Constructor Summary

AccessConstructor and Description
pack-priv
PublishSubject()

Constructs a PublishSubject.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if successful, false if the subject has terminated
add
(PublishSubject.PublishDisposable<T>
the subscriber to add
ps
)

Tries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated.

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

Returns:

the new PublishSubject
create
()

Constructs a PublishSubject.

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.
pack-priv void
remove(PublishSubject.PublishDisposable<T>
the subject to remove
ps
)

Atomically removes the given subscriber if it is subscribed to the subject.

protected void
subscribeActual(Observer<? super T>
the incoming Observer, never null
t
)

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

EMPTYback to summary
pack-priv static final PublishSubject.PublishDisposable<T>[] EMPTY

An empty subscribers array to avoid allocating it all the time.

Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error

The error, write before terminating and read after checking subscribers.

subscribersback to summary
pack-priv final AtomicReference<PublishSubject.PublishDisposable<T>[]> subscribers

The array of currently subscribed subscribers.

TERMINATEDback to summary
pack-priv static final PublishSubject.PublishDisposable<T>[] TERMINATED

The terminated indicator for the subscribers array.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

PublishSubjectback to summary
pack-priv PublishSubject()

Constructs a PublishSubject.

Annotations
@SuppressWarnings:unchecked
Since
2.0

Method Detail

addback to summary
pack-priv boolean add(PublishSubject.PublishDisposable<T> ps)

Tries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated.

Parameters
ps:PublishSubject.PublishDisposable<T>

the subscriber to add

Returns:boolean

true if successful, false if the subject has terminated

createback to summary
public static <T> PublishSubject<T> create()

Constructs a PublishSubject.

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

the new PublishSubject

Annotations
@CheckReturnValue
@NonNull
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
@SuppressWarnings:unchecked
@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
@SuppressWarnings:unchecked
@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
removeback to summary
pack-priv void remove(PublishSubject.PublishDisposable<T> ps)

Atomically removes the given subscriber if it is subscribed to the subject.

Parameters
ps:PublishSubject.PublishDisposable<T>

the subject to remove

Annotations
@SuppressWarnings:unchecked
subscribeActualback to summary
protected void subscribeActual(Observer<? super T> t)

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
t:Observer<? super T>

the incoming Observer, never null

Annotations
@Override
io.reactivex.subjects back to summary

pack-priv final Class PublishSubject.PublishDisposable<T>

extends AtomicBoolean
implements Disposable
Class Inheritance
All Implemented Interfaces
io.reactivex.disposables.Disposable
Type Parameters
<T>
the value type

Wraps the actual subscriber, tracks its requests and makes cancellation to remove itself from the current subscribers array.

Field Summary

Modifier and TypeField and Description
pack-priv final Observer<? super T>
downstream

The actual subscriber.

pack-priv final PublishSubject<T>
parent

The subject state.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
PublishDisposable(Observer<? super T>
the actual subscriber
actual
,
PublishSubject<T>
the parent PublishProcessor
parent
)

Constructs a PublishSubscriber, wraps the actual subscriber and the state.

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.
public void
public void
public void
onNext(T t)

Inherited from java.util.concurrent.atomic.AtomicBoolean:
compareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetgetgetAcquiregetAndSetgetOpaquegetPlainlazySetsetsetOpaquesetPlainsetReleasetoStringweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

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

The actual subscriber.

parentback to summary
pack-priv final PublishSubject<T> parent

The subject state.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.atomic.AtomicBoolean.serialVersionUID.

Constructor Detail

PublishDisposableback to summary
pack-priv PublishDisposable(Observer<? super T> actual, PublishSubject<T> parent)

Constructs a PublishSubscriber, wraps the actual subscriber and the state.

Parameters
actual:Observer<? super T>

the actual subscriber

parent:PublishSubject<T>

the parent PublishProcessor

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
onCompleteback to summary
public void onComplete()
onErrorback to summary
public void onError(Throwable t)
onNextback to summary
public void onNext(T t)