Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / util / Observable.java
blob4fd9919e4c0e1babe7f17938f11d8002c487f942
1 /* Observable.java -- an object to be observed
2 Copyright (C) 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 /**
42 * This class represents an object which is observable. Other objects may
43 * register their intent to be notified when this object changes; and when
44 * this object does change, it will trigger the <code>update</code> method
45 * of each observer.
47 * Note that the <code>notifyObservers()</code> method of this class is
48 * unrelated to the <code>notify()</code> of Object.
50 * @author Warren Levy (warrenl@cygnus.com)
51 * @author Eric Blake (ebb9@email.byu.edu)
52 * @see Observer
53 * @status updated to 1.4
55 public class Observable
57 /** Tracks whether this object has changed. */
58 private boolean changed;
60 /* List of the Observers registered as interested in this Observable. */
61 private LinkedHashSet observers;
63 /**
64 * Constructs an Observable with zero Observers.
66 public Observable()
68 observers = new LinkedHashSet();
71 /**
72 * Adds an Observer. If the observer was already added this method does
73 * nothing.
75 * @param observer Observer to add
76 * @throws NullPointerException if observer is null
78 public synchronized void addObserver(Observer observer)
80 observers.add(observer);
83 /**
84 * Reset this Observable's state to unchanged. This is called automatically
85 * by <code>notifyObservers</code> once all observers have been notified.
87 * @see #notifyObservers()
89 protected synchronized void clearChanged()
91 changed = false;
94 /**
95 * Returns the number of observers for this object.
97 * @return number of Observers for this
99 public synchronized int countObservers()
101 return observers.size();
105 * Deletes an Observer of this Observable.
107 * @param victim Observer to delete
109 public synchronized void deleteObserver(Observer victim)
111 observers.remove(victim);
115 * Deletes all Observers of this Observable.
117 public synchronized void deleteObservers()
119 observers.clear();
123 * True if <code>setChanged</code> has been called more recently than
124 * <code>clearChanged</code>.
126 * @return whether or not this Observable has changed
128 public synchronized boolean hasChanged()
130 return changed;
134 * If the Observable has actually changed then tell all Observers about it,
135 * then reset state to unchanged.
137 * @see #notifyObservers(Object)
138 * @see Observer#update(Observable, Object)
140 public void notifyObservers()
142 notifyObservers(null);
146 * If the Observable has actually changed then tell all Observers about it,
147 * then reset state to unchanged. Note that though the order of
148 * notification is unspecified in subclasses, in Observable it is in the
149 * order of registration.
151 * @param obj argument to Observer's update method
152 * @see Observer#update(Observable, Object)
154 public void notifyObservers(Object obj)
156 if (! hasChanged())
157 return;
158 // Create clone inside monitor, as that is relatively fast and still
159 // important to keep threadsafe, but update observers outside of the
160 // lock since update() can call arbitrary code.
161 Set s;
162 synchronized (this)
164 s = (Set) observers.clone();
166 int i = s.size();
167 Iterator iter = s.iterator();
168 while (--i >= 0)
169 ((Observer) iter.next()).update(this, obj);
170 clearChanged();
174 * Marks this Observable as having changed.
176 protected synchronized void setChanged()
178 changed = true;