Package no.digipost.concurrent
Interface TargetState
-
- All Known Implementing Classes:
CountDown,OneTimeToggle
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface TargetState
An object which knows if a certain state has been reachedyet.The reference to a
TargetStateshould be named as the state it models when the state has been reached.E.g:
TargetState isShutdown = ... ... if(isShutdown.yet()) { ... }Or to run a repeating task until the target state is reached usinguntilThen(Runnable, Consumer):TargetState isShutdown = ... ... isShutdown.untilThen(() -> { ... }, exception -> handle(exception));
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classTargetState.TaskControlControl signals returned from tasks to control execution withuntilThen(Supplier, Consumer)
-
Field Summary
Fields Modifier and Type Field Description static TargetStateIMMEDIATELYA target state which is already reached.static TargetStateNEVERA target state which will never be reached.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static TargetStateall(Iterable<? extends TargetState> states)static TargetStateall(TargetState... states)default TargetStateor(TargetState that)default voiduntilThen(Runnable loopingTask, Consumer<? super Exception> exceptionHandler)Run a task in a loop until the target state is reached (yet()returnstrue).default voiduntilThen(Supplier<TargetState.TaskControl> loopingTask, Consumer<? super Exception> exceptionHandler)Run a task in a loop until the target state is reached (yet()returnstrue), though the loop can be controlled to exit prematurely usingTargetState.TaskControl.EXIT.booleanyet()Tell if the target state has been reached yet.
-
-
-
Field Detail
-
IMMEDIATELY
static final TargetState IMMEDIATELY
A target state which is already reached.
-
NEVER
static final TargetState NEVER
A target state which will never be reached.
-
-
Method Detail
-
yet
boolean yet()
Tell if the target state has been reached yet. Once this method returnstrue, it will never returnfalseagain.- Returns:
truewhen the target state has been reached,falseotherwise.
-
untilThen
default void untilThen(Supplier<TargetState.TaskControl> loopingTask, Consumer<? super Exception> exceptionHandler)
Run a task in a loop until the target state is reached (yet()returnstrue), though the loop can be controlled to exit prematurely usingTargetState.TaskControl.EXIT. Any exceptions thrown by the task is caught and forwarded to the given handler, and then the loop is continued. If theexceptionHandleritself throws an exception, the loop is exited, and the exception is propagated to the caller ofuntil(..).- Parameters:
loopingTask- the task to run repeatedly.exceptionHandler- the exception handler.
-
untilThen
default void untilThen(Runnable loopingTask, Consumer<? super Exception> exceptionHandler)
Run a task in a loop until the target state is reached (yet()returnstrue). Any exceptions thrown by the task is caught and forwarded to the given handler, and then the loop is continued. If theexceptionHandleritself throws an exception, the loop is exited, and the exception is propagated to the caller ofuntil(..).- Parameters:
loopingTask- the task to run repeatedly.exceptionHandler- the exception handler.
-
or
default TargetState or(TargetState that)
- Returns:
- a new TargetState which is reached when either this or the given state is reached.
-
all
static TargetState all(TargetState... states)
- Returns:
- a new TargetState which is reached only when all the given states are reached.
-
all
static TargetState all(Iterable<? extends TargetState> states)
- Returns:
- a new TargetState which is reached only when all the given states are reached.
-
-