Package rx.subjects

Class PublishSubject<T>

java.lang.Object
rx.Observable<T>
rx.subjects.Subject<T,T>
rx.subjects.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();

   
  • Field Details

  • Constructor Details

  • Method Details

    • create

      public static <T> PublishSubject<T> create()
      Creates and returns a new PublishSubject.
      Type Parameters:
      T - the value type
      Returns:
      the new PublishSubject
    • onCompleted

      public void onCompleted()
      Description copied from interface: Observer
      Notifies the Observer that the Observable has finished sending push-based notifications.

      The Observable will not call this method if it calls Observer.onError(java.lang.Throwable).

    • onError

      public void onError(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
    • onNext

      public void onNext(T v)
      Description copied from interface: Observer
      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 Observer.onCompleted() or Observer.onError(java.lang.Throwable).

      Parameters:
      v - the item emitted 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 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.