/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/signals/eval/EncapsulatingSignal.java

http://ambienttalk.googlecode.com/ · Java · 81 lines · 24 code · 10 blank · 47 comment · 1 complexity · c3e7cbbd1a3973a83b895c9f6f7ddb8f MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * EncapsulatingReactiveComputation.java created on Jun 10, 2008 at 8:48:38 PM
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.signals.eval;
  29. import java.util.HashSet;
  30. import java.util.Iterator;
  31. import edu.vub.at.signals.SignalListener;
  32. /**
  33. * When creating a dependency network inside the body of a method, this network may depend implicitly
  34. * on other reactive computations, for instance if the method was called with another behavior as the
  35. * receiver or as an argument of that method. EncapsulatingSignal instances are managed in a dynamic
  36. * variable by the interpreter to keep track of these implicit dependencies.
  37. *
  38. * Hence, when the method is triggered due to changes in an argument or the receiver, the dependency
  39. * network that was constructed in the previous invocation is automatically dismantled.
  40. *
  41. * @author smostinc
  42. */
  43. public class EncapsulatingSignal {
  44. private HashSet nestedSignalListeners_ = new HashSet();
  45. private int dependencyHeight_;
  46. public EncapsulatingSignal(int dependencyHeight) {
  47. dependencyHeight_ = dependencyHeight;
  48. }
  49. /**
  50. * When dependent computations are created within the control flow of this encapsulating
  51. * signal, their minimal dependency height should be greater than the dependency height
  52. * of the encapsulating signal.
  53. */
  54. public int dependencyHeight() {
  55. return dependencyHeight_;
  56. }
  57. /**
  58. * When the encapsulating reactive computation needs to be performed anew, all dependent
  59. * computations that were constructed within its dynamic extent need need to be unhooked.
  60. */
  61. public synchronized void unhookAllDependencies() {
  62. for (Iterator nestedSignalIt = nestedSignalListeners_.iterator(); nestedSignalIt.hasNext();) {
  63. SignalListener currentSignalListener = (SignalListener) nestedSignalIt.next();
  64. currentSignalListener.unhook();
  65. nestedSignalIt.remove();
  66. }
  67. }
  68. public synchronized void registerDependency(SignalListener theNestedListener) {
  69. nestedSignalListeners_.add(theNestedListener);
  70. }
  71. }