Class LockedBoolean

java.lang.Object
org.nanonative.nano.helper.LockedBoolean

public class LockedBoolean extends Object
A thread-safe class that manages a boolean state with synchronized actions. Unlike AtomicBoolean, LockedBoolean allows executing complex synchronized actions based on the current state and conditionally updating the state within a locked context. This class is designed for scenarios where actions need to be performed during state changes, ensuring thread safety and atomicity of operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new LockedBoolean with the initial state set to false.
    LockedBoolean(boolean state)
    Constructs a new LockedBoolean with the specified initial state.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    get()
    Retrieves the current state with thread safety.
    void
    run(boolean when, Consumer<Boolean> then)
    Executes a Consumer based on a condition without changing the state.
    void
    Executes a Consumer based on the current state.
    void
    set(boolean state)
    Sets the state to the specified value with thread safety.
    void
    set(boolean when, boolean then)
    Conditionally sets the state based on a condition.
    void
    set(boolean when, boolean then, Consumer<Boolean> run)
    Conditionally sets the state and executes a Consumer based on a condition.
    void
    set(boolean state, Consumer<Boolean> run)
    Sets the state to the specified value with thread safety.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LockedBoolean

      public LockedBoolean()
      Constructs a new LockedBoolean with the initial state set to false.
    • LockedBoolean

      public LockedBoolean(boolean state)
      Constructs a new LockedBoolean with the specified initial state.
      Parameters:
      state - the initial state
  • Method Details

    • set

      public void set(boolean state)
      Sets the state to the specified value with thread safety.
      Parameters:
      state - the new state value
    • set

      public void set(boolean state, Consumer<Boolean> run)
      Sets the state to the specified value with thread safety.
      Parameters:
      state - the new state value
      run - The Consumer to execute while setting new state.
    • set

      public void set(boolean when, boolean then)
      Conditionally sets the state based on a condition.
      Parameters:
      when - The condition that determines if the state should be set to then.
      then - The state to set if when is true.
    • set

      public void set(boolean when, boolean then, Consumer<Boolean> run)
      Conditionally sets the state and executes a Consumer based on a condition.
      Parameters:
      when - The condition that determines if the state should be set to then.
      then - The state to set if when is true.
      run - The Consumer to execute if the condition when is true.
    • run

      public void run(Consumer<Boolean> run)
      Executes a Consumer based on the current state.
      Parameters:
      run - The Runnable to execute if the current state is true.
    • run

      public void run(boolean when, Consumer<Boolean> then)
      Executes a Consumer based on a condition without changing the state.
      Parameters:
      when - The condition that determines which Runnable to execute.
      then - The Consumer to execute if when is true.
    • get

      public boolean get()
      Retrieves the current state with thread safety.
      Returns:
      the current boolean state
    • toString

      public String toString()
      Overrides:
      toString in class Object