1 17 18 package EDU.oswego.cs.dl.util.concurrent; 19 20 import java.beans.PropertyChangeListener ; 21 import java.beans.PropertyChangeEvent ; 22 import java.util.HashMap ; 23 import java.io.Serializable ; 24 import java.io.ObjectOutputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.IOException ; 27 28 71 72 public class PropertyChangeMulticaster implements Serializable { 73 74 79 82 83 protected transient PropertyChangeListener [] listeners = new PropertyChangeListener [0]; 84 85 86 90 protected final Object source; 91 92 97 protected HashMap children; 98 99 102 103 protected synchronized PropertyChangeMulticaster getChild(String propertyName) { 104 return (children == null)? null : 105 ((PropertyChangeMulticaster)children.get(propertyName)); 106 } 107 108 109 115 116 public PropertyChangeMulticaster(Object sourceBean) { 117 if (sourceBean == null) { 118 throw new NullPointerException (); 119 } 120 121 source = sourceBean; 122 } 123 124 133 134 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 135 136 if (listener == null) throw new NullPointerException (); 137 138 int len = listeners.length; 139 PropertyChangeListener [] newArray = new PropertyChangeListener [len + 1]; 140 if (len > 0) 141 System.arraycopy(listeners, 0, newArray, 0, len); 142 newArray[len] = listener; 143 listeners = newArray; 144 } 145 146 147 157 158 public synchronized void addPropertyChangeListenerIfAbsent(PropertyChangeListener listener) { 159 160 if (listener == null) throw new NullPointerException (); 161 162 int len = listeners.length; 164 PropertyChangeListener [] newArray = new PropertyChangeListener [len + 1]; 165 for (int i = 0; i < len; ++i) { 166 newArray[i] = listeners[i]; 167 if (listener.equals(listeners[i])) 168 return; } 170 newArray[len] = listener; 171 listeners = newArray; 172 } 173 174 175 186 187 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 188 189 int newlen = listeners.length-1; 190 if (newlen < 0 || listener == null) return; 191 192 194 PropertyChangeListener [] newArray = new PropertyChangeListener [newlen]; 195 196 for (int i = 0; i < newlen; ++i) { 197 if (listener.equals(listeners[i])) { 198 for (int k = i + 1; k <= newlen; ++k) newArray[k-1] = listeners[k]; 200 listeners = newArray; 201 return; 202 } 203 else 204 newArray[i] = listeners[i]; 205 } 206 207 if (listener.equals(listeners[newlen])) 209 listeners = newArray; 210 } 211 212 223 224 public void addPropertyChangeListener(String propertyName, 225 PropertyChangeListener listener) { 226 227 if (listener == null) throw new NullPointerException (); 228 229 PropertyChangeMulticaster child = null; 230 231 synchronized(this) { 232 if (children == null) 233 children = new HashMap (); 234 else 235 child = (PropertyChangeMulticaster)children.get(propertyName); 236 237 if (child == null) { 238 child = new PropertyChangeMulticaster(source); 239 children.put(propertyName, child); 240 } 241 } 242 243 child.addPropertyChangeListener(listener); 244 } 245 246 256 257 public void addPropertyChangeListenerIfAbsent(String propertyName, 258 PropertyChangeListener listener) { 259 260 if (listener == null) throw new NullPointerException (); 261 262 PropertyChangeMulticaster child = null; 263 264 synchronized(this) { 265 if (children == null) 266 children = new HashMap (); 267 else 268 child = (PropertyChangeMulticaster)children.get(propertyName); 269 270 if (child == null) { 271 child = new PropertyChangeMulticaster(source); 272 children.put(propertyName, child); 273 } 274 } 275 276 child.addPropertyChangeListenerIfAbsent(listener); 277 } 278 279 288 289 public void removePropertyChangeListener(String propertyName, 290 PropertyChangeListener listener) { 291 292 PropertyChangeMulticaster child = getChild(propertyName); 293 if (child != null) 294 child.removePropertyChangeListener(listener); 295 } 296 297 298 302 303 protected void multicast(PropertyChangeEvent evt) { 304 305 PropertyChangeListener [] array; PropertyChangeMulticaster child = null; 307 308 synchronized (this) { 309 array = listeners; 310 311 if (children != null && evt.getPropertyName() != null) 312 child = (PropertyChangeMulticaster)children.get(evt.getPropertyName()); 313 } 314 315 for (int i = 0; i < array.length; ++i) 316 array[i].propertyChange(evt); 317 318 if (child != null) 319 child.multicast(evt); 320 321 } 322 323 324 333 public void firePropertyChange(String propertyName, 334 Object oldValue, Object newValue) { 335 336 if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { 337 multicast(new PropertyChangeEvent (source, 338 propertyName, 339 oldValue, 340 newValue)); 341 } 342 343 } 344 345 357 public void firePropertyChange(String propertyName, 358 int oldValue, int newValue) { 359 if (oldValue != newValue) { 360 multicast(new PropertyChangeEvent (source, 361 propertyName, 362 new Integer (oldValue), 363 new Integer (newValue))); 364 } 365 } 366 367 368 380 public void firePropertyChange(String propertyName, 381 boolean oldValue, boolean newValue) { 382 if (oldValue != newValue) { 383 multicast(new PropertyChangeEvent (source, 384 propertyName, 385 new Boolean (oldValue), 386 new Boolean (newValue))); 387 } 388 } 389 390 396 public void firePropertyChange(PropertyChangeEvent evt) { 397 Object oldValue = evt.getOldValue(); 398 Object newValue = evt.getNewValue(); 399 if (oldValue == null || newValue == null || !oldValue.equals(newValue)) 400 multicast(evt); 401 } 402 403 411 public boolean hasListeners(String propertyName) { 412 413 PropertyChangeMulticaster child; 414 415 synchronized (this) { 416 if (listeners.length > 0) 417 return true; 418 else if (propertyName == null || children == null) 419 return false; 420 else { 421 child = (PropertyChangeMulticaster)children.get(propertyName); 422 if (child == null) 423 return false; 424 } 425 } 426 427 return child.hasListeners(null); 428 } 429 430 431 438 private synchronized void writeObject(ObjectOutputStream s) throws IOException { 439 s.defaultWriteObject(); 440 441 for (int i = 0; i < listeners.length; i++) { 442 PropertyChangeListener l = listeners[i]; 443 if (listeners[i] instanceof Serializable ) { 444 s.writeObject(listeners[i]); 445 } 446 } 447 s.writeObject(null); 448 } 449 450 451 private void readObject(ObjectInputStream s) throws ClassNotFoundException , IOException { 452 listeners = new PropertyChangeListener [0]; s.defaultReadObject(); 454 455 Object listenerOrNull; 456 while (null != (listenerOrNull = s.readObject())) { 457 addPropertyChangeListener((PropertyChangeListener )listenerOrNull); 458 } 459 } 460 461 } 462 | Popular Tags |