1 4 package gnu.mapping; 5 6 7 8 public abstract class Location 9 { 10 14 15 public Location () 16 { 17 } 18 19 public Symbol getKeySymbol () 20 { 21 return null; 22 } 23 24 public Object getKeyProperty () 25 { 26 return null; 27 } 28 29 public String toString() 30 { 31 StringBuffer sbuf = new StringBuffer (); 32 sbuf.append(getClass().getName()); 33 Symbol sym = getKeySymbol(); 34 sbuf.append('['); 35 if (sym != null) 36 { 37 sbuf.append(sym); 38 Object property = getKeyProperty(); 39 if (property != null && property != this) 42 { 43 sbuf.append('/'); 44 sbuf.append(property); 45 } 46 } 47 51 sbuf.append("]"); 52 return sbuf.toString(); 53 } 54 55 56 public static final String UNBOUND = new String ("(unbound)"); 57 58 public abstract Object get (Object defaultValue); 59 60 62 public final Object get () 63 { 64 Object unb = Location.UNBOUND; 65 Object val = get(unb); 66 if (val == unb) 67 throw new UnboundLocationException(this); 68 return val; 69 } 70 71 public abstract void set (Object value); 72 73 public void undefine () 74 { 75 set(UNBOUND); 76 } 77 78 81 public Object setWithSave (Object newValue, CallContext ctx) 82 { 83 ctx.pushFluid(this); 84 Object old = get(UNBOUND); 85 set(newValue); 86 return old; 87 } 88 89 91 public void setRestore (Object oldValue, CallContext ctx) 92 { 93 set(oldValue); 95 ctx.popFluid(); 96 } 97 98 public boolean isBound () 99 { 100 Object 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 getValue () 115 { 116 return get(null); 117 } 118 119 public final Object setValue (Object newValue) 120 { 121 Object value = get(null); 122 set(newValue); 123 return value; 124 } 125 126 127 public boolean entered () 128 { 129 return false; 130 } 131 132 public void print(java.io.PrintWriter ps) 133 { 134 ps.print ("#<location "); 135 Symbol name = getKeySymbol(); 136 if (name != null) 137 ps.print(name); 138 Object unb = Location.UNBOUND; 139 Object 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 public static Location make (Object init, String name) 152 { 153 ThreadLocation loc = new ThreadLocation(name); 154 loc.setGlobal(init); 155 return loc; 156 } 157 158 public static IndirectableLocation make (String 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 |