1 |
/* java.util.Observable |
/* Observable.java -- an object to be observed |
2 |
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
package java.util; |
package java.util; |
40 |
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 |
|
|
* "The Java Language Specification", ISBN 0-201-63451-1 |
|
|
* plus online API docs for JDK 1.2 beta from http://www.javasoft.com. |
|
|
* Status: Believed complete and correct. |
|
|
*/ |
|
|
|
|
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. |
46 |
|
* |
47 |
|
* Note that the <code>notifyObservers()</code> method of this class is |
48 |
|
* unrelated to the <code>notify()</code> of Object. |
49 |
|
* |
50 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy <warrenl@cygnus.com> |
51 |
* @date September 2, 1998. |
* @author Eric Blake <ebb9@email.byu.edu> |
52 |
|
* @see Observer |
53 |
|
* @status updated to 1.4 |
54 |
*/ |
*/ |
55 |
public class Observable |
public class Observable |
56 |
{ |
{ |
57 |
/** tracks whether this object has changed */ |
/** Tracks whether this object has changed. */ |
58 |
private boolean changed; |
private boolean changed; |
59 |
|
|
60 |
/* list of the Observers registered as interested in this Observable */ |
/* List of the Observers registered as interested in this Observable. */ |
61 |
private Vector observers; |
private Vector observers; |
62 |
|
|
63 |
/* TBD: This might be better implemented as an Observer[] |
/* TBD: This might be better implemented as an Observer[] |
81 |
* Adds an Observer. If the observer was already added this method does |
* Adds an Observer. If the observer was already added this method does |
82 |
* nothing. |
* nothing. |
83 |
* |
* |
84 |
* @param observer Observer to add. |
* @param observer Observer to add |
85 |
|
* @throws NullPointerException if observer is null |
86 |
*/ |
*/ |
87 |
public synchronized void addObserver(Observer observer) |
public synchronized void addObserver(Observer observer) |
88 |
{ |
{ |
89 |
if (!observers.contains(observer)) |
if (! observers.contains(observer)) |
90 |
observers.addElement(observer); |
observers.addElement(observer); |
91 |
} |
} |
92 |
|
|
93 |
/** |
/** |
94 |
* Reset this Observable's state to unchanged. |
* Reset this Observable's state to unchanged. This is called automatically |
95 |
|
* by <code>notifyObservers</code> once all observers have been notified. |
96 |
|
* |
97 |
|
* @see #notifyObservers() |
98 |
*/ |
*/ |
99 |
protected synchronized void clearChanged() |
protected synchronized void clearChanged() |
100 |
{ |
{ |
102 |
} |
} |
103 |
|
|
104 |
/** |
/** |
105 |
* @return Number of Observers for this Observable. |
* Returns the number of observers for this object. |
106 |
|
* |
107 |
|
* @return number of Observers for this |
108 |
*/ |
*/ |
109 |
public synchronized int countObservers() |
public synchronized int countObservers() |
110 |
{ |
{ |
114 |
/** |
/** |
115 |
* Deletes an Observer of this Observable. |
* Deletes an Observer of this Observable. |
116 |
* |
* |
117 |
* @param victim Observer to delete. |
* @param victim Observer to delete |
118 |
*/ |
*/ |
119 |
public synchronized void deleteObserver(Observer victim) |
public synchronized void deleteObserver(Observer victim) |
120 |
{ |
{ |
130 |
} |
} |
131 |
|
|
132 |
/** |
/** |
133 |
* @return Whether or not this Observable has changed. |
* True if <code>setChanged</code> has been called more recently than |
134 |
|
* <code>clearChanged</code>. |
135 |
|
* |
136 |
|
* @return whether or not this Observable has changed |
137 |
*/ |
*/ |
138 |
public synchronized boolean hasChanged() |
public synchronized boolean hasChanged() |
139 |
{ |
{ |
142 |
|
|
143 |
/** |
/** |
144 |
* If the Observable has actually changed then tell all Observers about it, |
* If the Observable has actually changed then tell all Observers about it, |
145 |
* then resets state to unchanged. |
* then reset state to unchanged. |
146 |
|
* |
147 |
|
* @see #notifyObservers(Object) |
148 |
|
* @see Observer#update(Observable, Object) |
149 |
*/ |
*/ |
150 |
public void notifyObservers() |
public void notifyObservers() |
151 |
{ |
{ |
154 |
|
|
155 |
/** |
/** |
156 |
* If the Observable has actually changed then tell all Observers about it, |
* If the Observable has actually changed then tell all Observers about it, |
157 |
* then resets state to unchanged. |
* then reset state to unchanged. Note that though the order of |
158 |
* Note that though the order of notification is unspecified in subclasses, |
* notification is unspecified in subclasses, in Observable it is in the |
159 |
* in Observable it is in the order of registration. |
* order of registration. |
160 |
* |
* |
161 |
* @param obj Arguement to Observer's update method. |
* @param obj argument to Observer's update method |
162 |
|
* @see Observer#update(Observable, Object) |
163 |
*/ |
*/ |
164 |
public void notifyObservers(Object obj) |
public void notifyObservers(Object obj) |
165 |
{ |
{ |
166 |
if (!hasChanged()) |
if (! hasChanged()) |
167 |
return; |
return; |
168 |
Vector ob1 = (Vector) observers.clone(); |
Vector ob1 = (Vector) observers.clone(); |
169 |
|
|