FileDocCategorySizeDatePackage
Observable.javaAPI DocAndroid 1.5 API5034Wed May 06 22:41:04 BST 2009java.util

Observable

public class Observable extends Object
Observable is used to notify a group of Observer objects when a change occurs. On creation, the set of observers is empty. After a change occurred, the application can call the {@link #notifyObservers()} method. This will cause the invocation of the {@code update()} method of all registered Observers. The order of invocation is not specified. This implementation will call the Observers in the order they registered. Subclasses are completely free in what order they call the update methods.
see
Observer
since
Android 1.0

Fields Summary
Vector
observers
boolean
changed
Constructors Summary
public Observable()
Constructs a new {@code Observable} object.

since
Android 1.0


                   
      
        super();
    
Methods Summary
public synchronized voidaddObserver(java.util.Observer observer)
Adds the specified observer to the list of observers. If it is already registered, it is not added a second time.

param
observer the Observer to add.
since
Android 1.0

        if (observer == null) {
            throw new NullPointerException();
        }
        if (!observers.contains(observer))
            observers.addElement(observer);
    
protected synchronized voidclearChanged()
Clears the changed flag for this {@code Observable}. After calling {@code clearChanged()}, {@code hasChanged()} will return {@code false}.

since
Android 1.0

        changed = false;
    
public synchronized intcountObservers()
Returns the number of observers registered to this {@code Observable}.

return
the number of observers.
since
Android 1.0

        return observers.size();
    
public synchronized voiddeleteObserver(java.util.Observer observer)
Removes the specified observer from the list of observers. Passing null won't do anything.

param
observer the observer to remove.
since
Android 1.0

        observers.removeElement(observer);
    
public synchronized voiddeleteObservers()
Removes all observers from the list of observers.

since
Android 1.0

        observers.setSize(0);
    
public synchronized booleanhasChanged()
Returns the changed flag for this {@code Observable}.

return
{@code true} when the changed flag for this {@code Observable} is set, {@code false} otherwise.
since
Android 1.0

        return changed;
    
public voidnotifyObservers()
If {@code hasChanged()} returns {@code true}, calls the {@code update()} method for every observer in the list of observers using null as the argument. Afterwards, calls {@code clearChanged()}.

Equivalent to calling {@code notifyObservers(null)}.

since
Android 1.0

        notifyObservers(null);
    
public voidnotifyObservers(java.lang.Object data)
If {@code hasChanged()} returns {@code true}, calls the {@code update()} method for every Observer in the list of observers using the specified argument. Afterwards calls {@code clearChanged()}.

param
data the argument passed to {@code update()}.
since
Android 1.0

        if (hasChanged()) {
            // Must clone the vector in case deleteObserver is called
            Vector<Observer> clone = (Vector<Observer>)observers.clone();
            int size = clone.size();
            for (int i = 0; i < size; i++) {
                clone.elementAt(i).update(this, data);
            }
            clearChanged();
        }
    
protected synchronized voidsetChanged()
Sets the changed flag for this {@code Observable}. After calling {@code setChanged()}, {@code hasChanged()} will return {@code true}.

since
Android 1.0

        changed = true;