Class Subject<T>

java.lang.Object
io.reactivex.rxjava3.core.Observable<T>
io.reactivex.rxjava3.subjects.Subject<T>
Type Parameters:
T - the item value type
All Implemented Interfaces:
ObservableSource<T>, Observer<T>
Direct Known Subclasses:
AsyncSubject, BehaviorSubject, PublishSubject, ReplaySubject, SerializedSubject, UnicastSubject

public abstract class Subject<T> extends Observable<T> implements Observer<T>
Represents an Observer and an Observable at the same time, allowing multicasting events from a single source to multiple child Observers.

All methods except the Observer.onSubscribe(io.reactivex.rxjava3.disposables.Disposable), Observer.onNext(Object), Observer.onError(Throwable) and Observer.onComplete() are thread-safe. Use toSerialized() to make these methods thread-safe as well.

  • Constructor Details

    • Subject

      public Subject()
  • Method Details

    • hasObservers

      @CheckReturnValue public abstract boolean hasObservers()
      Returns true if the subject has any Observers.

      The method is thread-safe.

      Returns:
      true if the subject has any Observers
    • hasThrowable

      @CheckReturnValue public abstract boolean hasThrowable()
      Returns true if the subject has reached a terminal state through an error event.

      The method is thread-safe.

      Returns:
      true if the subject has reached a terminal state through an error event
      See Also:
    • hasComplete

      @CheckReturnValue public abstract boolean hasComplete()
      Returns true if the subject has reached a terminal state through a complete event.

      The method is thread-safe.

      Returns:
      true if the subject has reached a terminal state through a complete event
      See Also:
    • getThrowable

      @Nullable @CheckReturnValue public abstract @Nullable Throwable getThrowable()
      Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.

      The method is thread-safe.

      Returns:
      the error that caused the Subject to terminate or null if the Subject hasn't terminated yet
    • toSerialized

      @NonNull @CheckReturnValue public final @NonNull Subject<T> toSerialized()
      Wraps this Subject and serializes the calls to the onSubscribe, onNext, onError and onComplete methods, making them thread-safe.

      The method is thread-safe.

      Returns:
      the wrapped and serialized subject