| 1 | /* java.util.Observable
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001 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 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 42 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 43 | * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|---|
| 44 | * Status: Believed complete and correct.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * @author Warren Levy <[email protected]>
|
|---|
| 49 | * @date September 2, 1998.
|
|---|
| 50 | */
|
|---|
| 51 | public class Observable
|
|---|
| 52 | {
|
|---|
| 53 | /** tracks whether this object has changed */
|
|---|
| 54 | private boolean changed;
|
|---|
| 55 |
|
|---|
| 56 | /* list of the Observers registered as interested in this Observable */
|
|---|
| 57 | private Vector observers;
|
|---|
| 58 |
|
|---|
| 59 | /* TBD: This might be better implemented as an Observer[]
|
|---|
| 60 | * but that would mean writing more code rather than making use of
|
|---|
| 61 | * the existing Vector class (this also implies a larger text code
|
|---|
| 62 | * space in resulting executables). The tradeoff is one of speed
|
|---|
| 63 | * (manipulating the Observer[] directly) vs. size/reuse. In the future,
|
|---|
| 64 | * we may decide to make the tradeoff and reimplement with an Observer[].
|
|---|
| 65 | */
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Constructs an Observable with zero Observers.
|
|---|
| 69 | */
|
|---|
| 70 | public Observable()
|
|---|
| 71 | {
|
|---|
| 72 | changed = false;
|
|---|
| 73 | observers = new Vector();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Adds an Observer. If the observer was already added this method does
|
|---|
| 78 | * nothing.
|
|---|
| 79 | *
|
|---|
| 80 | * @param observer Observer to add.
|
|---|
| 81 | */
|
|---|
| 82 | public synchronized void addObserver(Observer observer)
|
|---|
| 83 | {
|
|---|
| 84 | if (!observers.contains(observer))
|
|---|
| 85 | observers.addElement(observer);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Reset this Observable's state to unchanged.
|
|---|
| 90 | */
|
|---|
| 91 | protected synchronized void clearChanged()
|
|---|
| 92 | {
|
|---|
| 93 | changed = false;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * @return Number of Observers for this Observable.
|
|---|
| 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.removeElement(victim);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Deletes all Observers of this Observable.
|
|---|
| 116 | */
|
|---|
| 117 | public synchronized void deleteObservers()
|
|---|
| 118 | {
|
|---|
| 119 | observers.removeAllElements();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * @return Whether or not this Observable has changed.
|
|---|
| 124 | */
|
|---|
| 125 | public synchronized boolean hasChanged()
|
|---|
| 126 | {
|
|---|
| 127 | return changed;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * If the Observable has actually changed then tell all Observers about it,
|
|---|
| 132 | * then resets state to unchanged.
|
|---|
| 133 | */
|
|---|
| 134 | public void notifyObservers()
|
|---|
| 135 | {
|
|---|
| 136 | notifyObservers(null);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * If the Observable has actually changed then tell all Observers about it,
|
|---|
| 141 | * then resets state to unchanged.
|
|---|
| 142 | * Note that though the order of notification is unspecified in subclasses,
|
|---|
| 143 | * in Observable it is in the order of registration.
|
|---|
| 144 | *
|
|---|
| 145 | * @param obj Arguement to Observer's update method.
|
|---|
| 146 | */
|
|---|
| 147 | public void notifyObservers(Object obj)
|
|---|
| 148 | {
|
|---|
| 149 | if (!hasChanged())
|
|---|
| 150 | return;
|
|---|
| 151 | Vector ob1 = (Vector) observers.clone();
|
|---|
| 152 |
|
|---|
| 153 | for (int i = 0; i < ob1.size(); i++)
|
|---|
| 154 | ((Observer) ob1.elementAt(i)).update(this, obj);
|
|---|
| 155 |
|
|---|
| 156 | clearChanged();
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Marks this Observable as having changed.
|
|---|
| 161 | */
|
|---|
| 162 | protected synchronized void setChanged()
|
|---|
| 163 | {
|
|---|
| 164 | changed = true;
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|