Class BooleanValue
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:
Notes:
-
This class is not thread-safe. For concurrent access, use
AtomicBooleaninstead. -
This class supports three states (
true ,false ,null ). If you only need two states (true /false ), consider usingFlaginstead.
Example:
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BooleanValuecreate()Creates a new boolean value initialized tofalse .booleanChecks if the current value is equal to the specified value.booleanChecks if the current value matches any of the specified values.booleanReturnstrue if the value is not set totrue (i.e., it'sfalse ornull ).booleanisTrue()Returnstrue if the value is set totrue .static BooleanValueCreates a new boolean value with the specified initial value.
-
Constructor Details
-
BooleanValue
Constructor.- Parameters:
value- The initial value.
-
-
Method Details
-
create
Creates a new boolean value initialized tofalse .Example:
BooleanValue
value = BooleanValue.create ();assertEquals (false ,value .get());- Returns:
- A new boolean value.
-
of
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
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 BooleanValueempty = BooleanValue.of (null );assertTrue (empty .is(null )); -
isAny
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
Returnstrue if the value is not set totrue (i.e., it'sfalse ornull ).Example:
BooleanValue
value = BooleanValue.of (false );if (value .isNotTrue()) {log ("Value is not true" ); }- Returns:
true if the value is not set totrue ,false otherwise.
-
isTrue
Returnstrue if the value is set totrue .Example:
BooleanValue
value = BooleanValue.of (true );if (value .isTrue()) {log ("Value is true" ); }- Returns:
true if the value is set totrue ,false otherwise (including whennull ).
-