Enum Class SubscriptionHelper

java.lang.Object
java.lang.Enum<SubscriptionHelper>
io.helidon.common.reactive.SubscriptionHelper
All Implemented Interfaces:
Serializable, Comparable<SubscriptionHelper>, Constable, Flow.Subscription

public enum SubscriptionHelper extends Enum<SubscriptionHelper> implements Flow.Subscription
Helper enum with a singleton cancellation indicator and utility methods to perform atomic actions on Flow.Subscriptions.
  • Enum Constant Details

    • CANCELED

      public static final SubscriptionHelper CANCELED
      The singleton instance indicating a canceled subscription.
    • BAD_REQUEST

      public static final SubscriptionHelper BAD_REQUEST
      The singleton instance indicating a subscription requested with non-positive.
  • Method Details

    • values

      public static SubscriptionHelper[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static SubscriptionHelper valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • request

      public void request(long n)
      Specified by:
      request in interface Flow.Subscription
    • cancel

      public void cancel()
      Specified by:
      cancel in interface Flow.Subscription
    • addRequest

      public static long addRequest(AtomicLong field, long n)
      Atomically add the given request amount to the field while capping it at Long.MAX_VALUE.
      Parameters:
      field - the target field to update
      n - the request amount to add, must be positive (not verified)
      Returns:
      the old request amount after the operation
    • produced

      public static long produced(AtomicLong field, long n)
      Atomically subtract the given number from the field if that field is not already at Long.MAX_VALUE and return the new value.
      Parameters:
      field - the target field to update
      n - the number to subtract
      Returns:
      the new value after the subtraction
      Throws:
      IllegalStateException - if n is bigger than the field's value, which indicates an operator bug
    • setOnce

      public static boolean setOnce(AtomicReference<Flow.Subscription> subscriptionField, Flow.Subscription upstream)
      Atomically sets the only upstream subscription in the field.
      Parameters:
      subscriptionField - the field to store the only upstream subscription
      upstream - the only upstream to set and request from
      Returns:
      true if the operation succeeded, false if the field holds the cancellation indicator
      Throws:
      IllegalStateException - if the subscriptionField already contains a non-canceled subscription instance
    • deferredSetOnce

      public static boolean deferredSetOnce(AtomicReference<Flow.Subscription> subscriptionField, AtomicLong requestedField, Flow.Subscription upstream)
      Atomically sets the only upstream subscription in the field and then requests the amount accumulated in the requestedField.
      Parameters:
      subscriptionField - the field to store the only upstream subscription
      requestedField - the request amounts accumulated so far
      upstream - the only upstream to set and request from
      Returns:
      true if the operation succeeded, false if the field indicated the upstream should be cancelled immediately
      Throws:
      IllegalStateException - if the subscriptionField already contains a non-canceled subscription instance
    • deferredRequest

      public static void deferredRequest(AtomicReference<Flow.Subscription> subscriptionField, AtomicLong requestedField, long n)
      Accumulates request amounts until the subscription field receives a Subscription instance, then requests this accumulated amount and forwards subsequent requests to it.
      Parameters:
      subscriptionField - the field possibly containing a Subscription instance.
      requestedField - the field used for accumulating requests until the Subscription instance arrives
      n - the request amount to accumulate or forward, must be positive (not verified)
    • cancel

      public static boolean cancel(AtomicReference<Flow.Subscription> subscriptionField)
      Atomically swap in the CANCELED instance and call cancel() on any previous Subscription held.
      Parameters:
      subscriptionField - the target field to cancel atomically.
      Returns:
      true if the current thread succeeded with the cancellation (as only one thread is able to)
    • badRequest

      public static boolean badRequest(AtomicReference<Flow.Subscription> subscriptionField)
      Atomically swap in the BAD_REQUEST instance, if and only if previous value is null.
      Parameters:
      subscriptionField - the target field to swap atomically.
      Returns:
      true if swap was successful
    • validate

      public static void validate(Flow.Subscription current, Flow.Subscription incoming)
      Check if current is null and incoming is not null.
      Parameters:
      current - the current subscription, should be null
      incoming - the incoming subscription, should be non-null
      Throws:
      IllegalStateException - if current is not-null indicating a bug in an operator calling onSubscribe more than once
    • replace

      public static void replace(AtomicReference<Flow.Subscription> field, Flow.Subscription incoming)
      Atomically swap in the given incoming Flow.Subscription or cancel it if the target field is canceled.
      Parameters:
      field - the target field
      incoming - the incoming subscription, nulls allowed