Package no.digipost.concurrent
Class OneTimeToggle
java.lang.Object
no.digipost.concurrent.OneTimeToggle
- All Implemented Interfaces:
TargetState
A kind of specialized variant of
AtomicBoolean which
can only be toggled once. There is no way to "untoggle" it.
Successive attempts to toggle it with now() will have no effect,
but it is possible to enforce a fail-fast toggle only once policy using
nowOrIfAlreadyThenThrow(Supplier) which will throw an exception
if the toggle happens several times.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface no.digipost.concurrent.TargetState
TargetState.TaskControl -
Field Summary
Fields inherited from interface no.digipost.concurrent.TargetState
IMMEDIATELY, NEVER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidnow()Toggle it! This will makeyet()returntrue.voidnowAndUnlessAlreadyToggled(Runnable action) Toggle it, to makeyet()returntrue, and if a toggle was actually done (i.e.<T> Optional<T>nowAndUnlessAlreadyToggled(Supplier<T> supplier) Toggle it, to makeyet()returntrue, and if a toggle was actually done (i.e.<E extends Throwable>
voidnowOrIfAlreadyThenThrow(Supplier<E> exceptionSupplier) Toggle it, or throw exception if the toggle has already been done, for instance by another thread.booleanyet()Tell if the target state has been reached yet.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface no.digipost.concurrent.TargetState
or, untilThen, untilThen
-
Constructor Details
-
OneTimeToggle
public OneTimeToggle()
-
-
Method Details
-
now
public void now()Toggle it! This will makeyet()returntrue. -
nowOrIfAlreadyThenThrow
Toggle it, or throw exception if the toggle has already been done, for instance by another thread.- Type Parameters:
E- the exception type that this method may throw.- Parameters:
exceptionSupplier- supply exception to throw if it was already toggled.- Throws:
E extends Throwable
-
yet
public boolean yet()Description copied from interface:TargetStateTell if the target state has been reached yet. Once this method returnstrue, it will never returnfalseagain.- Specified by:
yetin interfaceTargetState- Returns:
truewhen the target state has been reached,falseotherwise.
-
nowAndUnlessAlreadyToggled
Toggle it, to makeyet()returntrue, and if a toggle was actually done (i.e. theOneTimeTogglehas not already been toggled), execute the provided action. This can be used to facilitate at-most-once execution semantics, even in a multi-threaded context.- Parameters:
action- the action to run if the toggle was actually switched.
-
nowAndUnlessAlreadyToggled
Toggle it, to makeyet()returntrue, and if a toggle was actually done (i.e. theOneTimeTogglehas not already been toggled), use the given supplier to resolve a value. This can be used to facilitate at-most-once execution semantics, even in a multi-threaded context.The given supplier may resolve a
nullvalue, and in that case the return value from this method will be indistinguishable from an invocation on an already toggledOneTimeToggle.- Type Parameters:
T- the type of the value resolved by the given supplier.- Parameters:
supplier- the supplier to resolve a value if the toggle was actually switched.- Returns:
- An
Optionalcontaining the value from the given supplier, if it was executed, otherwise alwaysOptional.empty().
-