114 /** |
114 /** |
115 * The underlying map storing the data. |
115 * The underlying map storing the data. |
116 * We maintain the invariant that this table contains only |
116 * We maintain the invariant that this table contains only |
117 * mappings of the form |
117 * mappings of the form |
118 * Key<T> -> T or Key<T> -> Factory<T> */ |
118 * Key<T> -> T or Key<T> -> Factory<T> */ |
119 private Map<Key,Object> ht = new HashMap<Key,Object>(); |
119 private Map<Key<?>,Object> ht = new HashMap<Key<?>,Object>(); |
120 |
120 |
121 /** Set the factory for the key in this context. */ |
121 /** Set the factory for the key in this context. */ |
122 public <T> void put(Key<T> key, Factory<T> fac) { |
122 public <T> void put(Key<T> key, Factory<T> fac) { |
123 checkState(ht); |
123 checkState(ht); |
124 Object old = ht.put(key, fac); |
124 Object old = ht.put(key, fac); |
126 throw new AssertionError("duplicate context value"); |
126 throw new AssertionError("duplicate context value"); |
127 } |
127 } |
128 |
128 |
129 /** Set the value for the key in this context. */ |
129 /** Set the value for the key in this context. */ |
130 public <T> void put(Key<T> key, T data) { |
130 public <T> void put(Key<T> key, T data) { |
131 if (data instanceof Factory) |
131 if (data instanceof Factory<?>) |
132 throw new AssertionError("T extends Context.Factory"); |
132 throw new AssertionError("T extends Context.Factory"); |
133 checkState(ht); |
133 checkState(ht); |
134 Object old = ht.put(key, data); |
134 Object old = ht.put(key, data); |
135 if (old != null && !(old instanceof Factory) && old != data && data != null) |
135 if (old != null && !(old instanceof Factory<?>) && old != data && data != null) |
136 throw new AssertionError("duplicate context value"); |
136 throw new AssertionError("duplicate context value"); |
137 } |
137 } |
138 |
138 |
139 /** Get the value for the key in this context. */ |
139 /** Get the value for the key in this context. */ |
140 public <T> T get(Key<T> key) { |
140 public <T> T get(Key<T> key) { |
141 checkState(ht); |
141 checkState(ht); |
142 Object o = ht.get(key); |
142 Object o = ht.get(key); |
143 if (o instanceof Factory) { |
143 if (o instanceof Factory<?>) { |
144 Factory fac = (Factory)o; |
144 Factory<?> fac = (Factory<?>)o; |
145 o = fac.make(); |
145 o = fac.make(); |
146 if (o instanceof Factory) |
146 if (o instanceof Factory<?>) |
147 throw new AssertionError("T extends Context.Factory"); |
147 throw new AssertionError("T extends Context.Factory"); |
148 assert ht.get(key) == o; |
148 assert ht.get(key) == o; |
149 } |
149 } |
150 |
150 |
151 /* The following cast can't fail unless there was |
151 /* The following cast can't fail unless there was |