1. /*
  2. * @(#)Observable.java 1.35 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * This class represents an observable object, or "data"
  10. * in the model-view paradigm. It can be subclassed to represent an
  11. * object that the application wants to have observed.
  12. * <p>
  13. * An observable object can have one or more observers. An observer
  14. * may be any object that implements interface <tt>Observer</tt>. After an
  15. * observable instance changes, an application calling the
  16. * <code>Observable</code>'s <code>notifyObservers</code> method
  17. * causes all of its observers to be notified of the change by a call
  18. * to their <code>update</code> method.
  19. * <p>
  20. * The order in which notifications will be delivered is unspecified.
  21. * The default implementation provided in the Observerable class will
  22. * notify Observers in the order in which they registered interest, but
  23. * subclasses may change this order, use no guaranteed order, deliver
  24. * notifications on separate threads, or may guarantee that their
  25. * subclass follows this order, as they choose.
  26. * <p>
  27. * Note that this notification mechanism is has nothing to do with threads
  28. * and is completely separate from the <tt>wait</tt> and <tt>notify</tt>
  29. * mechanism of class <tt>Object</tt>.
  30. * <p>
  31. * When an observable object is newly created, its set of observers is
  32. * empty. Two observers are considered the same if and only if the
  33. * <tt>equals</tt> method returns true for them.
  34. *
  35. * @author Chris Warth
  36. * @version 1.35, 01/23/03
  37. * @see java.util.Observable#notifyObservers()
  38. * @see java.util.Observable#notifyObservers(java.lang.Object)
  39. * @see java.util.Observer
  40. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
  41. * @since JDK1.0
  42. */
  43. public class Observable {
  44. private boolean changed = false;
  45. private Vector obs;
  46. /** Construct an Observable with zero Observers. */
  47. public Observable() {
  48. obs = new Vector();
  49. }
  50. /**
  51. * Adds an observer to the set of observers for this object, provided
  52. * that it is not the same as some observer already in the set.
  53. * The order in which notifications will be delivered to multiple
  54. * observers is not specified. See the class comment.
  55. *
  56. * @param o an observer to be added.
  57. * @throws NullPointerException if the parameter o is null.
  58. */
  59. public synchronized void addObserver(Observer o) {
  60. if (o == null)
  61. throw new NullPointerException();
  62. if (!obs.contains(o)) {
  63. obs.addElement(o);
  64. }
  65. }
  66. /**
  67. * Deletes an observer from the set of observers of this object.
  68. *
  69. * @param o the observer to be deleted.
  70. */
  71. public synchronized void deleteObserver(Observer o) {
  72. obs.removeElement(o);
  73. }
  74. /**
  75. * If this object has changed, as indicated by the
  76. * <code>hasChanged</code> method, then notify all of its observers
  77. * and then call the <code>clearChanged</code> method to
  78. * indicate that this object has no longer changed.
  79. * <p>
  80. * Each observer has its <code>update</code> method called with two
  81. * arguments: this observable object and <code>null</code>. In other
  82. * words, this method is equivalent to:
  83. * <blockquote><tt>
  84. * notifyObservers(null)</tt></blockquote>
  85. *
  86. * @see java.util.Observable#clearChanged()
  87. * @see java.util.Observable#hasChanged()
  88. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
  89. */
  90. public void notifyObservers() {
  91. notifyObservers(null);
  92. }
  93. /**
  94. * If this object has changed, as indicated by the
  95. * <code>hasChanged</code> method, then notify all of its observers
  96. * and then call the <code>clearChanged</code> method to indicate
  97. * that this object has no longer changed.
  98. * <p>
  99. * Each observer has its <code>update</code> method called with two
  100. * arguments: this observable object and the <code>arg</code> argument.
  101. *
  102. * @param arg any object.
  103. * @see java.util.Observable#clearChanged()
  104. * @see java.util.Observable#hasChanged()
  105. * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
  106. */
  107. public void notifyObservers(Object arg) {
  108. /*
  109. * a temporary array buffer, used as a snapshot of the state of
  110. * current Observers.
  111. */
  112. Object[] arrLocal;
  113. synchronized (this) {
  114. /* We don't want the Observer doing callbacks into
  115. * arbitrary code while holding its own Monitor.
  116. * The code where we extract each Observable from
  117. * the Vector and store the state of the Observer
  118. * needs synchronization, but notifying observers
  119. * does not (should not). The worst result of any
  120. * potential race-condition here is that:
  121. * 1) a newly-added Observer will miss a
  122. * notification in progress
  123. * 2) a recently unregistered Observer will be
  124. * wrongly notified when it doesn't care
  125. */
  126. if (!changed)
  127. return;
  128. arrLocal = obs.toArray();
  129. clearChanged();
  130. }
  131. for (int i = arrLocal.length-1; i>=0; i--)
  132. ((Observer)arrLocal[i]).update(this, arg);
  133. }
  134. /**
  135. * Clears the observer list so that this object no longer has any observers.
  136. */
  137. public synchronized void deleteObservers() {
  138. obs.removeAllElements();
  139. }
  140. /**
  141. * Marks this <tt>Observable</tt> object as having been changed; the
  142. * <tt>hasChanged</tt> method will now return <tt>true</tt>.
  143. */
  144. protected synchronized void setChanged() {
  145. changed = true;
  146. }
  147. /**
  148. * Indicates that this object has no longer changed, or that it has
  149. * already notified all of its observers of its most recent change,
  150. * so that the <tt>hasChanged</tt> method will now return <tt>false</tt>.
  151. * This method is called automatically by the
  152. * <code>notifyObservers</code> methods.
  153. *
  154. * @see java.util.Observable#notifyObservers()
  155. * @see java.util.Observable#notifyObservers(java.lang.Object)
  156. */
  157. protected synchronized void clearChanged() {
  158. changed = false;
  159. }
  160. /**
  161. * Tests if this object has changed.
  162. *
  163. * @return <code>true</code> if and only if the <code>setChanged</code>
  164. * method has been called more recently than the
  165. * <code>clearChanged</code> method on this object;
  166. * <code>false</code> otherwise.
  167. * @see java.util.Observable#clearChanged()
  168. * @see java.util.Observable#setChanged()
  169. */
  170. public synchronized boolean hasChanged() {
  171. return changed;
  172. }
  173. /**
  174. * Returns the number of observers of this <tt>Observable</tt> object.
  175. *
  176. * @return the number of observers of this object.
  177. */
  178. public synchronized int countObservers() {
  179. return obs.size();
  180. }
  181. }