Package no.digipost.concurrent
Class OneTimeToggle
- java.lang.Object
-
- no.digipost.concurrent.OneTimeToggle
-
- All Implemented Interfaces:
TargetState
public final class OneTimeToggle extends Object implements TargetState
A kind of specialized variant ofAtomicBoolean
which can only be toggled once. There is no way to "untoggle" it. Successive attempts to toggle it withnow()
will have no effect, but it is possible to enforce a fail-fast toggle only once policy usingnowOrIfAlreadyThenThrow(Supplier)
which will throw an exception if the toggle happens several times.- See Also:
TargetState
-
-
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 Constructor Description OneTimeToggle()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
now()
Toggle it! This will makeyet()
returntrue
.void
nowAndUnlessAlreadyToggled(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.boolean
yet()
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, wait
-
Methods inherited from interface no.digipost.concurrent.TargetState
or, untilThen, untilThen
-
-
-
-
Method Detail
-
now
public void now()
Toggle it! This will makeyet()
returntrue
.
-
nowOrIfAlreadyThenThrow
public <E extends Throwable> void nowOrIfAlreadyThenThrow(Supplier<E> exceptionSupplier) throws E extends Throwable
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:TargetState
Tell if the target state has been reached yet. Once this method returnstrue
, it will never returnfalse
again.- Specified by:
yet
in interfaceTargetState
- Returns:
true
when the target state has been reached,false
otherwise.
-
nowAndUnlessAlreadyToggled
public void nowAndUnlessAlreadyToggled(Runnable action)
Toggle it, to makeyet()
returntrue
, and if a toggle was actually done (i.e. theOneTimeToggle
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.- Parameters:
action
- the action to run if the toggle was actually switched.
-
nowAndUnlessAlreadyToggled
public <T> Optional<T> nowAndUnlessAlreadyToggled(Supplier<T> supplier)
Toggle it, to makeyet()
returntrue
, and if a toggle was actually done (i.e. theOneTimeToggle
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 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
Optional
containing the value from the given supplier, if it was executed, otherwise alwaysOptional.empty()
.
-
-