cz.cuni.utils
Class Flag<T>

java.lang.Object
  extended by cz.cuni.utils.Flag<T>
Type Parameters:
T - type of the flag
Direct Known Subclasses:
ImmutableFlag

public class Flag<T>
extends java.lang.Object

This is flag class which is designed for Boolean or Integer types (but it should work with other types as well as long as they have equals() implemented correctly. It allows you to store the state of flag and register listeners on the flag. I've design it because of Threads which communicates over sockets. I'm using two boolean flags -> threadIsRunning (which is set at the beginning of run() and at the end of it) and communicationAlive ... usually communication can be dropped or thread exits (due to exception for instance) and you need to take some actions because of it. Check out FlagListener interface documentation too!


Nested Class Summary
(package private)  class Flag.ListenerStore<T>
           
 
Field Summary
protected  ImmutableFlag<T> immutableWrapper
          Immutable version of this flag.
private  java.util.ArrayList<Flag.ListenerStore<T>> listeners
           
private  T value
           
 
Constructor Summary
Flag()
          Initialize the flag with 'initialValue'.
Flag(T initialValue)
          Initialize the flag with 'initialValue'.
 
Method Summary
 boolean addListener(FlagListener<T> listener)
          Adds new listener to the flag.
 boolean addListener(FlagListener<T> listener, int callListenerWithParam)
          Adds new listener to the flag with specified param.
 void clearListeners()
          Call to clear (remove) all the listeners on the flag.
 T getFlag()
          Returns the value of the flag.
 ImmutableFlag<T> getImmutable()
           
 boolean isListenning(FlagListener<T> listener)
          Checks whether listener is already registered (with any parameter).
 boolean isListenning(FlagListener<T> listener, int listenerParam)
          Checks whether listener is already registered (with specified parameter).
static void main(java.lang.String[] args)
          Not so good test of the flag - you need to confirm the results for yourself.
 boolean removeListener(FlagListener<T> listener)
          Removes all registered 'listener' from the flag (doesn't depend on the parameter, all listeners are removed).
 boolean removeListener(FlagListener<T> listener, int listenerParam)
          Removes all registered 'listener' from the flag (with specified listenerParam).
 void setFlag(T newValue)
          Changes the flag and informs all listeners.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

value

private T value

listeners

private java.util.ArrayList<Flag.ListenerStore<T>> listeners

immutableWrapper

protected ImmutableFlag<T> immutableWrapper
Immutable version of this flag.

Constructor Detail

Flag

public Flag()
Initialize the flag with 'initialValue'.


Flag

public Flag(T initialValue)
Initialize the flag with 'initialValue'.

Parameters:
initialValue -
Method Detail

setFlag

public void setFlag(T newValue)
Changes the flag and informs all listeners.

Parameters:
newValue -

getImmutable

public ImmutableFlag<T> getImmutable()
Returns:
Immutable version of this flag, setFlag(T) method of such a flag will raise an exception.

getFlag

public T getFlag()
Returns the value of the flag.

Returns:
value of the flag

addListener

public boolean addListener(FlagListener<T> listener,
                           int callListenerWithParam)
Adds new listener to the flag with specified param. When listener.flagChanged is called, it will pass the specified param to it, allowing you to differentiate between many flags in one method (see javadoc for FlagListener interface for more explanation)

Parameters:
listener -

addListener

public boolean addListener(FlagListener<T> listener)
Adds new listener to the flag. When listener.flagChanged is called, it will pass 0 as listenerParam (see javadoc for FlagListener interface for more explanation).

Parameters:
listener -

removeListener

public boolean removeListener(FlagListener<T> listener)
Removes all registered 'listener' from the flag (doesn't depend on the parameter, all listeners are removed).

Parameters:
listener -
Returns:
success (if at least one listener was removed)

removeListener

public boolean removeListener(FlagListener<T> listener,
                              int listenerParam)
Removes all registered 'listener' from the flag (with specified listenerParam). Note that ALL 'listener' with 'listenerParam' are removed.

Parameters:
listener -
Returns:
success (if at least one listener was removed)

isListenning

public boolean isListenning(FlagListener<T> listener)
Checks whether listener is already registered (with any parameter).

Parameters:
listener -
Returns:
true if listener is already registered

isListenning

public boolean isListenning(FlagListener<T> listener,
                            int listenerParam)
Checks whether listener is already registered (with specified parameter).

Parameters:
listener -
Returns:
true if listener is registered

clearListeners

public void clearListeners()
Call to clear (remove) all the listeners on the flag. Should be used when the flag isn't going to be used again to allow GC to collect the listeners (for instance anonymous objects).


main

public static void main(java.lang.String[] args)
Not so good test of the flag - you need to confirm the results for yourself.