home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / util / Observable.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  1.8 KB  |  85 lines

  1. package java.util;
  2.  
  3. public class Observable {
  4.    private boolean changed = false;
  5.    private Object obs;
  6.  
  7.    public synchronized void addObserver(Observer o) {
  8.       if (this.obs != null) {
  9.          if (this.obs instanceof ObserverList) {
  10.             if (!((ObserverList)this.obs).contains(o)) {
  11.                ((ObserverList)this.obs).addElement(o);
  12.                return;
  13.             }
  14.  
  15.             return;
  16.          }
  17.  
  18.          if (this.obs != o) {
  19.             ObserverList tmp = new ObserverList();
  20.             ((Vector)tmp).addElement(this.obs);
  21.             ((Vector)tmp).addElement(o);
  22.             this.obs = tmp;
  23.             return;
  24.          }
  25.       } else {
  26.          this.obs = o;
  27.       }
  28.  
  29.    }
  30.  
  31.    public synchronized void deleteObserver(Observer o) {
  32.       if (this.obs == o) {
  33.          this.obs = null;
  34.       } else {
  35.          if (this.obs != null && this.obs instanceof ObserverList) {
  36.             ((ObserverList)this.obs).removeElement(o);
  37.          }
  38.  
  39.       }
  40.    }
  41.  
  42.    public void notifyObservers() {
  43.       this.notifyObservers((Object)null);
  44.    }
  45.  
  46.    public synchronized void notifyObservers(Object arg) {
  47.       if (this.hasChanged()) {
  48.          if (this.obs != null) {
  49.             if (this.obs instanceof ObserverList) {
  50.                ((ObserverList)this.obs).notifyObservers(this, arg);
  51.             } else {
  52.                ((Observer)this.obs).update(this, arg);
  53.             }
  54.          }
  55.  
  56.          this.clearChanged();
  57.       }
  58.  
  59.    }
  60.  
  61.    public synchronized void deleteObservers() {
  62.       this.obs = null;
  63.    }
  64.  
  65.    protected synchronized void setChanged() {
  66.       this.changed = true;
  67.    }
  68.  
  69.    protected synchronized void clearChanged() {
  70.       this.changed = false;
  71.    }
  72.  
  73.    public synchronized boolean hasChanged() {
  74.       return this.changed;
  75.    }
  76.  
  77.    public synchronized int countObservers() {
  78.       if (this.obs != null) {
  79.          return this.obs instanceof ObserverList ? ((ObserverList)this.obs).size() : 1;
  80.       } else {
  81.          return 0;
  82.       }
  83.    }
  84. }
  85.