KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > NamedLocation


1 // Copyright (c) 2004 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.mapping;
5
6 /** A Location that can be used as an entry in an Environment. */
7
8 public abstract class NamedLocation extends IndirectableLocation
9   implements
10   /* #ifdef JAVA2 */
11   java.util.Map.Entry /* <EnvironmentKey, Object> */,
12   /* #endif */
13   EnvironmentKey
14 {
15   NamedLocation next;
16
17   public boolean entered ()
18   {
19     return next != null;
20   }
21
22   public Environment getEnvironment ()
23   {
24     for (NamedLocation loc = this; loc != null; loc = loc.next)
25       {
26     if (loc.name == null)
27       {
28         Environment env = (Environment) loc.value;
29         if (env != null)
30           return env;
31       }
32       }
33     return super.getEnvironment();
34   }
35
36   final Symbol name;
37   final Object JavaDoc property;
38
39   public NamedLocation (NamedLocation loc)
40   {
41     name = loc.name;
42     property = loc.property;
43   }
44
45   public NamedLocation (Symbol name, Object JavaDoc property)
46   {
47     this.name = name;
48     this.property = property;
49   }
50
51   public final Symbol getKeySymbol ()
52   {
53     return name;
54   }
55
56   public final Object JavaDoc getKeyProperty ()
57   {
58     return property;
59   }
60
61   public final boolean matches (EnvironmentKey key)
62   {
63     return Symbol.equals(key.getKeySymbol(), this.name)
64       && key.getKeyProperty() == this.property;
65   }
66
67   public final boolean matches (Symbol symbol, Object JavaDoc property)
68   {
69     return Symbol.equals(symbol, this.name) && property == this.property;
70   }
71
72   public final Object JavaDoc /*<EnvironmentKey>*/ getKey ()
73   {
74     if (property == null)
75       return name;
76     else
77       return this;
78   }
79
80   public boolean equals (Object JavaDoc x)
81   {
82     if (! (x instanceof NamedLocation))
83       return false;
84     NamedLocation e2 = (NamedLocation) x;
85     if (name == null ? e2.name != null : ! name.equals(e2.name))
86       return false;
87     if (property != e2.property)
88       return false;
89     Object JavaDoc val1 = getValue();
90     Object JavaDoc val2 = e2.getValue();
91     if (val1 == val2)
92       return true;
93     if (val1 == null || val2 == null)
94       return false;
95     return val1.equals(val2);
96   }
97
98   public int hashCode ()
99   {
100     int h = name.hashCode() ^ System.identityHashCode(property);
101     Object JavaDoc val = getValue();
102     if (val != null)
103       h ^= val.hashCode();
104     return h;
105   }
106
107   public synchronized Object JavaDoc setWithSave (Object JavaDoc newValue, CallContext ctx)
108   {
109     Object JavaDoc old;
110     if (base != null)
111       {
112     if (value == INDIRECT_FLUIDS)
113           return base.setWithSave(newValue, ctx);
114     old = base;
115     base = null;
116       }
117     else
118       {
119     old = value;
120       }
121     value = newValue;
122     ctx.pushFluid(this);
123     return old;
124   }
125
126   public synchronized void setRestore (Object JavaDoc oldValue, CallContext ctx)
127   {
128     if (value == INDIRECT_FLUIDS)
129       base.setRestore(oldValue, ctx);
130     else
131       {
132         if (oldValue instanceof Location)
133           {
134             value = null;
135             base = (Location) oldValue;
136           }
137         else
138           {
139             value = oldValue;
140             base = null;
141           }
142         ctx.popFluid();
143       }
144   }
145 }
146
Popular Tags