Observable.java revision 0
0N/A * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * This class represents an observable object, or "data" 0N/A * in the model-view paradigm. It can be subclassed to represent an 0N/A * object that the application wants to have observed. 0N/A * An observable object can have one or more observers. An observer 0N/A * may be any object that implements interface <tt>Observer</tt>. After an 0N/A * observable instance changes, an application calling the 0N/A * <code>Observable</code>'s <code>notifyObservers</code> method 0N/A * causes all of its observers to be notified of the change by a call 0N/A * to their <code>update</code> method. 0N/A * The order in which notifications will be delivered is unspecified. 0N/A * The default implementation provided in the Observable class will 0N/A * notify Observers in the order in which they registered interest, but 0N/A * subclasses may change this order, use no guaranteed order, deliver 0N/A * notifications on separate threads, or may guarantee that their 0N/A * subclass follows this order, as they choose. 0N/A * Note that this notification mechanism is has nothing to do with threads 0N/A * and is completely separate from the <tt>wait</tt> and <tt>notify</tt> 0N/A * mechanism of class <tt>Object</tt>. 0N/A * When an observable object is newly created, its set of observers is 0N/A * empty. Two observers are considered the same if and only if the 0N/A * <tt>equals</tt> method returns true for them. 0N/A * @author Chris Warth 0N/A * @see java.util.Observable#notifyObservers() 0N/A * @see java.util.Observable#notifyObservers(java.lang.Object) 0N/A * @see java.util.Observer 0N/A * @see java.util.Observer#update(java.util.Observable, java.lang.Object) 0N/A /** Construct an Observable with zero Observers. */ 0N/A * Adds an observer to the set of observers for this object, provided 0N/A * that it is not the same as some observer already in the set. 0N/A * The order in which notifications will be delivered to multiple 0N/A * observers is not specified. See the class comment. 0N/A * @param o an observer to be added. 0N/A * @throws NullPointerException if the parameter o is null. 0N/A * Deletes an observer from the set of observers of this object. 0N/A * Passing <CODE>null</CODE> to this method will have no effect. 0N/A * @param o the observer to be deleted. 0N/A * If this object has changed, as indicated by the 0N/A * <code>hasChanged</code> method, then notify all of its observers 0N/A * and then call the <code>clearChanged</code> method to 0N/A * indicate that this object has no longer changed. 0N/A * Each observer has its <code>update</code> method called with two 0N/A * arguments: this observable object and <code>null</code>. In other 0N/A * words, this method is equivalent to: 0N/A * notifyObservers(null)</tt></blockquote> 0N/A * @see java.util.Observable#clearChanged() 0N/A * @see java.util.Observable#hasChanged() 0N/A * @see java.util.Observer#update(java.util.Observable, java.lang.Object) 0N/A * If this object has changed, as indicated by the 0N/A * <code>hasChanged</code> method, then notify all of its observers 0N/A * and then call the <code>clearChanged</code> method to indicate 0N/A * that this object has no longer changed. 0N/A * Each observer has its <code>update</code> method called with two 0N/A * arguments: this observable object and the <code>arg</code> argument. 0N/A * @param arg any object. 0N/A * @see java.util.Observable#clearChanged() 0N/A * @see java.util.Observable#hasChanged() 0N/A * @see java.util.Observer#update(java.util.Observable, java.lang.Object) 0N/A * a temporary array buffer, used as a snapshot of the state of 0N/A * current Observers. 0N/A synchronized (
this) {
0N/A /* We don't want the Observer doing callbacks into 0N/A * arbitrary code while holding its own Monitor. 0N/A * The code where we extract each Observable from 0N/A * the Vector and store the state of the Observer 0N/A * needs synchronization, but notifying observers 0N/A * does not (should not). The worst result of any 0N/A * potential race-condition here is that: 0N/A * 1) a newly-added Observer will miss a 0N/A * notification in progress 0N/A * 2) a recently unregistered Observer will be 0N/A * wrongly notified when it doesn't care 0N/A * Clears the observer list so that this object no longer has any observers. 0N/A * Marks this <tt>Observable</tt> object as having been changed; the 0N/A * <tt>hasChanged</tt> method will now return <tt>true</tt>. 0N/A * Indicates that this object has no longer changed, or that it has 0N/A * already notified all of its observers of its most recent change, 0N/A * so that the <tt>hasChanged</tt> method will now return <tt>false</tt>. 0N/A * This method is called automatically by the 0N/A * <code>notifyObservers</code> methods. 0N/A * @see java.util.Observable#notifyObservers() 0N/A * @see java.util.Observable#notifyObservers(java.lang.Object) 0N/A * Tests if this object has changed. 0N/A * @return <code>true</code> if and only if the <code>setChanged</code> 0N/A * method has been called more recently than the 0N/A * <code>clearChanged</code> method on this object; 0N/A * <code>false</code> otherwise. 0N/A * @see java.util.Observable#clearChanged() 0N/A * @see java.util.Observable#setChanged() 0N/A * Returns the number of observers of this <tt>Observable</tt> object. 0N/A * @return the number of observers of this object.