Class Flag

java.lang.Object
org.apache.juneau.commons.lang.Flag

public class Flag extends Object
A simple mutable boolean flag.

This class provides a thread-unsafe alternative to AtomicBoolean for cases where atomic operations are not required. It is useful in situations where you need to pass a mutable boolean reference to lambdas, inner classes, or methods.

Notes:
  • This class is not thread-safe. For concurrent access, use AtomicBoolean instead.
  • This class supports only two states (true/false). If you need to represent three states (true/false/null), use BooleanValue instead.
Example:

// Create a flag to track if an operation was performed Flag processed = Flag.create(); // Use in a lambda list.forEach(x -> { if (x.needsProcessing()) { processed.set(); process(x); } }); if (processed.isSet()) { log("Processing completed"); }

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Flag
    Creates a new flag initialized to false.
    boolean
    Compares the specified object with this flag for equality.
    boolean
    Sets the flag to true and returns the previous value.
    boolean
    Sets the flag to false and returns the previous value.
    int
    Returns the hash code value for this flag.
    ifNotSet(Snippet snippet)
    Executes a code snippet if the flag is false.
    ifSet(Snippet snippet)
    Executes a code snippet if the flag is true.
    boolean
    Returns true if the flag is set.
    boolean
    Returns true if the flag is not set.
    static Flag
    of(boolean value)
    Creates a new flag with the specified initial state.
    set()
    Sets the flag to true.
    setIf(boolean value)
    Sets the flag to true if the specified value is true.
    Returns a string representation of this flag.
    Sets the flag to false.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • create

      public static Flag create()
      Creates a new flag initialized to false.
      Example:

      Flag flag = Flag.create(); assertTrue(flag.isUnset());

      Returns:
      A new flag.
    • of

      public static Flag of(boolean value)
      Creates a new flag with the specified initial state.
      Example:

      Flag flag = Flag.of(true); assertTrue(flag.isSet());

      Parameters:
      value - The initial state of the flag.
      Returns:
      A new flag.
    • getAndSet

      public boolean getAndSet()
      Sets the flag to true and returns the previous value.
      Example:

      Flag flag = Flag.create(); boolean wasSet = flag.getAndSet(); // Returns false, flag is now true boolean wasSet2 = flag.getAndSet(); // Returns true, flag remains true

      Returns:
      The value before it was set to true.
    • getAndUnset

      public boolean getAndUnset()
      Sets the flag to false and returns the previous value.
      Example:

      Flag flag = Flag.of(true); boolean wasSet = flag.getAndUnset(); // Returns true, flag is now false boolean wasSet2 = flag.getAndUnset(); // Returns false, flag remains false

      Returns:
      The value before it was set to false.
    • ifNotSet

      public Flag ifNotSet(Snippet snippet)
      Executes a code snippet if the flag is false.

      This method is useful for conditional execution based on the flag state, particularly in lambda expressions or method chains.

      Example:

      Flag initialized = Flag.create(); // Initialize only once initialized.ifNotSet(() -> { initialize(); initialized.set(); });

      Parameters:
      snippet - The code snippet to execute if the flag is false.
      Returns:
      This object.
    • ifSet

      public Flag ifSet(Snippet snippet)
      Executes a code snippet if the flag is true.

      This method is useful for conditional execution based on the flag state, particularly in lambda expressions or method chains.

      Example:

      Flag hasErrors = Flag.create(); // Log only if errors occurred hasErrors.ifSet(() -> logErrors());

      Parameters:
      snippet - The code snippet to execute if the flag is true.
      Returns:
      This object.
    • isSet

      public boolean isSet()
      Returns true if the flag is set.
      Example:

      Flag flag = Flag.of(true); assertTrue(flag.isSet());

      Returns:
      true if the flag is set, false otherwise.
    • isUnset

      public boolean isUnset()
      Returns true if the flag is not set.
      Example:

      Flag flag = Flag.create(); assertTrue(flag.isUnset());

      Returns:
      true if the flag is not set, false otherwise.
    • set

      public Flag set()
      Sets the flag to true.
      Example:

      Flag flag = Flag.create(); flag.set(); assertTrue(flag.isSet());

      Returns:
      This object.
    • setIf

      public Flag setIf(boolean value)
      Sets the flag to true if the specified value is true.

      This method uses a logical OR operation, so once the flag is set, it remains set regardless of subsequent calls with false.

      Example:

      Flag flag = Flag.create(); flag.setIf(false); // Flag remains false flag.setIf(true); // Flag becomes true flag.setIf(false); // Flag remains true

      Parameters:
      value - If true, the flag will be set to true.
      Returns:
      This object.
    • unset

      public Flag unset()
      Sets the flag to false.
      Example:

      Flag flag = Flag.of(true); flag.unset(); assertTrue(flag.isUnset());

      Returns:
      This object.
    • toString

      public String toString()
      Returns a string representation of this flag.

      The format is simply the string representation of the boolean value.

      Overrides:
      toString in class Object
      Returns:
      A string representation of this flag.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this flag for equality.

      Returns true if and only if the specified object is also a Flag and both flags have the same boolean value.

      Overrides:
      equals in class Object
      Parameters:
      o - The object to be compared for equality with this flag.
      Returns:
      true if the specified object is equal to this flag.
    • hashCode

      public int hashCode()
      Returns the hash code value for this flag.

      The hash code is computed from the boolean value using the standard Boolean.hashCode(boolean) method, which returns 1231 for true and 1237 for false.

      This ensures that flag1.equals(flag2) implies that flag1.hashCode()==flag2.hashCode() for any two flags flag1 and flag2, as required by the general contract of Object.hashCode().

      Overrides:
      hashCode in class Object
      Returns:
      The hash code value for this flag.