Class BooleanValue

java.lang.Object
org.apache.juneau.commons.lang.Value<Boolean>
org.apache.juneau.commons.lang.BooleanValue

public class BooleanValue extends Value<Boolean>
A simple mutable boolean value.

This class extends Value<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 BooleanValue found = BooleanValue.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

    • BooleanValue

      public BooleanValue(Boolean value)
      Constructor.
      Parameters:
      value - The initial value.
  • Method Details

    • create

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

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

      Returns:
      A new boolean value.
    • of

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

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

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

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

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

      Example:

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

      Overrides:
      is in class Value<Boolean>
      Parameters:
      value - The value to compare to.
      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 Utils.eq(Object, Object) for deep equality comparison of each value.

      Example:

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

      Parameters:
      values - The values to compare to.
      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:

      BooleanValue value = BooleanValue.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:

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

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