author | smarks |
Thu, 28 Apr 2016 14:29:09 -0700 | |
changeset 37682 | faf3ae8eefc7 |
parent 32108 | aa5490a167ee |
child 44421 | 3ab80e087775 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
37682 | 2 |
* Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
28 |
/** |
|
29 |
* This class represents an observable object, or "data" |
|
30 |
* in the model-view paradigm. It can be subclassed to represent an |
|
31 |
* object that the application wants to have observed. |
|
32 |
* <p> |
|
33 |
* An observable object can have one or more observers. An observer |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
34 |
* may be any object that implements interface {@code Observer}. After an |
2 | 35 |
* observable instance changes, an application calling the |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
36 |
* {@code Observable}'s {@code notifyObservers} method |
2 | 37 |
* causes all of its observers to be notified of the change by a call |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
38 |
* to their {@code update} method. |
2 | 39 |
* <p> |
40 |
* The order in which notifications will be delivered is unspecified. |
|
41 |
* The default implementation provided in the Observable class will |
|
42 |
* notify Observers in the order in which they registered interest, but |
|
43 |
* subclasses may change this order, use no guaranteed order, deliver |
|
44 |
* notifications on separate threads, or may guarantee that their |
|
45 |
* subclass follows this order, as they choose. |
|
46 |
* <p> |
|
10350
6d009f117062
4748706: typos in java.util.Observable documentation
darcy
parents:
5506
diff
changeset
|
47 |
* Note that this notification mechanism has nothing to do with threads |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
48 |
* and is completely separate from the {@code wait} and {@code notify} |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
49 |
* mechanism of class {@code Object}. |
2 | 50 |
* <p> |
51 |
* When an observable object is newly created, its set of observers is |
|
52 |
* empty. Two observers are considered the same if and only if the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
53 |
* {@code equals} method returns true for them. |
2 | 54 |
* |
55 |
* @author Chris Warth |
|
56 |
* @see java.util.Observable#notifyObservers() |
|
57 |
* @see java.util.Observable#notifyObservers(java.lang.Object) |
|
58 |
* @see java.util.Observer |
|
59 |
* @see java.util.Observer#update(java.util.Observable, java.lang.Object) |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
14342
diff
changeset
|
60 |
* @since 1.0 |
37682 | 61 |
* |
62 |
* @deprecated |
|
63 |
* This class and the {@link Observer} interface have been deprecated. |
|
64 |
* The event model supported by {@code Observer} and {@code Observable} |
|
65 |
* is quite limited, the order of notifications delivered by |
|
66 |
* {@code Observable} is unspecified, and state changes are not in |
|
67 |
* one-for-one correspondence with notifications. |
|
68 |
* For a richer event model, consider using the |
|
69 |
* {@link java.beans} package. For reliable and ordered |
|
70 |
* messaging among threads, consider using one of the concurrent data |
|
71 |
* structures in the {@link java.util.concurrent} package. |
|
2 | 72 |
*/ |
37682 | 73 |
@Deprecated(since="9") |
2 | 74 |
public class Observable { |
75 |
private boolean changed = false; |
|
12448 | 76 |
private Vector<Observer> obs; |
2 | 77 |
|
78 |
/** Construct an Observable with zero Observers. */ |
|
79 |
||
80 |
public Observable() { |
|
12448 | 81 |
obs = new Vector<>(); |
2 | 82 |
} |
83 |
||
84 |
/** |
|
85 |
* Adds an observer to the set of observers for this object, provided |
|
86 |
* that it is not the same as some observer already in the set. |
|
87 |
* The order in which notifications will be delivered to multiple |
|
88 |
* observers is not specified. See the class comment. |
|
89 |
* |
|
90 |
* @param o an observer to be added. |
|
91 |
* @throws NullPointerException if the parameter o is null. |
|
92 |
*/ |
|
93 |
public synchronized void addObserver(Observer o) { |
|
94 |
if (o == null) |
|
95 |
throw new NullPointerException(); |
|
96 |
if (!obs.contains(o)) { |
|
97 |
obs.addElement(o); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Deletes an observer from the set of observers of this object. |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
103 |
* Passing {@code null} to this method will have no effect. |
2 | 104 |
* @param o the observer to be deleted. |
105 |
*/ |
|
106 |
public synchronized void deleteObserver(Observer o) { |
|
107 |
obs.removeElement(o); |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* If this object has changed, as indicated by the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
112 |
* {@code hasChanged} method, then notify all of its observers |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
113 |
* and then call the {@code clearChanged} method to |
2 | 114 |
* indicate that this object has no longer changed. |
115 |
* <p> |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
116 |
* Each observer has its {@code update} method called with two |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
117 |
* arguments: this observable object and {@code null}. In other |
2 | 118 |
* words, this method is equivalent to: |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
119 |
* <blockquote>{@code |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
120 |
* notifyObservers(null)}</blockquote> |
2 | 121 |
* |
122 |
* @see java.util.Observable#clearChanged() |
|
123 |
* @see java.util.Observable#hasChanged() |
|
124 |
* @see java.util.Observer#update(java.util.Observable, java.lang.Object) |
|
125 |
*/ |
|
126 |
public void notifyObservers() { |
|
127 |
notifyObservers(null); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* If this object has changed, as indicated by the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
132 |
* {@code hasChanged} method, then notify all of its observers |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
133 |
* and then call the {@code clearChanged} method to indicate |
2 | 134 |
* that this object has no longer changed. |
135 |
* <p> |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
136 |
* Each observer has its {@code update} method called with two |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
137 |
* arguments: this observable object and the {@code arg} argument. |
2 | 138 |
* |
139 |
* @param arg any object. |
|
140 |
* @see java.util.Observable#clearChanged() |
|
141 |
* @see java.util.Observable#hasChanged() |
|
142 |
* @see java.util.Observer#update(java.util.Observable, java.lang.Object) |
|
143 |
*/ |
|
144 |
public void notifyObservers(Object arg) { |
|
145 |
/* |
|
146 |
* a temporary array buffer, used as a snapshot of the state of |
|
147 |
* current Observers. |
|
148 |
*/ |
|
149 |
Object[] arrLocal; |
|
150 |
||
151 |
synchronized (this) { |
|
152 |
/* We don't want the Observer doing callbacks into |
|
153 |
* arbitrary code while holding its own Monitor. |
|
154 |
* The code where we extract each Observable from |
|
155 |
* the Vector and store the state of the Observer |
|
156 |
* needs synchronization, but notifying observers |
|
157 |
* does not (should not). The worst result of any |
|
158 |
* potential race-condition here is that: |
|
159 |
* 1) a newly-added Observer will miss a |
|
160 |
* notification in progress |
|
161 |
* 2) a recently unregistered Observer will be |
|
162 |
* wrongly notified when it doesn't care |
|
163 |
*/ |
|
164 |
if (!changed) |
|
165 |
return; |
|
166 |
arrLocal = obs.toArray(); |
|
167 |
clearChanged(); |
|
168 |
} |
|
169 |
||
170 |
for (int i = arrLocal.length-1; i>=0; i--) |
|
171 |
((Observer)arrLocal[i]).update(this, arg); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Clears the observer list so that this object no longer has any observers. |
|
176 |
*/ |
|
177 |
public synchronized void deleteObservers() { |
|
178 |
obs.removeAllElements(); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
182 |
* Marks this {@code Observable} object as having been changed; the |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
183 |
* {@code hasChanged} method will now return {@code true}. |
2 | 184 |
*/ |
185 |
protected synchronized void setChanged() { |
|
186 |
changed = true; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Indicates that this object has no longer changed, or that it has |
|
191 |
* already notified all of its observers of its most recent change, |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
192 |
* so that the {@code hasChanged} method will now return {@code false}. |
2 | 193 |
* This method is called automatically by the |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
194 |
* {@code notifyObservers} methods. |
2 | 195 |
* |
196 |
* @see java.util.Observable#notifyObservers() |
|
197 |
* @see java.util.Observable#notifyObservers(java.lang.Object) |
|
198 |
*/ |
|
199 |
protected synchronized void clearChanged() { |
|
200 |
changed = false; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Tests if this object has changed. |
|
205 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
206 |
* @return {@code true} if and only if the {@code setChanged} |
2 | 207 |
* method has been called more recently than the |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
208 |
* {@code clearChanged} method on this object; |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
209 |
* {@code false} otherwise. |
2 | 210 |
* @see java.util.Observable#clearChanged() |
211 |
* @see java.util.Observable#setChanged() |
|
212 |
*/ |
|
213 |
public synchronized boolean hasChanged() { |
|
214 |
return changed; |
|
215 |
} |
|
216 |
||
217 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
218 |
* Returns the number of observers of this {@code Observable} object. |
2 | 219 |
* |
220 |
* @return the number of observers of this object. |
|
221 |
*/ |
|
222 |
public synchronized int countObservers() { |
|
223 |
return obs.size(); |
|
224 |
} |
|
225 |
} |