Class PublishSubject<T>

  • Type Parameters:
    T - the type of items observed and emitted by the Subject
    All Implemented Interfaces:
    Observer<T>


    public final class PublishSubject<T>
    extends Subject<T,T>
    Subject that, once an Observer has subscribed, emits all subsequently observed items to the subscriber.

    Example usage:

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

      • PublishSubject

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

      • create

        public static <T> PublishSubject<T> create()
        Creates and returns a new PublishSubject.
        Type Parameters:
        T - the value type
        Returns:
        the new PublishSubject
      • 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
      • hasThrowable

        @Experimental
        public boolean hasThrowable()
        Check if the Subject has terminated with an exception.
        Overrides:
        hasThrowable in class  Subject<T,T>
        Returns:
        true if the subject has received a throwable through onError.
      • hasCompleted

        @Experimental
        public boolean hasCompleted()
        Check if the Subject has terminated normally.
        Overrides:
        hasCompleted in class  Subject<T,T>
        Returns:
        true if the subject completed normally via onCompleted
      • getThrowable

        @Experimental
        public Throwable getThrowable()
        Returns the Throwable that terminated the Subject.
        Overrides:
        getThrowable in class  Subject<T,T>
        Returns:
        the Throwable that terminated the Subject or null if the subject hasn't terminated yet or it terminated normally.
      • getValue

        @Experimental
         @Deprecated
        public T getValue()
        Deprecated.  this method is scheduled to be removed in the next release
        Returns the current or latest value of the Subject if there is such a value and the subject hasn't terminated with an exception.

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

        The operation is threadsafe.

        Overrides:
        getValue in class  Subject<T,T>
        Returns:
        the current value or null if the Subject doesn't have a value, has terminated with an exception or has an actual null as a value.
      • getValues

        @Experimental
         @Deprecated
        public Object[] getValues()
        Deprecated.  this method is scheduled to be removed in the next release
        Returns a snapshot of the currently buffered non-terminal events.

        The operation is threadsafe.

        Overrides:
        getValues in class  Subject<T,T>
        Returns:
        a snapshot of the currently buffered non-terminal events.
      • getValues

        @Experimental
         @Deprecated
        public T[] getValues(T[] a)
        Deprecated.  this method is scheduled to be removed in the next release
        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.

        If the subject's values fit in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the subject's values is set to null.

        The operation is threadsafe.

        Overrides:
        getValues in class  Subject<T,T>
        Parameters:
        a - the array to fill in
        Returns:
        the array a if it had enough capacity or a new array containing the available values