Observer.
This subject does not have a public constructor by design; a new empty instance of this
BehaviorSubject can be created via the create() method and
a new non-empty instance can be created via createDefault(Object) (named as such to avoid
overload resolution conflict with Observable.create that creates an Observable, not a BehaviorSubject).
Since a Subject is conceptionally derived from the Processor type in the Reactive Streams specification,
nulls are not allowed (Rule 2.13) as
default initial values in createDefault(Object) or 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 BehaviorSubject is an io., it does not support backpressure.
When this BehaviorSubject is terminated via onError(Throwable) or onComplete(), the
last observed item (if any) is cleared and late io.s only receive
the respective terminal event.
The BehaviorSubject does not support clearing its cached value (to appear empty again), however, the
effect can be achieved by using a special item and making sure Observers subscribe through a
filter whose predicate filters out this special item:
BehaviorSubject<Integer> subject = BehaviorSubject.create();
final Integer EMPTY = Integer.MIN_VALUE;
Observable<Integer> observable = subject.filter(v -> v != EMPTY);
TestObserver<Integer> to1 = observable.test();
observable.onNext(1);
// this will "clear" the cache
observable.onNext(EMPTY);
TestObserver<Integer> to2 = observable.test();
subject.onNext(2);
subject.onComplete();
// to1 received both non-empty items
to1.assertResult(1, 2);
// to2 received only 2 even though the current item was EMPTY
// when it got subscribed
to2.assertResult(2);
// Observers coming after the subject was terminated receive
// no items and only the onComplete event in this case.
observable.test().assertResult();
Even though BehaviorSubject 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 BehaviorSubject 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 BehaviorSubject supports the standard state-peeking methods hasComplete(), hasThrowable(),
getThrowable() and hasObservers() as well as means to read the latest observed value
in a non-blocking and thread-safe manner via hasValue(), getValue(),
getValues() or getValues(Object[]).
BehaviorSubject 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.onError(Throwable) is called, the BehaviorSubject 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 BehaviorSubject when the onError()
was called, the global error handler is not invoked.
Example usage:
// observer will receive all 4 events (including "default").
BehaviorSubject<Object> subject = BehaviorSubject.createDefault("default");
subject.subscribe(observer);
subject.onNext("one");
subject.onNext("two");
subject.onNext("three");
// observer will receive the "one", "two" and "three" events, but not "zero"
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.subscribe(observer);
subject.onNext("two");
subject.onNext("three");
// observer will receive only onComplete
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.onComplete();
subject.subscribe(observer);
// observer will receive only onError
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.onError(new RuntimeException("error"));
subject.subscribe(observer);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final BehaviorSubject. | |
| private static final Object[] | EMPTY_ARRAY
An empty array to avoid allocation in getValues(). |
| pack-priv long | |
| pack-priv final ReadWriteLock | |
| pack-priv final Lock | |
| pack-priv final AtomicReference | |
| pack-priv final AtomicReference | |
| pack-priv static final BehaviorSubject. | |
| pack-priv final AtomicReference | |
| pack-priv final Lock |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv | BehaviorSubject(T
the initial value, not null (verified) defaultValue)Constructs a BehaviorSubject with the given initial value. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | |
| public static < the type of item the Subject will emit T> BehaviorSubject | |
| public static < the type of item the Subject will emit T> BehaviorSubject | Returns: the constructedBehaviorSubjectthe item that will be emitted first to any defaultValue)Observer as long as the
BehaviorSubject has not yet observed any items from its source ObservableCreates a |
| public Throwable | getThrowable()
Implements abstract io. |
| public T | Returns: a single value the Subject currently has or null if no such value existsReturns a single value the Subject currently has or null if no such value exists. |
| public Object[] | Returns: the array containing the snapshot of all values of the Subject
Deprecated
in 2.1.14; put the result of
Returns an Object array containing snapshot all values of the Subject.
getValue() into an array manually, will be removed in 3.x
|
| public T[] | Returns: the given array if the values fit into it or a new array containing all valuesthe target array to copy values into if it fits array)
Deprecated
in 2.1.14; put the result of
Returns a typed array containing a snapshot of all values of the Subject.
getValue() into an array manually, will be removed in 3.x
|
| public boolean | hasComplete()
Implements abstract io. |
| public boolean | hasObservers()
Implements abstract io. |
| public boolean | hasThrowable()
Implements abstract io. |
| public boolean | |
| public void | onComplete()
Implements io. Observable has finished sending push-based notifications.
|
| public void | onError(Throwable
the exception encountered by the Observable t)Implements io. Observable has experienced an error condition.
|
| public void | onNext(T
the item emitted by the Observable t)Implements io. |
| public void | onSubscribe(Disposable
the Disposable instance whose d)Disposable#dispose() can
be called anytime to cancel the connectionImplements io. onNext(Object)) and asynchronous manner.
|
| pack-priv void | |
| pack-priv void | |
| protected void | subscribeActual(Observer<? super T>
the incoming Observer, never null observer)Implements abstract io. Observers.
|
| pack-priv int | |
| pack-priv BehaviorSubject. |
| EMPTY | back to summary |
|---|---|
pack-priv static final BehaviorSubject.
| |
| EMPTY_ARRAY | back to summary |
|---|---|
| private static final Object[] EMPTY_ARRAY An empty array to avoid allocation in getValues(). | |
| index | back to summary |
|---|---|
| pack-priv long index | |
| lock | back to summary |
|---|---|
| pack-priv final ReadWriteLock lock | |
| readLock | back to summary |
|---|---|
| pack-priv final Lock readLock | |
| subscribers | back to summary |
|---|---|
| pack-priv final AtomicReference<BehaviorSubject. | |
| terminalEvent | back to summary |
|---|---|
| pack-priv final AtomicReference<Throwable> terminalEvent | |
| TERMINATED | back to summary |
|---|---|
pack-priv static final BehaviorSubject.
| |
| value | back to summary |
|---|---|
| pack-priv final AtomicReference<Object> value | |
| writeLock | back to summary |
|---|---|
| pack-priv final Lock writeLock | |
| BehaviorSubject | back to summary |
|---|---|
| pack-priv BehaviorSubject() Constructs an empty BehaviorSubject.
| |
| BehaviorSubject | back to summary |
|---|---|
| pack-priv BehaviorSubject(T defaultValue) Constructs a BehaviorSubject with the given initial value.
| |
| add | back to summary |
|---|---|
| pack-priv boolean add(BehaviorSubject. | |
| create | back to summary |
|---|---|
| public static <T> BehaviorSubject Creates a
| |
| createDefault | back to summary |
|---|---|
| public static <T> BehaviorSubject Creates a
| |
| getThrowable | back to summary |
|---|---|
| public Throwable getThrowable() Implements abstract io. Doc from io. Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet. The method is thread-safe. | |
| getValue | back to summary |
|---|---|
| public T getValue() Returns a single value the Subject currently has or null if no such value exists. The method is thread-safe.
| |
| getValues | back to summary |
|---|---|
| public Object[] getValues()
Deprecated in 2.1.14; put the result of Returns an Object array containing snapshot all values of the Subject. The method is thread-safe.
| |
| getValues | back to summary |
|---|---|
| public T[] getValues(T[] array)
Deprecated in 2.1.14; put the result of Returns a typed array containing a snapshot of all values of the Subject. The method follows the conventions of Collection.toArray by setting the array element after the last value to null (if the capacity permits). The method is thread-safe.
| |
| hasComplete | back to summary |
|---|---|
| public boolean hasComplete() Implements abstract io. Doc from io. Returns true if the subject has reached a terminal state through a complete event. The method is thread-safe.
| |
| hasObservers | back to summary |
|---|---|
| public boolean hasObservers() Implements abstract io. Doc from io. Returns true if the subject has any Observers. The method is thread-safe.
| |
| hasThrowable | back to summary |
|---|---|
| public boolean hasThrowable() Implements abstract io. Doc from io. Returns true if the subject has reached a terminal state through an error event. The method is thread-safe.
| |
| hasValue | back to summary |
|---|---|
| public boolean hasValue() Returns true if the subject has any value. The method is thread-safe.
| |
| onComplete | back to summary |
|---|---|
| public void onComplete() Implements io. Doc from io. Notifies the Observer that the
The
| |
| onError | back to summary |
|---|---|
| public void onError(Throwable t) Implements io. Doc from io. Notifies the Observer that the
If the | |
| onNext | back to summary |
|---|---|
| public void onNext(T t) Implements io. Doc from io. Provides the Observer with a new item to observe.
The
The
| |
| onSubscribe | back to summary |
|---|---|
| public void onSubscribe(Disposable d) Implements io. Doc from io. Provides the Observer with the means of cancelling (disposing) the
connection (channel) with the Observable in both
synchronous (from within
| |
| remove | back to summary |
|---|---|
pack-priv void remove(BehaviorSubject.
| |
| setCurrent | back to summary |
|---|---|
| pack-priv void setCurrent(Object o) | |
| subscribeActual | back to summary |
|---|---|
| protected void subscribeActual(Observer<? super T> observer) Implements abstract io. Doc from io. Operator implementations (both source and intermediate) should implement this method that
performs the necessary business logic and handles the incoming There is no need to call any of the plugin hooks on the current | |
| subscriberCount | back to summary |
|---|---|
| pack-priv int subscriberCount() | |
| terminate | back to summary |
|---|---|
pack-priv BehaviorSubject.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv volatile boolean | |
| pack-priv final Observer | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv long | |
| pack-priv boolean | |
| pack-priv AppendOnlyLinkedArrayList | |
| pack-priv final BehaviorSubject |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | dispose()
Implements io. |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| public boolean | isDisposed()
Implements io. |
| public boolean | test(Object
the value o)Implements io. |
| cancelled | back to summary |
|---|---|
| pack-priv volatile boolean cancelled | |
| downstream | back to summary |
|---|---|
| pack-priv final Observer<? super T> downstream | |
| emitting | back to summary |
|---|---|
| pack-priv boolean emitting | |
| fastPath | back to summary |
|---|---|
| pack-priv boolean fastPath | |
| index | back to summary |
|---|---|
| pack-priv long index | |
| next | back to summary |
|---|---|
| pack-priv boolean next | |
| queue | back to summary |
|---|---|
| pack-priv AppendOnlyLinkedArrayList<Object> queue | |
| state | back to summary |
|---|---|
| pack-priv final BehaviorSubject<T> state | |
| BehaviorDisposable | back to summary |
|---|---|
| pack-priv BehaviorDisposable(Observer<? super T> actual, BehaviorSubject<T> state) | |
| dispose | back to summary |
|---|---|
| public void dispose() Implements io. Doc from io. Dispose the resource, the operation should be idempotent.
| |
| emitFirst | back to summary |
|---|---|
| pack-priv void emitFirst() | |
| emitLoop | back to summary |
|---|---|
| pack-priv void emitLoop() | |
| emitNext | back to summary |
|---|---|
| pack-priv void emitNext(Object value, long stateIndex) | |
| isDisposed | back to summary |
|---|---|
| public boolean isDisposed() Implements io. Doc from io. Returns true if this resource has been disposed.
| |
| test | back to summary |
|---|---|
| public boolean test(Object o) Implements io. Doc from io. Test the given input value and return a boolean. | |