KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > Location


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 is an abstract cell/location/variable with a value. */
7
8 public abstract class Location
9 {
10   /* DEBUGGING
11   static int counter;
12   public int id=++counter;
13   */

14
15   public Location ()
16   {
17   }
18
19   public Symbol getKeySymbol ()
20   {
21     return null;
22   }
23
24   public Object JavaDoc getKeyProperty ()
25   {
26     return null;
27   }
28
29   public String JavaDoc toString()
30   {
31     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
32     sbuf.append(getClass().getName());
33     Symbol sym = getKeySymbol();
34     sbuf.append('[');
35     if (sym != null)
36       {
37     sbuf.append(sym);
38     Object JavaDoc property = getKeyProperty();
39     // For a ThreadLocation the property defaults to "this".
40
// In that case we'd better not print the property ...
41
if (property != null && property != this)
42       {
43         sbuf.append('/');
44         sbuf.append(property);
45       }
46       }
47     /* DEBUGGING:
48     sbuf.append(" #:");
49     sbuf.append(id);
50     */

51     sbuf.append("]");
52     return sbuf.toString();
53   }
54
55   /** Magic value used to indicate there is no property binding. */
56   public static final String JavaDoc UNBOUND = new String JavaDoc("(unbound)");
57
58   public abstract Object JavaDoc get (Object JavaDoc defaultValue);
59
60   /** Get the current value of this location.
61    * @exception UnboundLocationException the location does not have a value. */

62   public final Object JavaDoc get ()
63   {
64     Object JavaDoc unb = Location.UNBOUND;
65     Object JavaDoc val = get(unb);
66     if (val == unb)
67       throw new UnboundLocationException(this);
68     return val;
69   }
70
71   public abstract void set (Object JavaDoc value);
72
73   public void undefine ()
74   {
75     set(UNBOUND);
76   }
77
78   /** Set a value, but return cookie so old value can be restored.
79    * This is intended for fluid-let where (in the case of multiple threads)
80    * a simple save-restore isn't always the right thing. */

81   public Object JavaDoc setWithSave (Object JavaDoc newValue, CallContext ctx)
82   {
83     ctx.pushFluid(this);
84     Object JavaDoc old = get(UNBOUND);
85     set(newValue);
86     return old;
87   }
88
89   /** Restore an old value.
90    * @param oldValue the return value from a prior setWithSave. */

91   public void setRestore (Object JavaDoc oldValue, CallContext ctx)
92   {
93     // if (oldValue == UNBOUND) ???; // FIXME
94
set(oldValue);
95     ctx.popFluid();
96   }
97
98   public boolean isBound ()
99   {
100     Object JavaDoc unb = Location.UNBOUND;
101     return get(unb) != unb;
102   }
103
104   public boolean isConstant ()
105   {
106     return false;
107   }
108
109   public Location getBase ()
110   {
111     return this;
112   }
113
114   public final Object JavaDoc getValue ()
115   {
116     return get(null);
117   }
118
119   public final Object JavaDoc setValue (Object JavaDoc newValue)
120   {
121     Object JavaDoc value = get(null);
122     set(newValue);
123     return value;
124   }
125
126   /** True if directly entered in an Environment. (Only if NamedLocation.) */
127   public boolean entered ()
128   {
129     return false;
130   }
131
132   public void print(java.io.PrintWriter JavaDoc ps)
133   {
134     ps.print ("#<location ");
135     Symbol name = getKeySymbol();
136     if (name != null)
137       ps.print(name);
138     Object JavaDoc unb = Location.UNBOUND;
139     Object JavaDoc value = get(unb);
140     if (value != unb)
141       {
142     ps.print(" -> ");
143     ps.print(value);
144       }
145     else
146       ps.print("(unbound)");
147     ps.print ('>');
148   }
149
150   // The compiler emits calls to this method.
151
public static Location make (Object JavaDoc init, String JavaDoc name)
152   {
153     ThreadLocation loc = new ThreadLocation(name);
154     loc.setGlobal(init);
155     return loc;
156   }
157
158   // The compiler emits calls to this method.
159
public static IndirectableLocation make (String JavaDoc name)
160   {
161     Symbol sym = Namespace.EmptyNamespace.getSymbol(name.intern());
162     PlainLocation loc = new PlainLocation(sym, null);
163     loc.base = null;
164     loc.value = UNBOUND;
165     return loc;
166   }
167
168   public static IndirectableLocation make (Symbol name)
169   {
170     PlainLocation loc = new PlainLocation(name, null);
171     loc.base = null;
172     loc.value = UNBOUND;
173     return loc;
174   }
175 }
176
Popular Tags