Class BooleanHolder
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:
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 BooleanHoldercreate()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 BooleanHolderCreates a new boolean value with the specified initial value.
-
Constructor Details
-
BooleanHolder
Constructor.- Parameters:
value- The initial value. Can benull .
-
-
Method Details
-
create
Creates a new boolean value initialized tofalse .Example:
BooleanHolder
value = BooleanHolder.create ();assertEquals (false ,value .get());- Returns:
- A new boolean value.
-
of
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 benull .- Returns:
- A new boolean value.
-
is
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 BooleanHolderempty = BooleanHolder.of (null );assertTrue (empty .is(null )); -
isAny
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 benull .- 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:
BooleanHolder
value = BooleanHolder.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:
BooleanHolder
value = BooleanHolder.of (true );if (value .isTrue()) {log ("Value is true" ); }- Returns:
true if the value is set totrue ,false otherwise (including whennull ).
-