1 7 8 package java.util; 9 10 45 public class Observable { 46 private boolean changed = false; 47 private Vector obs; 48 49 50 51 public Observable() { 52 obs = new Vector (); 53 } 54 55 64 public synchronized void addObserver(Observer o) { 65 if (o == null) 66 throw new NullPointerException (); 67 if (!obs.contains(o)) { 68 obs.addElement(o); 69 } 70 } 71 72 77 public synchronized void deleteObserver(Observer o) { 78 obs.removeElement(o); 79 } 80 81 97 public void notifyObservers() { 98 notifyObservers(null); 99 } 100 101 115 public void notifyObservers(Object arg) { 116 120 Object [] arrLocal; 121 122 synchronized (this) { 123 135 if (!changed) 136 return; 137 arrLocal = obs.toArray(); 138 clearChanged(); 139 } 140 141 for (int i = arrLocal.length-1; i>=0; i--) 142 ((Observer )arrLocal[i]).update(this, arg); 143 } 144 145 148 public synchronized void deleteObservers() { 149 obs.removeAllElements(); 150 } 151 152 156 protected synchronized void setChanged() { 157 changed = true; 158 } 159 160 170 protected synchronized void clearChanged() { 171 changed = false; 172 } 173 174 184 public synchronized boolean hasChanged() { 185 return changed; 186 } 187 188 193 public synchronized int countObservers() { 194 return obs.size(); 195 } 196 } 197 | Popular Tags |