Class BehaviorSubject<T>

  • Type Parameters:
    T - the type of item expected to be observed by the Subject
    All Implemented Interfaces:
    Observer<T>


    public final class BehaviorSubject<T>
    extends Subject<T,T>
    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 events. 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 "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);  
    • Constructor Detail

      • BehaviorSubject

        protected BehaviorSubject(Observable.OnSubscribe<T> onSubscribe,
                                  rx.subjects.SubjectSubscriptionManager<T> state)
    • Method Detail

      • create

        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.
        Type Parameters:
        T - the type of item the Subject will emit
        Parameters:
        defaultValue - 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:
        the constructed BehaviorSubject
      • hasObservers

        public boolean hasObservers()
        Description copied from class: Subject
        Indicates whether the Subject has Observers subscribed to it.
        Specified by:
        hasObservers in class  Subject<T,T>
        Returns:
        true if there is at least one Observer subscribed to this Subject, false otherwise
      • hasValue

        @Beta
        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:
        true if and only if the subject has some value and hasn't terminated yet.
      • hasThrowable

        @Beta
        public boolean hasThrowable()
        Check if the Subject has terminated with an exception.
        Returns:
        true if the subject has received a throwable through onError.
      • hasCompleted

        @Beta
        public boolean hasCompleted()
        Check if the Subject has terminated normally.
        Returns:
        true if the subject completed normally via onCompleted()
      • getValue

        @Beta
        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:
        the current value or null if the Subject doesn't have a value, has terminated or has an actual null as a valid value.
      • getThrowable

        @Beta
        public Throwable getThrowable()
        Returns the Throwable that terminated the Subject.
        Returns:
        the Throwable that terminated the Subject or null if the subject hasn't terminated yet or it terminated normally.
      • getValues

        @Beta
        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 - the array to fill in
        Returns:
        the array a if it had enough capacity or a new array containing the available values
      • getValues

        @Beta
        public Object[] getValues()
        Returns a snapshot of the currently buffered non-terminal events.

        The operation is threadsafe.

        Returns:
        a snapshot of the currently buffered non-terminal events.
        Since:
        (If this graduates from being an Experimental class method, replace this parenthetical with the release number)