Class 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
AtomicBooleaninstead. -
This class supports only two states (
true /false ). If you need to represent three states (true /false /null ), useBooleanValueinstead.
Example:
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Flagcreate()Creates a new flag initialized tofalse .booleanCompares the specified object with this flag for equality.booleanSets the flag totrue and returns the previous value.booleanSets the flag tofalse and returns the previous value.inthashCode()Returns the hash code value for this flag.Executes a code snippet if the flag isfalse .Executes a code snippet if the flag istrue .booleanisSet()Returnstrue if the flag is set.booleanisUnset()Returnstrue if the flag is not set.static Flagof(boolean value) Creates a new flag with the specified initial state.set()Sets the flag totrue .setIf(boolean value) Sets the flag totrue if the specified value istrue .toString()Returns a string representation of this flag.unset()Sets the flag tofalse .
-
Method Details
-
create
Creates a new flag initialized tofalse .Example:
Flag
flag = Flag.create ();assertTrue (flag .isUnset());- Returns:
- A new flag.
-
of
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
Sets the flag totrue 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
Sets the flag tofalse 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
Executes a code snippet if the flag isfalse .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 isfalse .- Returns:
- This object.
-
ifSet
Executes a code snippet if the flag istrue .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 istrue .- Returns:
- This object.
-
isSet
Returnstrue if the flag is set.Example:
Flag
flag = Flag.of (true );assertTrue (flag .isSet());- Returns:
true if the flag is set,false otherwise.
-
isUnset
Returnstrue 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
Sets the flag totrue .Example:
Flag
flag = Flag.create ();flag .set();assertTrue (flag .isSet());- Returns:
- This object.
-
setIf
Sets the flag totrue if the specified value istrue .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- Iftrue , the flag will be set totrue .- Returns:
- This object.
-
unset
Sets the flag tofalse .Example:
Flag
flag = Flag.of (true );flag .unset();assertTrue (flag .isUnset());- Returns:
- This object.
-
toString
Returns a string representation of this flag.The format is simply the string representation of the boolean value.
-
equals
Compares the specified object with this flag for equality.Returns
true if and only if the specified object is also aFlag and both flags have the same boolean value. -
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 returns1231 fortrue and1237 forfalse .This ensures that
flag1.equals(flag2) implies thatflag1.hashCode()==flag2.hashCode() for any two flagsflag1 andflag2 , as required by the general contract ofObject.hashCode().
-