01: package snow.concurrent;
02:
03: import java.util.Vector;
04:
05: public class Counter {
06: private int counterValue = 0;
07:
08: public Counter() {
09: }
10:
11: public void increment(int count) {
12: counterValue += count;
13: notifyValueChanged();
14: }
15:
16: public int getCount() {
17: return counterValue;
18: }
19:
20: final private Vector<CounterListener> listeners = new Vector<CounterListener>();
21:
22: public void addCounterListener(CounterListener cl) {
23: listeners.add(cl);
24: }
25:
26: public void removeCounterListener(CounterListener cl) {
27: listeners.remove(cl);
28: }
29:
30: private void notifyValueChanged() {
31: for (CounterListener cl : listeners
32: .toArray(new CounterListener[listeners.size()])) {
33: cl.counterUpdated(counterValue);
34: }
35: }
36:
37: private void terminateCounter() {
38: listeners.clear();
39: }
40:
41: }
|