Enum SubscriptionHelper

    • Enum Constant Detail

      • 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 Detail

      • values

        public static SubscriptionHelper[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (SubscriptionHelper c : SubscriptionHelper.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static SubscriptionHelper valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (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 type has no constant with the specified name
        NullPointerException - if the argument is null
      • 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