1 | /* Observable.java -- an object to be observed
|
---|
2 | Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.util;
|
---|
40 |
|
---|
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>
|
---|
51 | * @author Eric Blake <ebb9@email.byu.edu>
|
---|
52 | * @see Observer
|
---|
53 | * @status updated to 1.4
|
---|
54 | */
|
---|
55 | public class Observable
|
---|
56 | {
|
---|
57 | /** Tracks whether this object has changed. */
|
---|
58 | private boolean changed;
|
---|
59 |
|
---|
60 | /* List of the Observers registered as interested in this Observable. */
|
---|
61 | private LinkedHashSet observers;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Constructs an Observable with zero Observers.
|
---|
65 | */
|
---|
66 | public Observable()
|
---|
67 | {
|
---|
68 | observers = new LinkedHashSet();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Adds an Observer. If the observer was already added this method does
|
---|
73 | * nothing.
|
---|
74 | *
|
---|
75 | * @param observer Observer to add
|
---|
76 | * @throws NullPointerException if observer is null
|
---|
77 | */
|
---|
78 | public synchronized void addObserver(Observer observer)
|
---|
79 | {
|
---|
80 | observers.add(observer);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Reset this Observable's state to unchanged. This is called automatically
|
---|
85 | * by <code>notifyObservers</code> once all observers have been notified.
|
---|
86 | *
|
---|
87 | * @see #notifyObservers()
|
---|
88 | */
|
---|
89 | protected synchronized void clearChanged()
|
---|
90 | {
|
---|
91 | changed = false;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Returns the number of observers for this object.
|
---|
96 | *
|
---|
97 | * @return number of Observers for this
|
---|
98 | */
|
---|
99 | public synchronized int countObservers()
|
---|
100 | {
|
---|
101 | return observers.size();
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Deletes an Observer of this Observable.
|
---|
106 | *
|
---|
107 | * @param victim Observer to delete
|
---|
108 | */
|
---|
109 | public synchronized void deleteObserver(Observer victim)
|
---|
110 | {
|
---|
111 | observers.remove(victim);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Deletes all Observers of this Observable.
|
---|
116 | */
|
---|
117 | public synchronized void deleteObservers()
|
---|
118 | {
|
---|
119 | observers.clear();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * True if <code>setChanged</code> has been called more recently than
|
---|
124 | * <code>clearChanged</code>.
|
---|
125 | *
|
---|
126 | * @return whether or not this Observable has changed
|
---|
127 | */
|
---|
128 | public synchronized boolean hasChanged()
|
---|
129 | {
|
---|
130 | return changed;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * If the Observable has actually changed then tell all Observers about it,
|
---|
135 | * then reset state to unchanged.
|
---|
136 | *
|
---|
137 | * @see #notifyObservers(Object)
|
---|
138 | * @see Observer#update(Observable, Object)
|
---|
139 | */
|
---|
140 | public void notifyObservers()
|
---|
141 | {
|
---|
142 | notifyObservers(null);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
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.
|
---|
150 | *
|
---|
151 | * @param obj argument to Observer's update method
|
---|
152 | * @see Observer#update(Observable, Object)
|
---|
153 | */
|
---|
154 | public void notifyObservers(Object obj)
|
---|
155 | {
|
---|
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)
|
---|
163 | {
|
---|
164 | s = (Set) observers.clone();
|
---|
165 | }
|
---|
166 | int i = s.size();
|
---|
167 | Iterator iter = s.iterator();
|
---|
168 | while (--i >= 0)
|
---|
169 | ((Observer) iter.next()).update(this, obj);
|
---|
170 | clearChanged();
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Marks this Observable as having changed.
|
---|
175 | */
|
---|
176 | protected synchronized void setChanged()
|
---|
177 | {
|
---|
178 | changed = true;
|
---|
179 | }
|
---|
180 | }
|
---|