Class OneTimeToggle

  • All Implemented Interfaces:
    TargetState

    public final class OneTimeToggle
    extends Object
    implements TargetState
    A kind of specialized variant of 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.
    See Also:
    TargetState
    • Constructor Detail

      • OneTimeToggle

        public OneTimeToggle()
    • Method Detail

      • now

        public void now()
        Toggle it! This will make yet() return true.
      • 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 returns true, it will never return false again.
        Specified by:
        yet in interface TargetState
        Returns:
        true when the target state has been reached, false otherwise.
      • nowAndUnlessAlreadyToggled

        public void nowAndUnlessAlreadyToggled​(Runnable action)
        Toggle it, to make 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.
        Parameters:
        action - the action to run if the toggle was actually switched.
      • nowAndUnlessAlreadyToggled

        public <T> Optional<T> nowAndUnlessAlreadyToggled​(Supplier<T> supplier)
        Toggle it, to make 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.

        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 always Optional.empty().