Observable.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Observable.java -- an object to be observed
  2. Copyright (C) 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * This class represents an object which is observable. Other objects may
  34. * register their intent to be notified when this object changes; and when
  35. * this object does change, it will trigger the <code>update</code> method
  36. * of each observer.
  37. *
  38. * Note that the <code>notifyObservers()</code> method of this class is
  39. * unrelated to the <code>notify()</code> of Object.
  40. *
  41. * @author Warren Levy (warrenl@cygnus.com)
  42. * @author Eric Blake (ebb9@email.byu.edu)
  43. * @see Observer
  44. * @status updated to 1.4
  45. */
  46. public class Observable
  47. {
  48. /** Tracks whether this object has changed. */
  49. private boolean changed;
  50. /* List of the Observers registered as interested in this Observable. */
  51. private LinkedHashSet observers;
  52. /**
  53. * Constructs an Observable with zero Observers.
  54. */
  55. public Observable()
  56. {
  57. observers = new LinkedHashSet();
  58. }
  59. /**
  60. * Adds an Observer. If the observer was already added this method does
  61. * nothing.
  62. *
  63. * @param observer Observer to add
  64. * @throws NullPointerException if observer is null
  65. */
  66. public synchronized void addObserver(Observer observer)
  67. {
  68. if (observer == null)
  69. throw new NullPointerException("can't add null observer");
  70. observers.add(observer);
  71. }
  72. /**
  73. * Reset this Observable's state to unchanged. This is called automatically
  74. * by <code>notifyObservers</code> once all observers have been notified.
  75. *
  76. * @see #notifyObservers()
  77. */
  78. protected synchronized void clearChanged()
  79. {
  80. changed = false;
  81. }
  82. /**
  83. * Returns the number of observers for this object.
  84. *
  85. * @return number of Observers for this
  86. */
  87. public synchronized int countObservers()
  88. {
  89. return observers.size();
  90. }
  91. /**
  92. * Deletes an Observer of this Observable.
  93. *
  94. * @param victim Observer to delete
  95. */
  96. public synchronized void deleteObserver(Observer victim)
  97. {
  98. observers.remove(victim);
  99. }
  100. /**
  101. * Deletes all Observers of this Observable.
  102. */
  103. public synchronized void deleteObservers()
  104. {
  105. observers.clear();
  106. }
  107. /**
  108. * True if <code>setChanged</code> has been called more recently than
  109. * <code>clearChanged</code>.
  110. *
  111. * @return whether or not this Observable has changed
  112. */
  113. public synchronized boolean hasChanged()
  114. {
  115. return changed;
  116. }
  117. /**
  118. * If the Observable has actually changed then tell all Observers about it,
  119. * then reset state to unchanged.
  120. *
  121. * @see #notifyObservers(Object)
  122. * @see Observer#update(Observable, Object)
  123. */
  124. public void notifyObservers()
  125. {
  126. notifyObservers(null);
  127. }
  128. /**
  129. * If the Observable has actually changed then tell all Observers about it,
  130. * then reset state to unchanged. Note that though the order of
  131. * notification is unspecified in subclasses, in Observable it is in the
  132. * order of registration.
  133. *
  134. * @param obj argument to Observer's update method
  135. * @see Observer#update(Observable, Object)
  136. */
  137. public void notifyObservers(Object obj)
  138. {
  139. if (! hasChanged())
  140. return;
  141. // Create clone inside monitor, as that is relatively fast and still
  142. // important to keep threadsafe, but update observers outside of the
  143. // lock since update() can call arbitrary code.
  144. Set s;
  145. synchronized (this)
  146. {
  147. s = (Set) observers.clone();
  148. }
  149. int i = s.size();
  150. Iterator iter = s.iterator();
  151. while (--i >= 0)
  152. ((Observer) iter.next()).update(this, obj);
  153. clearChanged();
  154. }
  155. /**
  156. * Marks this Observable as having changed.
  157. */
  158. protected synchronized void setChanged()
  159. {
  160. changed = true;
  161. }
  162. }