NamedLocation.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2004 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. /** A Location that can be used as an entry in an Environment. */
  5. public abstract class NamedLocation<T> extends IndirectableLocation<T>
  6. implements
  7. java.util.Map.Entry<EnvironmentKey, T>,
  8. EnvironmentKey
  9. {
  10. NamedLocation next;
  11. public boolean entered ()
  12. {
  13. return next != null;
  14. }
  15. public Environment getEnvironment ()
  16. {
  17. for (NamedLocation loc = this; loc != null; loc = loc.next)
  18. {
  19. if (loc.name == null)
  20. {
  21. Environment env = (Environment) loc.value;
  22. if (env != null)
  23. return env;
  24. }
  25. }
  26. return super.getEnvironment();
  27. }
  28. final Symbol name;
  29. final Object property;
  30. public NamedLocation (NamedLocation loc)
  31. {
  32. name = loc.name;
  33. property = loc.property;
  34. }
  35. public NamedLocation (Symbol name, Object property)
  36. {
  37. this.name = name;
  38. this.property = property;
  39. }
  40. public final Symbol getKeySymbol ()
  41. {
  42. return name;
  43. }
  44. public final Object getKeyProperty ()
  45. {
  46. return property;
  47. }
  48. public final boolean matches (EnvironmentKey key)
  49. {
  50. return Symbol.equals(key.getKeySymbol(), this.name)
  51. && key.getKeyProperty() == this.property;
  52. }
  53. public final boolean matches (Symbol symbol, Object property)
  54. {
  55. return Symbol.equals(symbol, this.name) && property == this.property;
  56. }
  57. public final EnvironmentKey getKey ()
  58. {
  59. if (property == null)
  60. return name;
  61. else
  62. return this;
  63. }
  64. public boolean equals (Object x)
  65. {
  66. if (! (x instanceof NamedLocation))
  67. return false;
  68. NamedLocation e2 = (NamedLocation) x;
  69. if (name == null ? e2.name != null : ! name.equals(e2.name))
  70. return false;
  71. if (property != e2.property)
  72. return false;
  73. Object val1 = getValue();
  74. Object val2 = e2.getValue();
  75. if (val1 == val2)
  76. return true;
  77. if (val1 == null || val2 == null)
  78. return false;
  79. return val1.equals(val2);
  80. }
  81. public int hashCode ()
  82. {
  83. int h = name.hashCode() ^ System.identityHashCode(property);
  84. Object val = getValue();
  85. if (val != null)
  86. h ^= val.hashCode();
  87. return h;
  88. }
  89. public synchronized Object setWithSave(T newValue) {
  90. Object old;
  91. if (base != null) {
  92. if (value == INDIRECT_FLUIDS)
  93. return base.setWithSave(newValue);
  94. old = base;
  95. base = null;
  96. } else {
  97. old = value;
  98. }
  99. value = newValue;
  100. return old;
  101. }
  102. public synchronized void setRestore (Object oldValue)
  103. {
  104. if (value == INDIRECT_FLUIDS)
  105. base.setRestore(oldValue);
  106. else
  107. {
  108. if (oldValue instanceof Location)
  109. {
  110. value = null;
  111. base = (Location) oldValue;
  112. }
  113. else
  114. {
  115. value = oldValue;
  116. base = null;
  117. }
  118. }
  119. }
  120. }