Top Description Fields Constructors Methods
rx.subjects

public final Class BehaviorSubject<T>

extends Subject<T, T>
Class Inheritance
Type Parameters
<T>
the type of item expected to be observed by the Subject
Imports
java.lang.reflect.Array, java.util.*, rx.Observer, rx.exceptions.Exceptions, rx.functions.Action1, rx.internal.operators.NotificationLite, rx.subjects.SubjectSubscriptionManager.SubjectObserver

Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer.

Example usage:

 // observer will receive all 4 events (including "default").
 BehaviorSubject<Object> subject = BehaviorSubject.create("default");
 subject.subscribe(observer);
 subject.onNext("one");
 subject.onNext("two");
 subject.onNext("three");

 // observer will receive the "one", "two" and "three" events, but not "default" and "zero"
 BehaviorSubject<Object> subject = BehaviorSubject.create("default");
 subject.onNext("zero");
 subject.onNext("one");
 subject.subscribe(observer);
 subject.onNext("two");
 subject.onNext("three");

 // observer will receive only onCompleted
 BehaviorSubject<Object> subject = BehaviorSubject.create("default");
 subject.onNext("zero");
 subject.onNext("one");
 subject.onCompleted();
 subject.subscribe(observer);

 // observer will receive only onError
 BehaviorSubject<Object> subject = BehaviorSubject.create("default");
 subject.onNext("zero");
 subject.onNext("one");
 subject.onError(new RuntimeException("error"));
 subject.subscribe(observer);
  

Field Summary

Modifier and TypeField and Description
private static final Object[]
EMPTY_ARRAY

An empty array to trigger getValues() to return a new array.

private final SubjectSubscriptionManager<T>

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public static <
the type of item the Subject will emit
T
>
BehaviorSubject<T>

Returns:

the constructed BehaviorSubject
create
()

Creates a BehaviorSubject without a default item.

public static <
the type of item the Subject will emit
T
>
BehaviorSubject<T>

Returns:

the constructed BehaviorSubject
create
(T
the item that will be emitted first to any Observer as long as the BehaviorSubject has not yet observed any items from its source Observable
defaultValue
)

Creates a BehaviorSubject that emits the last item it observed and all subsequent items to each Observer that subscribes to it.

private static <T> BehaviorSubject<T>
create(T defaultValue, boolean hasDefault)

public Throwable

Returns:

the Throwable that terminated the Subject or null if the subject hasn't terminated yet or it terminated normally.
getThrowable
()

Returns the Throwable that terminated the Subject.

public T

Returns:

the current value or null if the Subject doesn't have a value, has terminated or has an actual null as a valid value.
getValue
()

Returns the current value of the Subject if there is such a value and the subject hasn't terminated yet.

public T[]

Returns:

the array a if it had enough capacity or a new array containing the available values
getValues
(T[]
the array to fill in
a
)

Returns a snapshot of the currently buffered non-terminal events into the provided a array or creates a new array if it has not enough capacity.

public Object[]

Returns:

a snapshot of the currently buffered non-terminal events.
getValues
()

Returns a snapshot of the currently buffered non-terminal events.

public boolean

Returns:

true if the subject completed normally via onCompleted()
hasCompleted
()

Check if the Subject has terminated normally.

public boolean
hasObservers()

Implements abstract rx.subjects.Subject.hasObservers.

Indicates whether the Subject has Observers subscribed to it.
public boolean

Returns:

true if the subject has received a throwable through onError.
hasThrowable
()

Check if the Subject has terminated with an exception.

public boolean

Returns:

true if and only if the subject has some value and hasn't terminated yet.
hasValue
()

Check if the Subject has a value.

public void
onCompleted()

Implements rx.Observer.onCompleted.

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

Implements rx.Observer.onError.

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

Implements rx.Observer.onNext.

Provides the Observer with a new item to observe.
pack-priv int
Inherited from rx.subjects.Subject:
toSerialized

Field Detail

EMPTY_ARRAYback to summary
private static final Object[] EMPTY_ARRAY

An empty array to trigger getValues() to return a new array.

stateback to summary
private final SubjectSubscriptionManager<T> state

Constructor Detail

BehaviorSubjectback to summary
protected BehaviorSubject(Observable.OnSubscribe<T> onSubscribe, SubjectSubscriptionManager<T> state)

Method Detail

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

Creates a BehaviorSubject without a default item.

Parameters
<T>
the type of item the Subject will emit
Returns:BehaviorSubject<T>

the constructed BehaviorSubject

createback to summary
public static <T> BehaviorSubject<T> create(T defaultValue)

Creates a BehaviorSubject that emits the last item it observed and all subsequent items to each Observer that subscribes to it.

Parameters
<T>
the type of item the Subject will emit
defaultValue:T

the item that will be emitted first to any Observer as long as the BehaviorSubject has not yet observed any items from its source Observable

Returns:BehaviorSubject<T>

the constructed BehaviorSubject

createback to summary
private static <T> BehaviorSubject<T> create(T defaultValue, boolean hasDefault)
getThrowableback to summary
public Throwable getThrowable()

Returns the Throwable that terminated the Subject.

Returns:Throwable

the Throwable that terminated the Subject or null if the subject hasn't terminated yet or it terminated normally.

Since
1.2
getValueback to summary
public T getValue()

Returns the current value of the Subject if there is such a value and the subject hasn't terminated yet.

The method can return null for various reasons. Use hasValue(), hasThrowable() and hasCompleted() to determine if such null is a valid value, there was an exception or the Subject terminated (with or without receiving any value).

Returns:T

the current value or null if the Subject doesn't have a value, has terminated or has an actual null as a valid value.

Since
1.2
getValuesback to summary
public T[] getValues(T[] a)

Returns a snapshot of the currently buffered non-terminal events into the provided a array or creates a new array if it has not enough capacity.

Parameters
a:T[]

the array to fill in

Returns:T[]

the array a if it had enough capacity or a new array containing the available values

Annotations
@SuppressWarnings:unchecked
Since
1.2
getValuesback to summary
public Object[] getValues()

Returns a snapshot of the currently buffered non-terminal events.

The operation is thread-safe.

Returns:Object[]

a snapshot of the currently buffered non-terminal events.

Annotations
@SuppressWarnings:unchecked
Since
(If this graduates from being an Experimental class method, replace this parenthetical with the release number), 1.2
hasCompletedback to summary
public boolean hasCompleted()

Check if the Subject has terminated normally.

Returns:boolean

true if the subject completed normally via onCompleted()

Since
1.2
hasObserversback to summary
public boolean hasObservers()

Implements abstract rx.subjects.Subject.hasObservers.

Doc from rx.subjects.Subject.hasObservers.

Indicates whether the Subject has Observers subscribed to it.

Returns:boolean

true if there is at least one Observer subscribed to this Subject, false otherwise

Annotations
@Override
hasThrowableback to summary
public boolean hasThrowable()

Check if the Subject has terminated with an exception.

Returns:boolean

true if the subject has received a throwable through onError.

Since
1.2
hasValueback to summary
public boolean hasValue()

Check if the Subject has a value.

Use the getValue() method to retrieve such a value.

Note that unless hasCompleted() or hasThrowable() returns true, the value retrieved by getValue() may get outdated.

Returns:boolean

true if and only if the subject has some value and hasn't terminated yet.

Since
1.2
onCompletedback to summary
public void onCompleted()

Implements rx.Observer.onCompleted.

Doc from rx.Observer.onCompleted.

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 e)

Implements rx.Observer.onError.

Doc from rx.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 onCompleted.

Parameters
e:Throwable

the exception encountered by the Observable

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

Implements rx.Observer.onNext.

Doc from rx.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 onCompleted or onError.

Parameters
v:T

the item emitted by the Observable

Annotations
@Override
subscriberCountback to summary
pack-priv int subscriberCount()