OSDN Git Service

* All files: Updated copyright to reflect Cygnus purchase.
[pf3gnuchains/gcc-fork.git] / libjava / java / util / Observable.java
1 /* Copyright (C) 1998, 1999  Red Hat, Inc.
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.util;
10  
11 /**
12  * @author Warren Levy <warrenl@cygnus.com>
13  * @date September 2, 1998.
14  */
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.
19  */
20  
21 public class Observable
22 {
23   /* tracks whether this object has changed */
24   private boolean changed;
25
26   /* list of the Observers registered as interested in this Observable */
27   private Vector observerVec;
28
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[].
35    */
36
37   public Observable()
38   {
39     changed = false;
40     observerVec = new Vector();
41   }
42
43   public synchronized void addObserver(Observer obs)
44   {
45     // JDK 1.2 spec says not to add this if it is already there
46     if (!observerVec.contains(obs))
47       observerVec.addElement(obs);
48   }
49
50   protected synchronized void clearChanged()
51   {
52     changed = false;
53   }
54
55   public synchronized int countObservers()
56   {
57     return observerVec.size();
58   }
59
60   public synchronized void deleteObserver(Observer obs)
61   {
62     observerVec.removeElement(obs);
63   }
64
65   public synchronized void deleteObservers()
66   {
67     observerVec.removeAllElements();
68   }
69
70   public synchronized boolean hasChanged()
71   {
72     return changed;
73   }
74
75   public void notifyObservers()
76   {
77     notifyObservers(null);
78   }
79
80   public void notifyObservers(Object arg)
81   {
82     if (changed)
83       {
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.
87          */
88         for (int i = 0, numObs = observerVec.size(); i < numObs; i++)
89           ((Observer) (observerVec.elementAt(i))).update(this, arg);
90         changed = false;
91       }
92   }
93
94   protected synchronized void setChanged()
95   {
96     changed = true;
97   }
98 }