Class EndConsumerHelper

java.lang.Object
io.reactivex.rxjava3.internal.util.EndConsumerHelper

public final class EndConsumerHelper extends Object
Utility class to help report multiple subscriptions with the same consumer type instead of the internal "Disposable already set!" message that is practically reserved for internal operators and indicate bugs in them.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Utility class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Builds the error message with the consumer class.
    static void
    Report a ProtocolViolationException with a personalized message referencing the simple type name of the consumer class and report it via RxJavaPlugins.onError.
    static boolean
    setOnce(AtomicReference<Disposable> upstream, Disposable next, Class<?> observer)
    Atomically updates the target upstream AtomicReference from null to the non-null next Disposable, otherwise disposes next and reports a ProtocolViolationException if the AtomicReference doesn't contain the shared disposed indicator.
    static boolean
    setOnce(AtomicReference<org.reactivestreams.Subscription> upstream, org.reactivestreams.Subscription next, Class<?> subscriber)
    Atomically updates the target upstream AtomicReference from null to the non-null next Subscription, otherwise cancels next and reports a ProtocolViolationException if the AtomicReference doesn't contain the shared cancelled indicator.
    static boolean
    validate(Disposable upstream, Disposable next, Class<?> observer)
    Ensures that the upstream Disposable is null and returns true, otherwise disposes the next Disposable and if the upstream is not the shared disposed instance, reports a ProtocolViolationException due to multiple subscribe attempts.
    static boolean
    validate(org.reactivestreams.Subscription upstream, org.reactivestreams.Subscription next, Class<?> subscriber)
    Ensures that the upstream Subscription is null and returns true, otherwise cancels the next Subscription and if the upstream is not the shared cancelled instance, reports a ProtocolViolationException due to multiple subscribe attempts.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EndConsumerHelper

      private EndConsumerHelper()
      Utility class.
  • Method Details

    • validate

      public static boolean validate(Disposable upstream, Disposable next, Class<?> observer)
      Ensures that the upstream Disposable is null and returns true, otherwise disposes the next Disposable and if the upstream is not the shared disposed instance, reports a ProtocolViolationException due to multiple subscribe attempts.
      Parameters:
      upstream - the upstream current value
      next - the Disposable to check for nullness and dispose if necessary
      observer - the class of the consumer to have a personalized error message if the upstream already contains a non-cancelled Disposable.
      Returns:
      true if successful, false if the upstream was non null
    • setOnce

      public static boolean setOnce(AtomicReference<Disposable> upstream, Disposable next, Class<?> observer)
      Atomically updates the target upstream AtomicReference from null to the non-null next Disposable, otherwise disposes next and reports a ProtocolViolationException if the AtomicReference doesn't contain the shared disposed indicator.
      Parameters:
      upstream - the target AtomicReference to update
      next - the Disposable to set on it atomically
      observer - the class of the consumer to have a personalized error message if the upstream already contains a non-cancelled Disposable.
      Returns:
      true if successful, false if the content of the AtomicReference was non null
    • validate

      public static boolean validate(org.reactivestreams.Subscription upstream, org.reactivestreams.Subscription next, Class<?> subscriber)
      Ensures that the upstream Subscription is null and returns true, otherwise cancels the next Subscription and if the upstream is not the shared cancelled instance, reports a ProtocolViolationException due to multiple subscribe attempts.
      Parameters:
      upstream - the upstream current value
      next - the Subscription to check for nullness and cancel if necessary
      subscriber - the class of the consumer to have a personalized error message if the upstream already contains a non-cancelled Subscription.
      Returns:
      true if successful, false if the upstream was non null
    • setOnce

      public static boolean setOnce(AtomicReference<org.reactivestreams.Subscription> upstream, org.reactivestreams.Subscription next, Class<?> subscriber)
      Atomically updates the target upstream AtomicReference from null to the non-null next Subscription, otherwise cancels next and reports a ProtocolViolationException if the AtomicReference doesn't contain the shared cancelled indicator.
      Parameters:
      upstream - the target AtomicReference to update
      next - the Subscription to set on it atomically
      subscriber - the class of the consumer to have a personalized error message if the upstream already contains a non-cancelled Subscription.
      Returns:
      true if successful, false if the content of the AtomicReference was non null
    • composeMessage

      public static String composeMessage(String consumer)
      Builds the error message with the consumer class.
      Parameters:
      consumer - the class of the consumer
      Returns:
      the error message string
    • reportDoubleSubscription

      public static void reportDoubleSubscription(Class<?> consumer)
      Report a ProtocolViolationException with a personalized message referencing the simple type name of the consumer class and report it via RxJavaPlugins.onError.
      Parameters:
      consumer - the class of the consumer