fipaos.util
Class FlagHandler


public class FlagHandler
implements Serializable

FlagHandler provides an easy method of using bitwise flags to store state, minimising the use of multiple booleans for state management. FlagHandler is thread safe - no race conditions can occur when accessing the same FlagHandler from multiple threads.


Constructor Summary
FlagHandler()
          Constructs an empty FlagHandler - there are intially no flags set or known about.

Method Summary
 synchronized booleanaddFlag(String flag)
          Add a new flag to the handler.
 synchronized booleanaddFlag(String flag, String mxflag)
          Add a new flag to the handler.
 synchronized booleanaddFlag(String flag, String[] mxflags)
          Add a new flag to the handler.
 synchronized voidclearAllFlags()
          Sets all of the flags to false (clears every flag);
 synchronized voidclearFlag(String flag)
          Clears a flag (set it to false).
 booleancontainsFlag(String flag)
          Checks to see if this handler contains the specified flag.
 intgetNumberOfFlags()
          Returns the number of flags currently being handled.
 synchronized voidsetFlag(String flag)
          Sets a flag to true, and sets any mutually exclusive flags to false.
 synchronized voidsetMutualExclusions(String flag, String[] mxflags)
          Adds a set of mutually exclusive flag markers to the specified flag.
 synchronized booleantestFlag(String flag)
          Tests the value of the flag.
 synchronized voidtoggleFlag(String flag)
          Toggles the value of the flag, clearing any mutually exclusive flags if this flag is set.
 StringtoString()
          Returns a string based representation of the status of every flag in the handler.

Constructor Detail

FlagHandler

public FlagHandler()
Constructs an empty FlagHandler - there are intially no flags set or known about. Add new flags using addFlag(), and then set, clear and test them using the appropriate methods.
Method Detail

addFlag

public synchronized boolean addFlag(String flag)
Add a new flag to the handler. This will not set the flag, it will simply make the handler aware of the flag, and assign the flag a bit position. Once added, a flag cannot be removed.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.
Returns: True if the flag was added, false if the flag name is already in use or an error occurs

addFlag

public synchronized boolean addFlag(String flag, String mxflag)
Add a new flag to the handler. This will not set the flag, it will simply make the handler aware of the flag, and assign the flag a bit position. Once added, a flag cannot be removed.
This method also sets the flag to be mutually exclusive with the second flag specified. This means that if this flag is set, then the mutually exclusive flag will be cleared automatically, and vice versa. More mutual exclusions can be set using the setMutualExclusions() method. The semantics of the mutual exclusion mean that clearing a flag will not set any mutually exclusive flags, even though setting a flag clears the mutually exclusive flags.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.
mxflag - Another flag that this flag is mutually exclusive with
Returns: True if the flag was added, false if the flag name is already in use or an error occurs

addFlag

public synchronized boolean addFlag(String flag, String[] mxflags)
Add a new flag to the handler. This will not set the flag, it will simply make the handler aware of the flag, and assign the flag a bit position. Once added, a flag cannot be removed.
This method also sets the flag to be mutually exclusive with the array of flags specified. This means that if this flag is set, then the mutually exclusive flags will be cleared automatically, and vice versa. More mutual exclusions can be set using the setMutualExclusions() method. The semantics of the mutual exclusion mean that clearing a flag will not set any mutually exclusive flags, even though setting a flag clears the mutually exclusive flags.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.
mxflags - An array of mutually exclusive flags
Returns: True if the flag was added, false if the flag name is already in use or an error occurs

clearAllFlags

public synchronized void clearAllFlags()
Sets all of the flags to false (clears every flag);

clearFlag

public synchronized void clearFlag(String flag)
Clears a flag (set it to false).
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.

containsFlag

public boolean containsFlag(String flag)
Checks to see if this handler contains the specified flag.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.
Returns: True if the flag is in this handler, false otherwise

getNumberOfFlags

public int getNumberOfFlags()
Returns the number of flags currently being handled. Useful if you want to calculate the number of flags that can still be put into this handler.
Returns: The number of flags currently in use by this handler

setFlag

public synchronized void setFlag(String flag)
Sets a flag to true, and sets any mutually exclusive flags to false.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.

setMutualExclusions

public synchronized void setMutualExclusions(String flag, String[] mxflags)
Adds a set of mutually exclusive flag markers to the specified flag. These will automatically be cleared when this flag is set. If this flag is cleared then these flags are not automatically set.
Parameters:
flag - The flag to add to
mxflags - The mutually exclusive flag names

testFlag

public synchronized boolean testFlag(String flag)
Tests the value of the flag.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.
Returns: True is the flag is set, false if it not or is not present

toggleFlag

public synchronized void toggleFlag(String flag)
Toggles the value of the flag, clearing any mutually exclusive flags if this flag is set.
Note: If this flag is cleared then the mutually exclusive flags are not set.
Parameters:
flag - The flag name as a String. Flags are always referenced by string to ease programmer effort.

toString

public String toString()
Returns a string based representation of the status of every flag in the handler.
Note: The list of flags returned is not sorted.
Returns: A string representation of the status of the flags

Association Links

to Class java.util.BitSet

to Class java.util.List

to Class java.util.Hashtable