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.
An object which knows if a certain state has been
reached
yet.
The reference to a TargetState should
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 using untilThen(Runnable, Consumer):
TargetState isShutdown = ...
...
isShutdown.untilThen(() -> {
...
}, exception -> handle(exception));
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumControl signals returned from tasks to control execution withuntilThen(Supplier, Consumer) -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TargetStateA target state which is already reached.static final TargetStateA target state which will never be reached. -
Method Summary
Modifier and TypeMethodDescriptionstatic TargetStateall(Iterable<? extends TargetState> states) static TargetStateall(TargetState... states) default TargetStateor(TargetState that) default voidRun 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 Details
-
IMMEDIATELY
A target state which is already reached. -
NEVER
A target state which will never be reached.
-
-
Method Details
-
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
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
- Returns:
- a new TargetState which is reached when either this or the given state is reached.
-
all
- Returns:
- a new TargetState which is reached only when all the given states are reached.
-
all
- Returns:
- a new TargetState which is reached only when all the given states are reached.
-