Class BooleanHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<Boolean>
org.apache.juneau.commons.lang.BooleanHolder

public class BooleanHolder extends Holder<Boolean>
A simple mutable boolean value.

This class extends Holder<Boolean> and provides a convenient way to pass mutable boolean references to lambdas, inner classes, or methods. Unlike Flag, this class supports three states: true, false, and null.

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

// Create a boolean value to track if a condition was met BooleanHolder found = BooleanHolder.create(); // Use in a lambda list.forEach(x -> { if (x.matches(criteria)) { found.set(true); } }); if (found.isTrue()) { log("Match found!"); }

See Also:
  • Constructor Details

    • BooleanHolder

      public BooleanHolder(Boolean value)
      Constructor.
      Parameters:
      value - The initial value. Can be null.
  • Method Details

    • create

      public static BooleanHolder create()
      Creates a new boolean value initialized to false.
      Example:

      BooleanHolder value = BooleanHolder.create(); assertEquals(false, value.get());

      Returns:
      A new boolean value.
    • of

      public static BooleanHolder of(Boolean value)
      Creates a new boolean value with the specified initial value.
      Example:

      BooleanHolder value = BooleanHolder.of(true); assertEquals(true, value.get());

      Parameters:
      value - The initial value. Can be null.
      Returns:
      A new boolean value.
    • is

      public boolean is(Boolean value)
      Checks if the current value is equal to the specified value.

      Uses Shorts.eq(Object, Object) for deep equality comparison, which handles nulls safely.

      Example:

      BooleanHolder value = BooleanHolder.of(true); assertTrue(value.is(true)); assertFalse(value.is(false)); // Handles null safely BooleanHolder empty = BooleanHolder.of(null); assertTrue(empty.is(null));

      Overrides:
      is in class Holder<Boolean>
      Parameters:
      value - The value to compare to. Can be null.
      Returns:
      true if the current value is equal to the specified value.
    • isAny

      public boolean isAny(Boolean... values)
      Checks if the current value matches any of the specified values.

      Uses Shorts.eq(Object, Object) for deep equality comparison of each value.

      Example:

      BooleanHolder value = BooleanHolder.of(true); assertTrue(value.isAny(true, null)); assertFalse(value.isAny(false));

      Parameters:
      values - The values to compare to. Must not be null.
      Returns:
      true if the current value matches any of the specified values.
    • isNotTrue

      public boolean isNotTrue()
      Returns true if the value is not set to true (i.e., it's false or null).
      Example:

      BooleanHolder value = BooleanHolder.of(false); if (value.isNotTrue()) { log("Value is not true"); }

      Returns:
      true if the value is not set to true, false otherwise.
    • isTrue

      public boolean isTrue()
      Returns true if the value is set to true.
      Example:

      BooleanHolder value = BooleanHolder.of(true); if (value.isTrue()) { log("Value is true"); }

      Returns:
      true if the value is set to true, false otherwise (including when null).