jut.ref
Class Counter

java.lang.Object
  extended by jut.ref.Counter
All Implemented Interfaces:
ParamCallable<Boolean,Long>, ICount

public class Counter
extends Object
implements ICount, ParamCallable<Boolean,Long>

wrapper for a long value that is to be accessed and modified out of more than one context. Counter is thread-safe.

Author:
Georg Dietrich

Field Summary
protected  ArrayList<ParamCallable<Boolean,Long>> difListeners
          listeners observing changes to this Counter.
protected  ArrayList<ParamCallable<Boolean,Long>> valListeners
          listeners observing the value of this Counter.
protected  long value
          the counter used internally.
 
Fields inherited from interface jut.ref.ICount
DUMMY
 
Constructor Summary
Counter()
           
 
Method Summary
 Counter add(long val)
          add a value to the counter.
 Counter addDifListener(ParamCallable<Boolean,Long> listener)
          add a (non-null) change listener to this Counter. since Counter implements ParamCallable, one counter can be linked to another counter by adding it to the change listeners of the other counter. note that listeners can be added multiple times. in this case, they will also be notified multiple times.
 Counter addValListener(ParamCallable<Boolean,Long> listener)
          add a (non-null) value listener to this Counter. note that listeners can be added multiple times. in this case, they will also be notified multiple times.
 Boolean call(Long increment)
          implementation of the ParamCallable interface, so a counter can be added to the list of listeners of another counter.
 long get()
          
 boolean removeDifListener(ParamCallable<Boolean,Long> listener)
          unregister a change listener. note that only one instance of the listener is unregistered, not all instances.
 boolean removeValListener(ParamCallable<Boolean,Long> listener)
          unregister a value listener. note that only one instance of the listener is unregistered, not all instances.
<T> T
sync(ParamCallable<T,Long> task)
          run a synchronized task, e.g. compare and set, and return the tasks result.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

value

protected long value
the counter used internally.


valListeners

protected final ArrayList<ParamCallable<Boolean,Long>> valListeners
listeners observing the value of this Counter.


difListeners

protected final ArrayList<ParamCallable<Boolean,Long>> difListeners
listeners observing changes to this Counter.

Constructor Detail

Counter

public Counter()
Method Detail

addValListener

public Counter addValListener(ParamCallable<Boolean,Long> listener)
add a (non-null) value listener to this Counter. note that listeners can be added multiple times. in this case, they will also be notified multiple times.

if a listener returns false, all its instances are unregistered after all listeners have been called.

Parameters:
listener - - the (non-null) value listener to add to this Counter.
Returns:
a reference to the object itself.

addDifListener

public Counter addDifListener(ParamCallable<Boolean,Long> listener)
add a (non-null) change listener to this Counter. since Counter implements ParamCallable, one counter can be linked to another counter by adding it to the change listeners of the other counter. note that listeners can be added multiple times. in this case, they will also be notified multiple times.

if a listener returns false, all its instances are unregistered after all listeners have been called.

Parameters:
listener - - the (non-null) change listener to add to this Counter.
Returns:
a reference to the object itself.

removeValListener

public boolean removeValListener(ParamCallable<Boolean,Long> listener)
unregister a value listener. note that only one instance of the listener is unregistered, not all instances.

Parameters:
listener - - the listener to remove.
Returns:
false if the listener was not registered.

removeDifListener

public boolean removeDifListener(ParamCallable<Boolean,Long> listener)
unregister a change listener. note that only one instance of the listener is unregistered, not all instances.

Parameters:
listener - - the listener to remove.
Returns:
false if the listener was not registered.

add

public Counter add(long val)
add a value to the counter. subsequently calls the Counter's listeners. even if zero is added, the listeners are called.

Specified by:
add in interface ICount
Parameters:
val - - the value to add to this counter.
Returns:
a reference to the object itself.

get

public long get()

Specified by:
get in interface ICount
Returns:
the current value of the counter.

sync

public <T> T sync(ParamCallable<T,Long> task)
run a synchronized task, e.g. compare and set, and return the tasks result.

Type Parameters:
T - - the type of the task's return value.
Parameters:
task - - the task to run. the ParamCallable.call(Object) method is provided with the counter value.
Returns:
the result of the task.

call

public Boolean call(Long increment)
implementation of the ParamCallable interface, so a counter can be added to the list of listeners of another counter.

Specified by:
call in interface ParamCallable<Boolean,Long>
Parameters:
increment - - the value to add to this Counter.
Returns:
true so this Counter is not removed from the listeners of another Counter.