Adding ObjectInputStream and ObjectOutputStream (but without implementation). Adding PropertyChange related classes.
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 import java.io.Serializable;
28 import java.io.ObjectStreamField;
29 import java.io.ObjectOutputStream;
30 import java.io.ObjectInputStream;
31 import java.io.IOException;
32 import java.util.Hashtable;
33 import java.util.Map.Entry;
36 * This is a utility class that can be used by beans that support constrained
37 * properties. It manages a list of listeners and dispatches
38 * {@link PropertyChangeEvent}s to them. You can use an instance of this class
39 * as a member field of your bean and delegate these types of work to it.
40 * The {@link VetoableChangeListener} can be registered for all properties
41 * or for a property specified by name.
43 * Here is an example of {@code VetoableChangeSupport} usage that follows
44 * the rules and recommendations laid out in the JavaBeans™ specification:
46 * public class MyBean {
47 * private final VetoableChangeSupport vcs = new VetoableChangeSupport(this);
49 * public void addVetoableChangeListener(VetoableChangeListener listener) {
50 * this.vcs.addVetoableChangeListener(listener);
53 * public void removeVetoableChangeListener(VetoableChangeListener listener) {
54 * this.vcs.removeVetoableChangeListener(listener);
57 * private String value;
59 * public String getValue() {
63 * public void setValue(String newValue) throws PropertyVetoException {
64 * String oldValue = this.value;
65 * this.vcs.fireVetoableChange("value", oldValue, newValue);
66 * this.value = newValue;
73 * A {@code VetoableChangeSupport} instance is thread-safe.
75 * This class is serializable. When it is serialized it will save
76 * (and restore) any listeners that are themselves serializable. Any
77 * non-serializable listeners will be skipped during serialization.
79 * @see PropertyChangeSupport
81 public class VetoableChangeSupport implements Serializable {
82 private VetoableChangeListenerMap map = new VetoableChangeListenerMap();
85 * Constructs a <code>VetoableChangeSupport</code> object.
87 * @param sourceBean The bean to be given as the source for any events.
89 public VetoableChangeSupport(Object sourceBean) {
90 if (sourceBean == null) {
91 throw new NullPointerException();
97 * Add a VetoableChangeListener to the listener list.
98 * The listener is registered for all properties.
99 * The same listener object may be added more than once, and will be called
100 * as many times as it is added.
101 * If <code>listener</code> is null, no exception is thrown and no action
104 * @param listener The VetoableChangeListener to be added
106 public void addVetoableChangeListener(VetoableChangeListener listener) {
107 if (listener == null) {
110 if (listener instanceof VetoableChangeListenerProxy) {
111 VetoableChangeListenerProxy proxy =
112 (VetoableChangeListenerProxy)listener;
113 // Call two argument add method.
114 addVetoableChangeListener(proxy.getPropertyName(),
115 proxy.getListener());
117 this.map.add(null, listener);
122 * Remove a VetoableChangeListener from the listener list.
123 * This removes a VetoableChangeListener that was registered
124 * for all properties.
125 * If <code>listener</code> was added more than once to the same event
126 * source, it will be notified one less time after being removed.
127 * If <code>listener</code> is null, or was never added, no exception is
128 * thrown and no action is taken.
130 * @param listener The VetoableChangeListener to be removed
132 public void removeVetoableChangeListener(VetoableChangeListener listener) {
133 if (listener == null) {
136 if (listener instanceof VetoableChangeListenerProxy) {
137 VetoableChangeListenerProxy proxy =
138 (VetoableChangeListenerProxy)listener;
139 // Call two argument remove method.
140 removeVetoableChangeListener(proxy.getPropertyName(),
141 proxy.getListener());
143 this.map.remove(null, listener);
148 * Returns an array of all the listeners that were added to the
149 * VetoableChangeSupport object with addVetoableChangeListener().
151 * If some listeners have been added with a named property, then
152 * the returned array will be a mixture of VetoableChangeListeners
153 * and <code>VetoableChangeListenerProxy</code>s. If the calling
154 * method is interested in distinguishing the listeners then it must
155 * test each element to see if it's a
156 * <code>VetoableChangeListenerProxy</code>, perform the cast, and examine
160 * VetoableChangeListener[] listeners = bean.getVetoableChangeListeners();
161 * for (int i = 0; i < listeners.length; i++) {
162 * if (listeners[i] instanceof VetoableChangeListenerProxy) {
163 * VetoableChangeListenerProxy proxy =
164 * (VetoableChangeListenerProxy)listeners[i];
165 * if (proxy.getPropertyName().equals("foo")) {
166 * // proxy is a VetoableChangeListener which was associated
167 * // with the property named "foo"
173 * @see VetoableChangeListenerProxy
174 * @return all of the <code>VetoableChangeListeners</code> added or an
175 * empty array if no listeners have been added
178 public VetoableChangeListener[] getVetoableChangeListeners(){
179 return this.map.getListeners();
183 * Add a VetoableChangeListener for a specific property. The listener
184 * will be invoked only when a call on fireVetoableChange names that
186 * The same listener object may be added more than once. For each
187 * property, the listener will be invoked the number of times it was added
189 * If <code>propertyName</code> or <code>listener</code> is null, no
190 * exception is thrown and no action is taken.
192 * @param propertyName The name of the property to listen on.
193 * @param listener The VetoableChangeListener to be added
195 public void addVetoableChangeListener(
197 VetoableChangeListener listener) {
198 if (listener == null || propertyName == null) {
201 listener = this.map.extract(listener);
202 if (listener != null) {
203 this.map.add(propertyName, listener);
208 * Remove a VetoableChangeListener for a specific property.
209 * If <code>listener</code> was added more than once to the same event
210 * source for the specified property, it will be notified one less time
211 * after being removed.
212 * If <code>propertyName</code> is null, no exception is thrown and no
214 * If <code>listener</code> is null, or was never added for the specified
215 * property, no exception is thrown and no action is taken.
217 * @param propertyName The name of the property that was listened on.
218 * @param listener The VetoableChangeListener to be removed
220 public void removeVetoableChangeListener(
222 VetoableChangeListener listener) {
223 if (listener == null || propertyName == null) {
226 listener = this.map.extract(listener);
227 if (listener != null) {
228 this.map.remove(propertyName, listener);
233 * Returns an array of all the listeners which have been associated
234 * with the named property.
236 * @param propertyName The name of the property being listened to
237 * @return all the <code>VetoableChangeListeners</code> associated with
238 * the named property. If no such listeners have been added,
239 * or if <code>propertyName</code> is null, an empty array is
243 public VetoableChangeListener[] getVetoableChangeListeners(String propertyName) {
244 return this.map.getListeners(propertyName);
248 * Reports a constrained property update to listeners
249 * that have been registered to track updates of
250 * all properties or a property with the specified name.
252 * Any listener can throw a {@code PropertyVetoException} to veto the update.
253 * If one of the listeners vetoes the update, this method passes
254 * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
255 * to all listeners that already confirmed this update
256 * and throws the {@code PropertyVetoException} again.
258 * No event is fired if old and new values are equal and non-null.
260 * This is merely a convenience wrapper around the more general
261 * {@link #fireVetoableChange(PropertyChangeEvent)} method.
263 * @param propertyName the programmatic name of the property that is about to change
264 * @param oldValue the old value of the property
265 * @param newValue the new value of the property
266 * @throws PropertyVetoException if one of listeners vetoes the property update
268 public void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
269 throws PropertyVetoException {
270 if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
271 fireVetoableChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
276 * Reports an integer constrained property update to listeners
277 * that have been registered to track updates of
278 * all properties or a property with the specified name.
280 * Any listener can throw a {@code PropertyVetoException} to veto the update.
281 * If one of the listeners vetoes the update, this method passes
282 * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
283 * to all listeners that already confirmed this update
284 * and throws the {@code PropertyVetoException} again.
286 * No event is fired if old and new values are equal.
288 * This is merely a convenience wrapper around the more general
289 * {@link #fireVetoableChange(String, Object, Object)} method.
291 * @param propertyName the programmatic name of the property that is about to change
292 * @param oldValue the old value of the property
293 * @param newValue the new value of the property
294 * @throws PropertyVetoException if one of listeners vetoes the property update
296 public void fireVetoableChange(String propertyName, int oldValue, int newValue)
297 throws PropertyVetoException {
298 if (oldValue != newValue) {
299 fireVetoableChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
304 * Reports a boolean constrained property update to listeners
305 * that have been registered to track updates of
306 * all properties or a property with the specified name.
308 * Any listener can throw a {@code PropertyVetoException} to veto the update.
309 * If one of the listeners vetoes the update, this method passes
310 * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
311 * to all listeners that already confirmed this update
312 * and throws the {@code PropertyVetoException} again.
314 * No event is fired if old and new values are equal.
316 * This is merely a convenience wrapper around the more general
317 * {@link #fireVetoableChange(String, Object, Object)} method.
319 * @param propertyName the programmatic name of the property that is about to change
320 * @param oldValue the old value of the property
321 * @param newValue the new value of the property
322 * @throws PropertyVetoException if one of listeners vetoes the property update
324 public void fireVetoableChange(String propertyName, boolean oldValue, boolean newValue)
325 throws PropertyVetoException {
326 if (oldValue != newValue) {
327 fireVetoableChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
332 * Fires a property change event to listeners
333 * that have been registered to track updates of
334 * all properties or a property with the specified name.
336 * Any listener can throw a {@code PropertyVetoException} to veto the update.
337 * If one of the listeners vetoes the update, this method passes
338 * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
339 * to all listeners that already confirmed this update
340 * and throws the {@code PropertyVetoException} again.
342 * No event is fired if the given event's old and new values are equal and non-null.
344 * @param event the {@code PropertyChangeEvent} to be fired
345 * @throws PropertyVetoException if one of listeners vetoes the property update
347 public void fireVetoableChange(PropertyChangeEvent event)
348 throws PropertyVetoException {
349 Object oldValue = event.getOldValue();
350 Object newValue = event.getNewValue();
351 if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
352 String name = event.getPropertyName();
354 VetoableChangeListener[] common = this.map.get(null);
355 VetoableChangeListener[] named = (name != null)
359 VetoableChangeListener[] listeners;
360 if (common == null) {
363 else if (named == null) {
367 listeners = new VetoableChangeListener[common.length + named.length];
368 System.arraycopy(common, 0, listeners, 0, common.length);
369 System.arraycopy(named, 0, listeners, common.length, named.length);
371 if (listeners != null) {
374 while (current < listeners.length) {
375 listeners[current].vetoableChange(event);
379 catch (PropertyVetoException veto) {
380 event = new PropertyChangeEvent(this.source, name, newValue, oldValue);
381 for (int i = 0; i < current; i++) {
383 listeners[i].vetoableChange(event);
385 catch (PropertyVetoException exception) {
386 // ignore exceptions that occur during rolling back
389 throw veto; // rethrow the veto exception
396 * Check if there are any listeners for a specific property, including
397 * those registered on all properties. If <code>propertyName</code>
398 * is null, only check for listeners registered on all properties.
400 * @param propertyName the property name.
401 * @return true if there are one or more listeners for the given property
403 public boolean hasListeners(String propertyName) {
404 return this.map.hasListeners(propertyName);
408 * @serialData Null terminated list of <code>VetoableChangeListeners</code>.
410 * At serialization time we skip non-serializable listeners and
411 * only serialize the serializable listeners.
413 private void writeObject(ObjectOutputStream s) throws IOException {
414 Hashtable<String, VetoableChangeSupport> children = null;
415 VetoableChangeListener[] listeners = null;
416 synchronized (this.map) {
417 for (Entry<String, VetoableChangeListener[]> entry : this.map.getEntries()) {
418 String property = entry.getKey();
419 if (property == null) {
420 listeners = entry.getValue();
422 if (children == null) {
423 children = new Hashtable<String, VetoableChangeSupport>();
425 VetoableChangeSupport vcs = new VetoableChangeSupport(this.source);
426 vcs.map.set(null, entry.getValue());
427 children.put(property, vcs);
431 ObjectOutputStream.PutField fields = s.putFields();
432 fields.put("children", children);
433 fields.put("source", this.source);
434 fields.put("vetoableChangeSupportSerializedDataVersion", 2);
437 if (listeners != null) {
438 for (VetoableChangeListener l : listeners) {
439 if (l instanceof Serializable) {
447 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
448 this.map = new VetoableChangeListenerMap();
450 ObjectInputStream.GetField fields = s.readFields();
452 Hashtable<String, VetoableChangeSupport> children = (Hashtable<String, VetoableChangeSupport>) fields.get("children", null);
453 this.source = fields.get("source", null);
454 fields.get("vetoableChangeSupportSerializedDataVersion", 2);
456 Object listenerOrNull;
457 while (null != (listenerOrNull = s.readObject())) {
458 this.map.add(null, (VetoableChangeListener)listenerOrNull);
460 if (children != null) {
461 for (Entry<String, VetoableChangeSupport> entry : children.entrySet()) {
462 for (VetoableChangeListener listener : entry.getValue().getVetoableChangeListeners()) {
463 this.map.add(entry.getKey(), listener);
470 * The object to be provided as the "source" for any generated events.
472 private Object source;
475 * @serialField children Hashtable
476 * @serialField source Object
477 * @serialField vetoableChangeSupportSerializedDataVersion int
479 private static final ObjectStreamField[] serialPersistentFields = {
480 new ObjectStreamField("children", Hashtable.class),
481 new ObjectStreamField("source", Object.class),
482 new ObjectStreamField("vetoableChangeSupportSerializedDataVersion", Integer.TYPE)
486 * Serialization version ID, so we're compatible with JDK 1.1
488 static final long serialVersionUID = -5090210921595982017L;
491 * This is a {@link ChangeListenerMap ChangeListenerMap} implementation
492 * that works with {@link VetoableChangeListener VetoableChangeListener} objects.
494 private static final class VetoableChangeListenerMap extends ChangeListenerMap<VetoableChangeListener> {
495 private static final VetoableChangeListener[] EMPTY = {};
498 * Creates an array of {@link VetoableChangeListener VetoableChangeListener} objects.
499 * This method uses the same instance of the empty array
500 * when {@code length} equals {@code 0}.
502 * @param length the array length
503 * @return an array with specified length
506 protected VetoableChangeListener[] newArray(int length) {
508 ? new VetoableChangeListener[length]
513 * Creates a {@link VetoableChangeListenerProxy VetoableChangeListenerProxy}
514 * object for the specified property.
516 * @param name the name of the property to listen on
517 * @param listener the listener to process events
518 * @return a {@code VetoableChangeListenerProxy} object
521 protected VetoableChangeListener newProxy(String name, VetoableChangeListener listener) {
522 return new VetoableChangeListenerProxy(name, listener);