1
   2
   3
   4
   5
   6import java.util.Vector;
   7
   8public
   9class Counter {
  10  int cnt;
  11
  12  public
  13  Counter() {
  14    this.cnt = 0;
  15    counterChangedListeners = new Vector();
  16  }
  17
  18  
  19  
  20  public
  21  void incr(int i) {
  22    cnt += i;
  23
  24    notifyCounterChanged();
  25  }
  26
  27  
  28  
  29
  30  private
  31  Vector counterChangedListeners;
  32
  33  protected
  34  void notifyCounterChanged() {
  35    Vector l;
  36    CounterChangedEvent e = new CounterChangedEvent(this,cnt);
  37
  38    synchronized (this) {
  39      l = (Vector)counterChangedListeners.clone();
  40    }
  41
  42    for ( int i = 0;
  43          i < l.size();
  44          ++i ) {
  45      ((CounterChangedListener)l.elementAt(i)).counterChanged(e);
  46    }
  47  }
  48
  49  
  50
  51  public
  52  synchronized
  53  void addCounterChangedListener(CounterChangedListener l) {
  54    counterChangedListeners.addElement(l);
  55  }
  56
  57  public
  58  synchronized
  59  void removeCounterChangedListener(CounterChangedListener l) {
  60    counterChangedListeners.removeElement(l);
  61  }
  62}