java.util

Class Observable


public class Observable
extends Object

This class represents an object which is observable. Other objects may register their intent to be notified when this object changes; and when this object does change, it will trigger the update method of each observer. Note that the notifyObservers() method of this class is unrelated to the notify() of Object.
See Also:
Observer

Constructor Summary

Observable()
Constructs an Observable with zero Observers.

Method Summary

void
addObserver(Observer observer)
Adds an Observer.
protected void
clearChanged()
Reset this Observable's state to unchanged.
int
countObservers()
Returns the number of observers for this object.
void
deleteObserver(Observer victim)
Deletes an Observer of this Observable.
void
deleteObservers()
Deletes all Observers of this Observable.
boolean
hasChanged()
True if setChanged has been called more recently than clearChanged.
void
notifyObservers()
If the Observable has actually changed then tell all Observers about it, then reset state to unchanged.
void
notifyObservers(Object obj)
If the Observable has actually changed then tell all Observers about it, then reset state to unchanged.
protected void
setChanged()
Marks this Observable as having changed.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Observable

public Observable()
Constructs an Observable with zero Observers.

Method Details

addObserver

public void addObserver(Observer observer)
Adds an Observer. If the observer was already added this method does nothing.
Parameters:
observer - Observer to add
Throws:
NullPointerException - if observer is null

clearChanged

protected void clearChanged()
Reset this Observable's state to unchanged. This is called automatically by notifyObservers once all observers have been notified.

countObservers

public int countObservers()
Returns the number of observers for this object.
Returns:
number of Observers for this

deleteObserver

public void deleteObserver(Observer victim)
Deletes an Observer of this Observable.
Parameters:
victim - Observer to delete

deleteObservers

public void deleteObservers()
Deletes all Observers of this Observable.

hasChanged

public boolean hasChanged()
True if setChanged has been called more recently than clearChanged.
Returns:
whether or not this Observable has changed

notifyObservers

public void notifyObservers()
If the Observable has actually changed then tell all Observers about it, then reset state to unchanged.

notifyObservers

public void notifyObservers(Object obj)
If the Observable has actually changed then tell all Observers about it, then reset state to unchanged. Note that though the order of notification is unspecified in subclasses, in Observable it is in the order of registration.
Parameters:
obj - argument to Observer's update method

setChanged

protected void setChanged()
Marks this Observable as having changed.

Observable.java -- an object to be observed Copyright (C) 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.