* All files: Updated copyright to reflect Cygnus purchase.
[official-gcc.git] / libjava / java / util / Observable.java
blob6a924697cfab1cac740a2c83d6c6b5972e864549
1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package java.util;
11 /**
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date September 2, 1998.
15 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
16 * "The Java Language Specification", ISBN 0-201-63451-1
17 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct.
21 public class Observable
23 /* tracks whether this object has changed */
24 private boolean changed;
26 /* list of the Observers registered as interested in this Observable */
27 private Vector observerVec;
29 /* TBD: This might be better implemented as an Observer[]
30 * but that would mean writing more code rather than making use of
31 * the existing Vector class (this also implies a larger text code
32 * space in resulting executables). The tradeoff is one of speed
33 * (manipulating the Observer[] directly) vs. size/reuse. In the future,
34 * we may decide to make the tradeoff and reimplement with an Observer[].
37 public Observable()
39 changed = false;
40 observerVec = new Vector();
43 public synchronized void addObserver(Observer obs)
45 // JDK 1.2 spec says not to add this if it is already there
46 if (!observerVec.contains(obs))
47 observerVec.addElement(obs);
50 protected synchronized void clearChanged()
52 changed = false;
55 public synchronized int countObservers()
57 return observerVec.size();
60 public synchronized void deleteObserver(Observer obs)
62 observerVec.removeElement(obs);
65 public synchronized void deleteObservers()
67 observerVec.removeAllElements();
70 public synchronized boolean hasChanged()
72 return changed;
75 public void notifyObservers()
77 notifyObservers(null);
80 public void notifyObservers(Object arg)
82 if (changed)
84 /* The JDK 1.2 spec states that though the order of notification
85 * is unspecified in subclasses, in Observable it is in the order
86 * of registration.
88 for (int i = 0, numObs = observerVec.size(); i < numObs; i++)
89 ((Observer) (observerVec.elementAt(i))).update(this, arg);
90 changed = false;
94 protected synchronized void setChanged()
96 changed = true;