public final class

BehaviorProcessor

extends FlowableProcessor<T>
java.lang.Object
   ↳ io.reactivex.Flowable<T>
     ↳ io.reactivex.processors.FlowableProcessor<T>
       ↳ io.reactivex.processors.BehaviorProcessor<T>

Class Overview

Processor that emits the most recent item it has observed and all subsequent observed items to each subscribed Subscriber.

Example usage:

 // observer will receive all events.
  BehaviorProcessor<Object> processor = BehaviorProcessor.create("default");
  processor.subscribe(observer);
  processor.onNext("one");
  processor.onNext("two");
  processor.onNext("three");

  // observer will receive the "one", "two" and "three" events, but not "zero"
  BehaviorProcessor<Object> processor = BehaviorProcessor.create("default");
  processor.onNext("zero");
  processor.onNext("one");
  processor.subscribe(observer);
  processor.onNext("two");
  processor.onNext("three");

  // observer will receive only onComplete
  BehaviorProcessor<Object> processor = BehaviorProcessor.create("default");
  processor.onNext("zero");
  processor.onNext("one");
  processor.onComplete();
  processor.subscribe(observer);

  // observer will receive only onError
  BehaviorProcessor<Object> processor = BehaviorProcessor.create("default");
  processor.onNext("zero");
  processor.onNext("one");
  processor.onError(new RuntimeException("error"));
  processor.subscribe(observer);
   

Summary

Public Methods
static <T> BehaviorProcessor<T> create()
Creates a BehaviorProcessor without a default item.
static <T> BehaviorProcessor<T> createDefault(T defaultValue)
Creates a BehaviorProcessor that emits the last item it observed and all subsequent items to each Subscriber that subscribes to it.
Throwable getThrowable()
Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.
T getValue()
Returns a single value the Subject currently has or null if no such value exists.
T[] getValues(T[] array)
Returns a typed array containing a snapshot of all values of the Subject.
Object[] getValues()
Returns an Object array containing snapshot all values of the Subject.
boolean hasComplete()
Returns true if the subject has reached a terminal state through a complete event.
boolean hasSubscribers()
Returns true if the subject has subscribers.
boolean hasThrowable()
Returns true if the subject has reached a terminal state through an error event.
boolean hasValue()
Returns true if the subject has any value.
boolean offer(T t)
Tries to emit the item to all currently subscribed Subscribers if all of them has requested some value, returns false otherwise.
void onComplete()
void onError(Throwable t)
void onNext(T t)
void onSubscribe(Subscription s)
Protected Methods
void subscribeActual(Subscriber<? super T> s)
Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic.
[Expand]
Inherited Methods
From class io.reactivex.processors.FlowableProcessor
From class io.reactivex.Flowable
From class java.lang.Object
From interface io.reactivex.FlowableSubscriber
From interface org.reactivestreams.Publisher
From interface org.reactivestreams.Subscriber

Public Methods

public static BehaviorProcessor<T> create ()

Creates a BehaviorProcessor without a default item.

Returns

public static BehaviorProcessor<T> createDefault (T defaultValue)

Creates a BehaviorProcessor that emits the last item it observed and all subsequent items to each Subscriber that subscribes to it.

Parameters
defaultValue the item that will be emitted first to any Subscriber as long as the BehaviorProcessor has not yet observed any items from its source Observable
Returns

public 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

public T getValue ()

Returns a single value the Subject currently has or null if no such value exists.

The method is thread-safe.

Returns
  • a single value the Subject currently has or null if no such value exists

public T[] getValues (T[] array)

Returns a typed array containing a snapshot of all values of the Subject.

The method follows the conventions of Collection.toArray by setting the array element after the last value to null (if the capacity permits).

The method is thread-safe.

Parameters
array the target array to copy values into if it fits
Returns
  • the given array if the values fit into it or a new array containing all values

public Object[] getValues ()

Returns an Object array containing snapshot all values of the Subject.

The method is thread-safe.

Returns
  • the array containing the snapshot of all values of the Subject

public 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

public boolean hasSubscribers ()

Returns true if the subject has subscribers.

The method is thread-safe.

Returns
  • true if the subject has subscribers

public 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

public boolean hasValue ()

Returns true if the subject has any value.

The method is thread-safe.

Returns
  • true if the subject has any value

public boolean offer (T t)

Tries to emit the item to all currently subscribed Subscribers if all of them has requested some value, returns false otherwise.

This method should be called in a sequential manner just like the onXXX methods of the PublishProcessor.

Calling with null will terminate the PublishProcessor and a NullPointerException is signalled to the Subscribers.

Parameters
t the item to emit, not null
Returns
  • true if the item was emitted to all Subscribers

public void onComplete ()

public void onError (Throwable t)

public void onNext (T t)

public void onSubscribe (Subscription s)

Protected Methods

protected void subscribeActual (Subscriber<? super T> s)

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic.

There is no need to call any of the plugin hooks on the current Flowable instance or the Subscriber.

Parameters
s the incoming Subscriber, never null