Interface TargetState

  • All Known Implementing Classes:
    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 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));
     
    • 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 returns true, it will never return false again.
        Returns:
        true when the target state has been reached, false otherwise.
      • 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() returns true), though the loop can be controlled to exit prematurely using TargetState.TaskControl.EXIT. Any exceptions thrown by the task is caught and forwarded to the given handler, and then the loop is continued. If the exceptionHandler itself throws an exception, the loop is exited, and the exception is propagated to the caller of until(..).
        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() returns true). Any exceptions thrown by the task is caught and forwarded to the given handler, and then the loop is continued. If the exceptionHandler itself throws an exception, the loop is exited, and the exception is propagated to the caller of until(..).
        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.