Filter:   InfoImg
download VetoableChangeMulticaster.java
Language: Java
LOC: 197
Project Info
Lyophilizer
Server: SourceForge
Type: cvs
...wego\cs\dl\util\concurrent\
   Barrier.java
   BoundedBuffer.java
   BoundedChannel.java
   BoundedLinkedQueue.java
   BoundedPriorityQueue.java
   ...enBarrierException.java
   Callable.java
   Channel.java
   ClockDaemon.java
   ConcurrentHashMap.java
   ...rrentReaderHashMap.java
   CondVar.java
   CopyOnWriteArrayList.java
   CopyOnWriteArraySet.java
   CountDown.java
   CyclicBarrier.java
   ...ultChannelCapacity.java
   DirectExecutor.java
   Executor.java
   FIFOReadWriteLock.java
   FIFOSemaphore.java
   FJTask.java
   FJTaskRunner.java
   FJTaskRunnerGroup.java
   FutureResult.java
   Heap.java
   Latch.java
   LayeredSync.java
   LinkedNode.java
   LinkedQueue.java
   LockedExecutor.java
   Mutex.java
   NullSync.java
   ObservableSync.java
   PooledExecutor.java
   PrioritySemaphore.java
   ...yChangeMulticaster.java
   Puttable.java
   QueuedExecutor.java
   QueuedSemaphore.java
   ...renceReadWriteLock.java
   ReadWriteLock.java
   ReentrantLock.java
   ...renceReadWriteLock.java
   Rendezvous.java
   Semaphore.java
   ...eControlledChannel.java
   Slot.java
   Sync.java
   SyncCollection.java
   SynchronizedBoolean.java
   SynchronizedByte.java
   SynchronizedChar.java
   SynchronizedDouble.java
   SynchronizedFloat.java
   SynchronizedInt.java
   SynchronizedLong.java
   SynchronizedRef.java
   SynchronizedShort.java
   SynchronizedVariable.java
   SynchronousChannel.java
   SyncList.java
   SyncMap.java
   SyncSet.java
   SyncSortedMap.java
   SyncSortedSet.java
   Takable.java
   ThreadedExecutor.java
   ThreadFactory.java
   ThreadFactoryUser.java
   TimeDaemon.java
   TimedCallable.java
   TimeoutException.java
   TimeoutSync.java
   ...eChangeMulticaster.java
   WaitableBoolean.java
   WaitableByte.java
   WaitableChar.java
   WaitableDouble.java
   WaitableFloat.java
   WaitableInt.java
   WaitableLong.java
   WaitableRef.java
   WaitableShort.java
   ...referenceSemaphore.java
   WaitFreeQueue.java
   ...renceReadWriteLock.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
  File: ProperyChangeMulticaster.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  This class is based on Sun JDK java.beans.VetoableChangeSupport,
  which is copyrighted by Sun. (It shares practically no code, but for 
  consistency, the documentation was lifted and adapted here.)

  History:
  Date       Who                What
  14Mar1999   dl                 first release
*/

package EDU.oswego.cs.dl.util.concurrent;

import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;

/**
 * This class is interoperable with java.beans.VetoableChangeSupport,
 * but relies on a streamlined copy-on-write scheme similar to
 * that used in CopyOnWriteArrayList. It also adheres to clarified
 * semantics of add, remove, and fireVetoableChange operations.
 * <p>
 * <b>Sample usage.</b>
 * 
 * <pre>
 * class Thing {
 *   protected Color myColor = Color.red; // an example property
 *   protected boolean changePending; // track whether in midst of change
 *
 *   // vetoable listeners:
 *   protected VetoableChangeMulticaster vetoers =
 *     new VetoableChangeMulticaster(this);
 *
 *   // Possibly also some ordinary listeners:
 *   protected PropertyChangeMulticaster listeners =
 *     new PropertyChangeMulticaster(this);
 *
 *   // registration methods, including:
 *   void addVetoer(VetoableChangeListener l) {
 *     // Use the `ifAbsent' version to avoid duplicate notifications
 *     vetoers.addVetoableChangeListenerIfAbsent(l);
 *   }
 *   
 *   public synchronized Color getColor() { // accessor
 *     return myColor;
 *   }
 *
 *   // Simple transactional control for vetos
 *
 *   public void setColor(int newColor) throws PropertyVetoException {
 *     Color oldColor = prepareSetColor(newColor);
 *    
 *     try {
 *       vetoers.fireVetoableChange("color", oldColor, newColor);
 *       commitColor(newColor);
 *       listeners.firePropertyChange("color", oldColor, newColor);
 *     }
 *     catch(PropertyVetoException ex) {
 *       abortSetColor();
 *       throw ex;
 *     }
 *   }
 *
 *   // Called on entry to proposed vetoable change from setColor.
 *   // Throws exception if there is already another change in progress.
 *   // Returns current color
 *   synchronized int prepareSetColor(Color c) throws PropertyVetoException {
 *     // only support one transaction at a time
 *     if (changePending) 
 *       throw new PropertyVetoException("Concurrent modification");
 *       // (Could alternatively wait out other transactions via
 *       // a wait/notify construction based on changePending.)
 *
 *     // perhaps some other screenings, like:
 *     else if (c == null) 
 *       throw new PropertyVetoException("Cannot change color to Null");
 *     else {
 *       changePending = true;
 *       return myColor;
 *     }
 *   }
 *
 *   synchronized void commitColor(Color newColor) { 
 *     myColor = newColor;
 *     changePending = false;
 *   }
 *
 *   synchronized void abortSetColor() {
 *     changePending = false;
 *   }
 *
 * }
 * </pre>   
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
 **/

public class VetoableChangeMulticaster implements Serializable {

  // This code is 90% identical with PropertyChangeMulticaster,
  // but there is no good way to unify the code while maintaining
  // interoperability with beans versions.

  /**
   * The array of listeners. Copied on each update
   **/

  protected transient VetoableChangeListener[] listeners = new VetoableChangeListener[0];


  /** 
   * The object to be provided as the "source" for any generated events.
   * @serial
   */
  protected final Object source;

  /** 
   * HashMap for managing listeners for specific properties.
   * Maps property names to VetoableChangeMulticaster objects.
   * @serial
   */
  protected HashMap children;

  /**
   * Return the child associated with property, or null if no such
   **/

  protected synchronized VetoableChangeMulticaster getChild(String propertyName) {
    return (children == null)? null : 
      ((VetoableChangeMulticaster)children.get(propertyName));
  }


  /**
   * Constructs a <code>VetoableChangeMulticaster</code> object.
   *
   * @param sourceBean  The bean to be given as the source for any events.
   * @exception NullPointerException if sourceBean is null
   */
  
  public VetoableChangeMulticaster(Object sourceBean) {
    if (sourceBean == null) {
      throw new NullPointerException();
    }

    source = sourceBean;
  }

  /**
   * Add a VetoableChangeListener to the listener list.
   * The listener is registered for all properties.
   * If the listener is added multiple times, it will
   * receive multiple change notifications upon any fireVetoableChange.
   *
   * @param listener  The VetoableChangeListener to be added
   */
  
  public synchronized void addVetoableChangeListener(VetoableChangeListener listener) {

    if (listener == null) throw new NullPointerException();

    int len = listeners.length;
    VetoableChangeListener[] newArray = new VetoableChangeListener[len + 1];
    if (len > 0)
      System.arraycopy(listeners, 0, newArray, 0, len);
    newArray[len] = listener;
    listeners = newArray;
  }

  /**
   * Add a PropertyChangeListener to the listener list if it is 
   * not already present.
   * The listener is registered for all properties.
   * The operation maintains Set semantics: If the listener is already 
   * registered, the operation has no effect.
   *
   * @param listener  The PropertyChangeListener to be added
   * @exception NullPointerException If listener is null
   */
  
  public synchronized void addVetoableChangeListenerIfAbsent(VetoableChangeListener listener) {

    if (listener == null) throw new NullPointerException();

    // Copy while checking if already present.
    int len = listeners.length; 
    VetoableChangeListener[] newArray = new VetoableChangeListener[len + 1];
    for (int i = 0; i < len; ++i) {
      newArray[i] = listeners[i];
      if (listener.equals(listeners[i]))
	return; // already present -- throw away copy
    }
    newArray[len] = listener;
    listeners = newArray;
  }


  /**
   * Remove an occurrence of a VetoableChangeListener from the listener list.
   * It removes at most one occurrence of the given listener.
   * If the listener was added multiple times it must be removed
   * mulitple times.
   * This removes a VetoableChangeListener that was registered
   * for all properties, and has no effect if registered for only
   * one or more specified properties.
   *
   * @param listener  The VetoableChangeListener to be removed
   */
  
  public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {

    int newlen = listeners.length-1;
    if (newlen < 0 || listener == null) return;

    // Copy while searching for element to remove

    VetoableChangeListener[] newArray = new VetoableChangeListener[newlen];

    for (int i = 0; i < newlen; ++i) { 
      if (listener.equals(listeners[i])) {
        //  copy remaining and exit
        for (int k = i + 1; k <= newlen; ++k) newArray[k-1] = listeners[k];
        listeners = newArray;
        return;
      }
      else
        newArray[i] = listeners[i];
    }
    
    // special-case last cell
    if (listener.equals(listeners[newlen]))
      listeners = newArray;

  }

  /**
   * Add a VetoableChangeListener for a specific property.  The listener
   * will be invoked only when a call on fireVetoableChange names that
   * specific property. However, if a listener is registered both for all
   * properties and a specific property, it will receive multiple 
   * notifications upon changes to that property.
   *
   * @param propertyName  The name of the property to listen on.
   * @param listener  The VetoableChangeListener to be added
   * @exception NullPointerException If listener is null
   */
  
  public void addVetoableChangeListener(String propertyName,
                                        VetoableChangeListener listener) {

    if (listener == null) throw new NullPointerException();

    VetoableChangeMulticaster child = null;

    synchronized(this) {
      if (children == null) 
        children = new HashMap();
      else 
        child = (VetoableChangeMulticaster)children.get(propertyName);
      
      if (child == null) {
        child = new VetoableChangeMulticaster(source);
        children.put(propertyName, child);
      }
    }

    child.addVetoableChangeListener(listener);
  }

  /**
   * Add a VetoableChangeListener for a specific property, if it is not
   * already registered.  The listener
   * will be invoked only when a call on fireVetoableChange names that
   * specific property. 
   *
   * @param propertyName  The name of the property to listen on.
   * @param listener  The VetoableChangeListener to be added
   * @exception NullPointerException If listener is null
   */
  
  public void addVetoableChangeListenerIfAbsent(String propertyName,
                                        VetoableChangeListener listener) {

    if (listener == null) throw new NullPointerException();

    VetoableChangeMulticaster child = null;

    synchronized(this) {
      if (children == null) 
        children = new HashMap();
      else 
        child = (VetoableChangeMulticaster)children.get(propertyName);
      
      if (child == null) {
        child = new VetoableChangeMulticaster(source);
        children.put(propertyName, child);
      }
    }

    child.addVetoableChangeListenerIfAbsent(listener);
  }


  /**
   * Remove a VetoableChangeListener for a specific property.
   * Affects only the given property. 
   * If the listener is also registered for all properties,
   * then it will continue to be registered for them.
   *
   * @param propertyName  The name of the property that was listened on.
   * @param listener  The VetoableChangeListener to be removed
   */
  
  public void removeVetoableChangeListener(String propertyName,
                                           VetoableChangeListener listener) {

    VetoableChangeMulticaster child = getChild(propertyName);
    if (child != null) 
      child.removeVetoableChangeListener(listener);
  }


  /**
   * Helper method to relay evt to all listeners. 
   * Called by all public fireVetoableChange methods.
   **/

  protected void multicast(PropertyChangeEvent evt) throws PropertyVetoException {

    VetoableChangeListener[] array;  // bind in synch block below
    VetoableChangeMulticaster child = null;

    synchronized (this) {
      array = listeners;

      if (children != null && evt.getPropertyName() != null)
        child = (VetoableChangeMulticaster)children.get(evt.getPropertyName());
    }

    // Loop through array, and then cascade to child.
    
    int i = 0; // make visible to catch clause

    try {
      for (i = 0; i < array.length; ++i)
        array[i].vetoableChange(evt);

      if  (child != null)
        child.multicast(evt);
    }

    catch (PropertyVetoException veto) {
      
      // Revert all that have been notified
      
      PropertyChangeEvent revert = 
        new PropertyChangeEvent(evt.getSource(), 
                                evt.getPropertyName(), 
                                evt.getNewValue(), 
                                evt.getOldValue());

      int lastNotified = (i < array.length)? i : (array.length-1);

      for (int k = 0; k <= lastNotified; ++k) {
        try {
          array[k].vetoableChange(revert);
        }
        catch (PropertyVetoException ignore) {
          // Cannot veto a reversion
        }
      }
      
      //  Rethrow the PropertyVetoException.
      throw veto;
    }
  }

  
  /**
   * Report a vetoable property update to any registered listeners. 
   * Notifications are sent serially (although in no particular order)
   * to the list of listeners,
   * aborting if one throws PropertyVetoException. Upon this exception,
   * fire a new event reverting this
   * change to all listeners that have already been notified
   * (ignoring any further vetos), 
   * suppress notifications to all other listeners, and
   * then rethrow the PropertyVetoException.
   * <p>
   * No event is fired if old and new are equal non-null.
   *
   * @param propertyName  The programmatic name of the property
   *		that was changed.
   * @param oldValue  The old value of the property.
   * @param newValue  The new value of the property.
   * @exception PropertyVetoException if a recipient wishes the property
   *              change to be rolled back.
   */
  public void fireVetoableChange(String propertyName, 
                                 Object oldValue, Object newValue) throws PropertyVetoException {
   
    if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
      multicast(new PropertyChangeEvent(source,
                                        propertyName, 
                                        oldValue, 
                                        newValue));
    }
    
  }

  /**
   * Report a vetoable property update to any registered listeners. 
   * Notifications are sent serially (although in no particular order)
   * to the list of listeners,
   * aborting if one throws PropertyVetoException. Upon this exception,
   * fire a new event reverting this
   * change to all listeners that have already been notified
   * (ignoring any further vetos), 
   * suppress notifications to all other listeners, and
   * then rethrow the PropertyVetoException.
   * <p>
   * No event is fired if old and new are equal.
   * <p>
   * This is merely a convenience wrapper around the more general
   * fireVetoableChange method that takes Object values.
   *
   * @param propertyName  The programmatic name of the property
   *		that was changed.
   * @param oldValue  The old value of the property.
   * @param newValue  The new value of the property.
   * @exception PropertyVetoException if the recipient wishes the property
   *              change to be rolled back.
   */
  public void fireVetoableChange(String propertyName, 
                                 int oldValue, int newValue) throws PropertyVetoException {
    if (oldValue != newValue) {
      multicast(new PropertyChangeEvent(source,
                                        propertyName, 
                                        new Integer(oldValue), 
                                        new Integer(newValue)));
    }
  }


  /**
   * Report a vetoable property update to any registered listeners. 
   * Notifications are sent serially (although in no particular order)
   * to the list of listeners,
   * aborting if one throws PropertyVetoException. Upon this exception,
   * fire a new event reverting this
   * change to all listeners that have already been notified
   * (ignoring any further vetos), 
   * suppress notifications to all other listeners, and
   * then rethrow the PropertyVetoException.
   * <p>
   * No event is fired if old and new are equal.
   * <p>
   * This is merely a convenience wrapper around the more general
   * fireVetoableChange method that takes Object values.
   *
   * @param propertyName  The programmatic name of the property
   *		that was changed.
   * @param oldValue  The old value of the property.
   * @param newValue  The new value of the property.
   * @exception PropertyVetoException if the recipient wishes the property
   *              change to be rolled back.
   */
  public void fireVetoableChange(String propertyName, 
                                 boolean oldValue, boolean newValue) throws PropertyVetoException {
    if (oldValue != newValue) {
      multicast(new PropertyChangeEvent(source,
                                        propertyName, 
                                        new Boolean(oldValue), 
                                        new Boolean(newValue)));
    }
  }

  /**
   * Report a vetoable property update to any registered listeners. 
   * Notifications are sent serially (although in no particular order)
   * to the list of listeners,
   * aborting if one throws PropertyVetoException. Upon this exception,
   * fire a new event reverting this
   * change to all listeners that have already been notified
   * (ignoring any further vetos), 
   * suppress notifications to all other listeners, and
   * then rethrow the PropertyVetoException.
   * <p>
   * No event is fired if old and new are equal and non-null.
   *
   * equal and non-null.
   * @param evt  The PropertyChangeEvent object.
   * @exception PropertyVetoException if the recipient wishes the property
   *              change to be rolled back.
   */
  public void fireVetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
    Object oldValue = evt.getOldValue();
    Object newValue = evt.getNewValue();
    if (oldValue == null || newValue == null || !oldValue.equals(newValue)) 
      multicast(evt);
  }

  /**
   * Check if there are any listeners for a specific property.
   * If propertyName is null, return whether there are any listeners at all.
   *
   * @param propertyName  the property name.
   * @return true if there are one or more listeners for the given property
   * 
   */
  public boolean hasListeners(String propertyName) {

    VetoableChangeMulticaster child;

    synchronized (this) {
      if (listeners.length > 0)
        return true;
      else if (propertyName == null || children == null)
        return false;
      else {
        child = (VetoableChangeMulticaster)children.get(propertyName);
        if (child == null)
          return false;
      }
    }
    
    return child.hasListeners(null);
  }


  /**
   * @serialData Null terminated list of <code>VetoableChangeListeners</code>.
   * <p>
   * At serialization time we skip non-serializable listeners and
   * only serialize the serializable listeners.
   *
   */
  private synchronized void writeObject(ObjectOutputStream s) throws IOException {
    s.defaultWriteObject();
    
    for (int i = 0; i < listeners.length; i++) {
      VetoableChangeListener l = listeners[i];
      if (listeners[i] instanceof Serializable) {
        s.writeObject(listeners[i]);
      }
    }
    s.writeObject(null);
  }
  
  
  private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
    listeners = new VetoableChangeListener[0];     // paranoically reset
    s.defaultReadObject();
    
    Object listenerOrNull;
    while (null != (listenerOrNull = s.readObject())) {
      addVetoableChangeListener((VetoableChangeListener)listenerOrNull);
    }
  }

}