Class NotificationLite<T>

java.lang.Object
rx.internal.operators.NotificationLite<T>
Type Parameters:
T - the element type

public final class NotificationLite<T> extends Object
For use in internal operators that need something like materialize and dematerialize wholly within the implementation of the operator but don't want to incur the allocation cost of actually creating Notification objects for every onNext and onCompleted.

An object is allocated inside error(Throwable) to wrap the Throwable but this shouldn't affect performance because exceptions should be exceptionally rare.

It's implemented as a singleton to maintain some semblance of type safety that is completely non-existent.

  • Field Details

    • INSTANCE

      private static final NotificationLite INSTANCE
    • ON_COMPLETED_SENTINEL

      private static final Object ON_COMPLETED_SENTINEL
    • ON_NEXT_NULL_SENTINEL

      private static final Object ON_NEXT_NULL_SENTINEL
  • Constructor Details

    • NotificationLite

      private NotificationLite()
  • Method Details

    • instance

      public static <T> NotificationLite<T> instance()
      Gets the NotificationLite singleton.
      Type Parameters:
      T - the value type
      Returns:
      the sole NotificationLite object
    • next

      public Object next(T t)
      Creates a lite onNext notification for the value passed in without doing any allocation. Can be unwrapped and sent with the accept(rx.Observer<? super T>, java.lang.Object) method.
      Parameters:
      t - the item emitted to onNext
      Returns:
      the item, or a null token representing the item if the item is null
    • completed

      public Object completed()
      Creates a lite onCompleted notification without doing any allocation. Can be unwrapped and sent with the accept(rx.Observer<? super T>, java.lang.Object) method.
      Returns:
      a completion token
    • error

      public Object error(Throwable e)
      Create a lite onError notification. This call creates an object to wrap the Throwable, but since there should only be one of these, the performance impact should be small. Can be unwrapped and sent with the accept(rx.Observer<? super T>, java.lang.Object) method.
      Parameters:
      e - the Throwable in the onError notification
      Returns:
      an object encapsulating the exception
    • accept

      public boolean accept(Observer<? super T> o, Object n)
      Unwraps the lite notification and calls the appropriate method on the Observer.
      Parameters:
      o - the Observer to call onNext, onCompleted, or onError.
      n - the lite notification
      Returns:
      true if n represents a termination event; false otherwise
      Throws:
      IllegalArgumentException - if the notification is null.
      NullPointerException - if the Observer is null.
    • isCompleted

      public boolean isCompleted(Object n)
      Indicates whether or not the lite notification represents an onCompleted event.
      Parameters:
      n - the lite notification
      Returns:
      true if n represents an onCompleted event; false otherwise
    • isError

      public boolean isError(Object n)
      Indicates whether or not the lite notification represents an onError event.
      Parameters:
      n - the lite notification
      Returns:
      true if n represents an onError event; false otherwise
    • isNull

      public boolean isNull(Object n)
      Indicates whether or not the lite notification represents a wrapped null onNext event.
      Parameters:
      n - the lite notification
      Returns:
      true if n represents a wrapped null onNext event, false otherwise
    • isNext

      public boolean isNext(Object n)
      Indicates whether or not the lite notification represents an onNext event.
      Parameters:
      n - the lite notification
      Returns:
      true if n represents an onNext event, false otherwise
    • kind

      public Notification.Kind kind(Object n)
      Indicates which variety a particular lite notification is. If you need something more complex than simply calling the right method on an Observer then you can use this method to get the Notification.Kind.
      Parameters:
      n - the lite notification
      Returns:
      the Notification.Kind of lite notification n is: either Kind.OnCompleted, Kind.OnError, or Kind.OnNext
      Throws:
      IllegalArgumentException - if the notification is null.
    • getValue

      public T getValue(Object n)
      Returns the item corresponding to this OnNext lite notification. Bad things happen if you pass this an OnComplete or OnError notification type. For performance reasons, this method does not check for this, so you are expected to prevent such a mishap.
      Parameters:
      n - the lite notification (of type Kind.OnNext)
      Returns:
      the unwrapped value, which can be null
    • getError

      public Throwable getError(Object n)
      Returns the Throwable corresponding to this OnError lite notification. Bad things happen if you pass this an OnComplete or OnNext notification type. For performance reasons, this method does not check for this, so you are expected to prevent such a mishap.
      Parameters:
      n - the lite notification (of type Kind.OnError)
      Returns:
      the Throwable wrapped inside n