- All Implemented Interfaces:
Serializable
,Comparable<SubscriptionHelper>
,Constable
,Flow.Subscription
Helper enum with a singleton cancellation indicator and utility methods to perform
atomic actions on
Flow.Subscription
s.-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionThe singleton instance indicating a subscription requested with non-positive.The singleton instance indicating a canceled subscription. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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
Returns the enum constant of this class with the specified name.static SubscriptionHelper[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
CANCELED
The singleton instance indicating a canceled subscription. -
BAD_REQUEST
The singleton instance indicating a subscription requested with non-positive.
-
-
Method Details
-
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
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 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
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
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
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
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
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
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
-