TransitiveObservable.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Modified by Yan Huang <yhuang@virginia.edu>
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package YaoGC;
  19. import java.util.*;
  20. /**
  21. * Observable is used to notify a group of Observer objects when a change
  22. * occurs. On creation, the set of observers is empty. After a change occurred,
  23. * the application can call the {@link #notifyObservers()} method. This will
  24. * cause the invocation of the {@code update()} method of all registered
  25. * Observers. The order of invocation is not specified. This implementation will
  26. * call the Observers in the order they registered. Subclasses are completely
  27. * free in what order they call the update methods.
  28. *
  29. * @see Observer
  30. */
  31. public class TransitiveObservable {
  32. List<TransitiveObserver> observers = new ArrayList<TransitiveObserver>();
  33. boolean changed = false;
  34. List<Socket> exports = new ArrayList<Socket>();
  35. static class Socket {
  36. Wire[] wires;
  37. int idx;
  38. public Socket(Wire[] ws, int i) {
  39. wires = ws;
  40. idx = i;
  41. }
  42. public void updateSocket(Wire w) {
  43. wires[idx] = w;
  44. }
  45. public boolean equals(Socket sock) {
  46. if (this == sock)
  47. return true;
  48. if (sock != null) {
  49. return (wires == sock.wires) && (idx == sock.idx);
  50. }
  51. return false;
  52. }
  53. }
  54. /**
  55. * Constructs a new {@code Observable} object.
  56. */
  57. public TransitiveObservable() {
  58. super();
  59. }
  60. /**
  61. * Adds the specified observer to the list of observers. If it is already
  62. * registered, it is not added a second time.
  63. *
  64. * @param observer
  65. * the Observer to add.
  66. */
  67. public void addObserver(TransitiveObserver observer, Socket sock) {
  68. if (observer == null) {
  69. throw new NullPointerException();
  70. }
  71. synchronized (this) {
  72. // if (!observers.contains(observer))
  73. observers.add(observer);
  74. exports.add(sock);
  75. }
  76. }
  77. /**
  78. * Clears the changed flag for this {@code Observable}. After calling
  79. * {@code clearChanged()}, {@code hasChanged()} will return {@code false}.
  80. */
  81. protected void clearChanged() {
  82. changed = false;
  83. }
  84. /**
  85. * Returns the number of observers registered to this {@code Observable}.
  86. *
  87. * @return the number of observers.
  88. */
  89. public int countObservers() {
  90. return observers.size();
  91. }
  92. /**
  93. * Removes the specified observer from the list of observers. Passing null
  94. * won't do anything.
  95. *
  96. * @param observer
  97. * the observer to remove.
  98. */
  99. public synchronized void deleteObserver(TransitiveObserver observer,
  100. Socket sock) {
  101. observers.remove(observer);
  102. exports.remove(sock);
  103. }
  104. /**
  105. * Removes all observers from the list of observers.
  106. */
  107. public synchronized void deleteObservers() {
  108. observers.clear();
  109. exports.clear();
  110. }
  111. /**
  112. * Returns the changed flag for this {@code Observable}.
  113. *
  114. * @return {@code true} when the changed flag for this {@code Observable} is
  115. * set, {@code false} otherwise.
  116. */
  117. public boolean hasChanged() {
  118. return changed;
  119. }
  120. /**
  121. * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
  122. * method for every observer in the list of observers using null as the
  123. * argument. Afterwards, calls {@code clearChanged()}.
  124. * <p>
  125. * Equivalent to calling {@code notifyObservers(null)}.
  126. */
  127. public void notifyObservers(boolean evaluate) {
  128. notifyObservers(evaluate, null);
  129. }
  130. /**
  131. * If {@code hasChanged()} returns {@code true}, calls the {@code update()}
  132. * method for every Observer in the list of observers using the specified
  133. * argument. Afterwards calls {@code clearChanged()}.
  134. *
  135. * @param data
  136. * the argument passed to {@code update()}.
  137. */
  138. public void notifyObservers(boolean evaluate, Object data) {
  139. int size = 0;
  140. TransitiveObserver[] arrays = null;
  141. synchronized (this) {
  142. if (hasChanged()) {
  143. clearChanged();
  144. size = observers.size();
  145. arrays = new TransitiveObserver[size];
  146. observers.toArray(arrays);
  147. }
  148. }
  149. if (arrays != null) {
  150. for (TransitiveObserver observer : arrays) {
  151. observer.update(evaluate, this, data);
  152. }
  153. }
  154. }
  155. /**
  156. * Sets the changed flag for this {@code Observable}. After calling
  157. * {@code setChanged()}, {@code hasChanged()} will return {@code true}.
  158. */
  159. protected void setChanged() {
  160. changed = true;
  161. }
  162. }