public final class OneTimeToggle extends Object implements TargetState
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.TargetStateTargetState.TaskControlIMMEDIATELY, NEVER| Constructor and Description |
|---|
OneTimeToggle() |
| Modifier and Type | Method and Description |
|---|---|
void |
now()
Toggle it! This will make
yet() return true. |
void |
nowAndUnlessAlreadyToggled(Runnable action)
Toggle it, to make
yet() return true, and if
a toggle was actually done (i.e. |
<T> Optional<T> |
nowAndUnlessAlreadyToggled(Supplier<T> supplier)
Toggle it, to make
yet() return true, and if
a toggle was actually done (i.e. |
<E extends Throwable> |
nowOrIfAlreadyThenThrow(Supplier<E> exceptionSupplier)
Toggle it, or throw exception if the toggle has already been done, for instance
by another thread.
|
boolean |
yet()
Tell if the target state has been reached yet.
|
public void now()
yet() return true.public <E extends Throwable> void nowOrIfAlreadyThenThrow(Supplier<E> exceptionSupplier) throws E extends Throwable
E - the exception type that this method may throw.exceptionSupplier - supply exception to throw if it was already toggled.E extends Throwablepublic boolean yet()
TargetStatetrue, it will never return false again.yet in interface TargetStatetrue when the target state has been reached,
false otherwise.public void nowAndUnlessAlreadyToggled(Runnable action)
yet() return true, and if
a toggle was actually done (i.e. the OneTimeToggle has 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.action - the action to run if the toggle was actually switched.public <T> Optional<T> nowAndUnlessAlreadyToggled(Supplier<T> supplier)
yet() return true, and if
a toggle was actually done (i.e. the OneTimeToggle has 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 null value, and in that case the return
value from this method will be indistinguishable from an invocation on an already toggled
OneTimeToggle.
T - the type of the value resolved by the given supplier.supplier - the supplier to resolve a value if the toggle was actually switched.Optional containing the value from the given supplier, if it was
executed, otherwise always Optional.empty().Copyright © 2019 Digipost. All rights reserved.