- java.lang.Object
-
- java.lang.Enum<SubscriptionHelper>
-
- io.helidon.common.reactive.SubscriptionHelper
-
- All Implemented Interfaces:
Serializable
,Comparable<SubscriptionHelper>
,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 onFlow.Subscription
s.
-
-
Enum Constant Summary
Enum Constants Enum Constant Description BAD_REQUEST
The singleton instance indicating a subscription requested with non-positive.CANCELED
The singleton instance indicating a canceled subscription.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static long
addRequest(AtomicLong field, long n)
Atomically add the given request amount to the field while capping it atLong.MAX_VALUE
.static boolean
badRequest(AtomicReference<Flow.Subscription> subscriptionField)
Atomically swap in theBAD_REQUEST
instance, if and only if previous value is null.void
cancel()
static boolean
cancel(AtomicReference<Flow.Subscription> subscriptionField)
Atomically swap in theCANCELED
instance and call cancel() on any previous Subscription held.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.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.static long
produced(AtomicLong field, long n)
Atomically subtract the given number from the field if that field is not already atLong.MAX_VALUE
and return the new value.static void
replace(AtomicReference<Flow.Subscription> field, Flow.Subscription incoming)
Atomically swap in the given incomingFlow.Subscription
or cancel it if the target field is canceled.void
request(long n)
static boolean
setOnce(AtomicReference<Flow.Subscription> subscriptionField, Flow.Subscription upstream)
Atomically sets the only upstream subscription in the field.static void
validate(Flow.Subscription current, Flow.Subscription incoming)
Check if current is null and incoming is not null.static SubscriptionHelper
valueOf(String name)
Returns the enum constant of this type with the specified name.static SubscriptionHelper[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
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 nameNullPointerException
- if the argument is null
-
request
public void request(long n)
- Specified by:
request
in interfaceFlow.Subscription
-
cancel
public void cancel()
- Specified by:
cancel
in interfaceFlow.Subscription
-
addRequest
public static long addRequest(AtomicLong field, long n)
Atomically add the given request amount to the field while capping it atLong.MAX_VALUE
.- Parameters:
field
- the target field to updaten
- 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 atLong.MAX_VALUE
and return the new value.- Parameters:
field
- the target field to updaten
- 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 subscriptionupstream
- 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 subscriptionrequestedField
- the request amounts accumulated so farupstream
- 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 arrivesn
- the request amount to accumulate or forward, must be positive (not verified)
-
cancel
public static boolean cancel(AtomicReference<Flow.Subscription> subscriptionField)
Atomically swap in theCANCELED
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 theBAD_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 nullincoming
- 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 incomingFlow.Subscription
or cancel it if the target field is canceled.- Parameters:
field
- the target fieldincoming
- the incoming subscription, nulls allowed
-
-