View Javadoc
1   package net.sourceforge.rpgee.utility;
2   
3   import java.util.Vector;
4   
5   import net.sourceforge.rpgee.Identified;
6   import net.sourceforge.rpgee.MutableIdentified;
7   import net.sourceforge.rpgee.Named;
8   import net.sourceforge.rpgee.exceptions.IdentityException;
9   
10  public class BaseObservable<T> implements Observable<T>, Identified<String>, MutableIdentified<String>, Named {
11      private boolean          changed = false;
12      private Vector<Observer> obs;
13      private String           id;
14      private String           name;
15  
16      /** Construct an BaseObservable with zero Observers. */
17      public BaseObservable() {
18          this(null);
19      }
20  
21      public BaseObservable(String _id) {
22          obs = new Vector<Observer>();
23          id = _id;
24      }
25  
26      /*
27       * (non-Javadoc)
28       * 
29       * @see net.sourceforge.rpgee.utility.Observable#addObserver(net.sourceforge.rpgee.utility.Observer)
30       */
31      public synchronized void addObserver(Observer o) {
32          if (o == null)
33              throw new NullPointerException();
34          if (!obs.contains(o)) {
35              obs.addElement(o);
36          }
37      }
38  
39      /*
40       * (non-Javadoc)
41       * 
42       * @see net.sourceforge.rpgee.utility.Observable#deleteObserver(net.sourceforge.rpgee.utility.Observer)
43       */
44      public synchronized void deleteObserver(Observer o) {
45          obs.removeElement(o);
46      }
47  
48      /*
49       * (non-Javadoc)
50       * 
51       * @see net.sourceforge.rpgee.utility.Observable#notifyObservers()
52       */
53      public void notifyObservers() throws Exception {
54          notifyObservers(null);
55      }
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see net.sourceforge.rpgee.utility.Observable#notifyObservers(java.lang.Object)
61       */
62      public void notifyObservers(T arg) throws Exception {
63          synchronized (this) {
64              if (!changed)
65                  return;
66              clearChanged();
67              for (Observer g : obs)
68                  g.update(this, arg);
69          }
70          this.clearChanged();
71      }
72  
73      /*
74       * (non-Javadoc)
75       * 
76       * @see net.sourceforge.rpgee.utility.Observable#deleteObservers()
77       */
78      public synchronized void deleteObservers() {
79          obs.removeAllElements();
80      }
81  
82      /**
83       * Marks this <tt>BaseObservable</tt> object as having been changed; the <tt>hasChanged</tt> method will now return
84       * <tt>true</tt>.
85       */
86      protected synchronized void setChanged() {
87          changed = true;
88      }
89  
90      /**
91       * Indicates that this object has no longer changed, or that it has already notified all of its observers of its most recent
92       * change, so that the <tt>hasChanged</tt> method will now return <tt>false</tt>. This method is called automatically by
93       * the <code>notifyObservers</code> methods.
94       * 
95       * @see java.util.Observable#notifyObservers()
96       * @see java.util.Observable#notifyObservers(java.lang.Object)
97       */
98      protected synchronized void clearChanged() {
99          changed = false;
100     }
101 
102     public synchronized boolean hasChanged() {
103         return changed;
104     }
105 
106     public synchronized int countObservers() {
107         return obs.size();
108     }
109 
110     public String getId() {
111         return id;
112     }
113 
114     public String getIdAsString() {
115         return id;
116     }
117 
118     public void setId(String _id) throws IdentityException {
119         id = _id;
120     }
121 
122     public String getName() {
123         return name;
124     }
125 
126     protected void setName(String name) {
127         this.name = name;
128     }
129 
130 }