Package rx.subjects

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();
    
       
    • 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
      • onError

        public void onError​(java.lang.Throwable e)
        Description copied from interface: Observer
        Notifies the Observer that the Observable has experienced an error condition.

        If the Observable calls this method, it will not thereafter call Observer.onNext(T) or Observer.onCompleted().

        Parameters:
        e - the exception encountered by the Observable
      • 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

        @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
      • getThrowable

        @Beta
        public java.lang.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.