1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.context; |
17 | |
|
18 | |
import java.util.AbstractCollection; |
19 | |
import java.util.AbstractMap; |
20 | |
import java.util.AbstractSet; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Enumeration; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.seasar.framework.util.EnumerationIterator; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public abstract class AbstractExternalContextMap extends AbstractMap { |
35 | |
|
36 | |
|
37 | |
private Set entrySet_; |
38 | |
|
39 | |
private Set keySet_; |
40 | |
|
41 | |
private Collection values_; |
42 | |
|
43 | 66 | public AbstractExternalContextMap() { |
44 | 66 | } |
45 | |
|
46 | |
public void clear() { |
47 | |
|
48 | 1 | List list = new ArrayList(); |
49 | 1 | for (Enumeration e = getAttributeNames(); e.hasMoreElements();) { |
50 | 2 | String key = (String) e.nextElement(); |
51 | 2 | list.add(key); |
52 | |
} |
53 | 1 | clearReally(list); |
54 | 1 | } |
55 | |
|
56 | |
private void clearReally(List keys) { |
57 | 1 | for (Iterator itr = keys.iterator(); itr.hasNext();) { |
58 | 2 | String key = (String) itr.next(); |
59 | 2 | removeAttribute(key); |
60 | |
} |
61 | 1 | } |
62 | |
|
63 | |
public boolean containsKey(Object key) { |
64 | 40 | return (getAttribute(key.toString()) != null); |
65 | |
} |
66 | |
|
67 | |
public boolean containsValue(Object value) { |
68 | 2 | if (value != null) { |
69 | 2 | for (Enumeration e = getAttributeNames(); e.hasMoreElements();) { |
70 | 5 | String key = (String) e.nextElement(); |
71 | 5 | Object attributeValue = getAttribute(key); |
72 | 5 | if (value.equals(attributeValue)) { |
73 | 1 | return true; |
74 | |
} |
75 | |
} |
76 | |
} |
77 | 1 | return false; |
78 | |
} |
79 | |
|
80 | |
public Set entrySet() { |
81 | 1 | if (entrySet_ == null) { |
82 | 1 | entrySet_ = new EntrySet(this); |
83 | |
} |
84 | 1 | return entrySet_; |
85 | |
} |
86 | |
|
87 | |
public Object get(Object key) { |
88 | 64 | return getAttribute(key.toString()); |
89 | |
} |
90 | |
|
91 | |
public Object put(Object key, Object value) { |
92 | 41 | String keyStr = key.toString(); |
93 | 41 | Object o = getAttribute(keyStr); |
94 | 41 | setAttribute(keyStr, value); |
95 | 41 | return o; |
96 | |
} |
97 | |
|
98 | |
public void putAll(Map map) { |
99 | 1 | for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) { |
100 | 2 | Map.Entry entry = (Map.Entry) itr.next(); |
101 | 2 | String key = (String) entry.getKey(); |
102 | 2 | setAttribute(key, entry.getValue()); |
103 | |
} |
104 | 1 | } |
105 | |
|
106 | |
public boolean isEmpty() { |
107 | 6 | return !getAttributeNames().hasMoreElements(); |
108 | |
} |
109 | |
|
110 | |
public Set keySet() { |
111 | 1 | if (keySet_ == null) { |
112 | 1 | keySet_ = new KeySet(this); |
113 | |
} |
114 | 1 | return keySet_; |
115 | |
} |
116 | |
|
117 | |
public Object remove(Object key) { |
118 | 1 | String keyStr = key.toString(); |
119 | 1 | Object o = getAttribute(keyStr); |
120 | 1 | removeAttribute(keyStr); |
121 | 1 | return o; |
122 | |
} |
123 | |
|
124 | |
public Collection values() { |
125 | 1 | if (values_ == null) { |
126 | 1 | values_ = new ValuesCollection(this); |
127 | |
} |
128 | 1 | return values_; |
129 | |
} |
130 | |
|
131 | |
protected abstract Object getAttribute(String key); |
132 | |
|
133 | |
protected abstract void setAttribute(String key, Object value); |
134 | |
|
135 | |
protected abstract Enumeration getAttributeNames(); |
136 | |
|
137 | |
protected abstract void removeAttribute(String key); |
138 | |
|
139 | 2 | abstract class AbstractExternalContextSet extends AbstractSet { |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public int size() { |
145 | 0 | int size = 0; |
146 | 0 | for (Iterator itr = iterator(); itr.hasNext(); size++) { |
147 | 0 | itr.next(); |
148 | |
} |
149 | 0 | return size; |
150 | |
} |
151 | |
|
152 | |
} |
153 | |
|
154 | |
class EntrySet extends AbstractExternalContextSet { |
155 | |
|
156 | |
private AbstractExternalContextMap contextMap_; |
157 | |
|
158 | 1 | public EntrySet(AbstractExternalContextMap contextMap) { |
159 | 1 | contextMap_ = contextMap; |
160 | 1 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public Iterator iterator() { |
166 | 1 | return new EntryIterator(contextMap_); |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public boolean remove(Object o) { |
173 | 0 | if (!(o instanceof Map.Entry)) { |
174 | 0 | return false; |
175 | |
} |
176 | 0 | Map.Entry entry = (Map.Entry) o; |
177 | 0 | Object returnObj = contextMap_.remove(entry.getKey()); |
178 | 0 | return (returnObj != null); |
179 | |
} |
180 | |
} |
181 | |
|
182 | |
class KeySet extends AbstractExternalContextSet { |
183 | |
|
184 | |
private AbstractExternalContextMap contextMap_; |
185 | |
|
186 | 1 | public KeySet(AbstractExternalContextMap contextMap) { |
187 | 1 | contextMap_ = contextMap; |
188 | 1 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public Iterator iterator() { |
194 | 1 | return new KeyIterator(contextMap_); |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public boolean remove(Object o) { |
201 | 0 | if (!(o instanceof String)) { |
202 | 0 | return false; |
203 | |
} |
204 | 0 | String s = (String) o; |
205 | 0 | Object returnObj = contextMap_.remove(s); |
206 | 0 | return (returnObj != null); |
207 | |
} |
208 | |
} |
209 | |
|
210 | |
class ValuesCollection extends AbstractCollection { |
211 | |
|
212 | |
private AbstractExternalContextMap contextMap_; |
213 | |
|
214 | 1 | public ValuesCollection(AbstractExternalContextMap contextMap) { |
215 | 1 | contextMap_ = contextMap; |
216 | 1 | } |
217 | |
|
218 | |
public int size() { |
219 | 1 | int size = 0; |
220 | 4 | for (Iterator itr = iterator(); itr.hasNext(); size++) { |
221 | 3 | itr.next(); |
222 | |
} |
223 | 1 | return size; |
224 | |
} |
225 | |
|
226 | |
public Iterator iterator() { |
227 | 4 | return new ValuesIterator(contextMap_); |
228 | |
} |
229 | |
|
230 | |
} |
231 | |
|
232 | |
abstract class AbstractExternalContextIterator extends EnumerationIterator { |
233 | |
|
234 | |
private final AbstractExternalContextMap contextMap_; |
235 | |
|
236 | |
private String currentKey_; |
237 | |
|
238 | 6 | private boolean removeCalled_ = false; |
239 | |
|
240 | |
public AbstractExternalContextIterator( |
241 | 6 | final AbstractExternalContextMap contextMap) { |
242 | 6 | super(contextMap.getAttributeNames()); |
243 | 6 | contextMap_ = contextMap; |
244 | 6 | } |
245 | |
|
246 | |
public Object next() { |
247 | 13 | currentKey_ = (String) super.next(); |
248 | |
try { |
249 | 13 | return doNext(); |
250 | |
} finally { |
251 | 13 | removeCalled_ = false; |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
public void remove() { |
256 | 0 | if (currentKey_ != null && !removeCalled_) { |
257 | 0 | doRemove(); |
258 | 0 | removeCalled_ = true; |
259 | |
} else { |
260 | 0 | throw new IllegalStateException(); |
261 | |
} |
262 | 0 | } |
263 | |
|
264 | |
protected String getCurrentKey() { |
265 | 13 | return currentKey_; |
266 | |
} |
267 | |
|
268 | |
protected Object getValueFromMap(String key) { |
269 | 11 | return contextMap_.get(key); |
270 | |
} |
271 | |
|
272 | |
protected void removeKeyFromMap(String key) { |
273 | 0 | contextMap_.remove(key); |
274 | 0 | } |
275 | |
|
276 | |
protected void removeValueFromMap(Object value) { |
277 | 0 | if (containsValue(value)) { |
278 | 0 | for (Iterator itr = entrySet().iterator(); itr.hasNext();) { |
279 | 0 | Map.Entry e = (Map.Entry) itr.next(); |
280 | 0 | if (value.equals(e.getValue())) { |
281 | 0 | contextMap_.remove(e.getKey()); |
282 | |
} |
283 | |
} |
284 | |
} |
285 | |
|
286 | 0 | } |
287 | |
|
288 | |
protected abstract Object doNext(); |
289 | |
|
290 | |
protected abstract void doRemove(); |
291 | |
} |
292 | |
|
293 | |
class EntryIterator extends AbstractExternalContextIterator { |
294 | |
|
295 | 1 | public EntryIterator(AbstractExternalContextMap contextMap) { |
296 | 1 | super(contextMap); |
297 | 1 | } |
298 | |
|
299 | |
protected Object doNext() { |
300 | 2 | String key = getCurrentKey(); |
301 | 2 | return new ImmutableEntry(key, getValueFromMap(key)); |
302 | |
} |
303 | |
|
304 | |
protected void doRemove() { |
305 | 0 | String key = getCurrentKey(); |
306 | 0 | removeKeyFromMap(key); |
307 | 0 | } |
308 | |
|
309 | |
} |
310 | |
|
311 | |
class KeyIterator extends AbstractExternalContextIterator { |
312 | |
|
313 | 1 | public KeyIterator(AbstractExternalContextMap contextMap) { |
314 | 1 | super(contextMap); |
315 | 1 | } |
316 | |
|
317 | |
protected Object doNext() { |
318 | 2 | return getCurrentKey(); |
319 | |
} |
320 | |
|
321 | |
protected void doRemove() { |
322 | 0 | removeKeyFromMap(getCurrentKey()); |
323 | 0 | } |
324 | |
} |
325 | |
|
326 | |
class ValuesIterator extends AbstractExternalContextIterator { |
327 | |
|
328 | 4 | public ValuesIterator(AbstractExternalContextMap contextMap) { |
329 | 4 | super(contextMap); |
330 | 4 | } |
331 | |
|
332 | |
protected Object doNext() { |
333 | 9 | String key = getCurrentKey(); |
334 | 9 | return getValueFromMap(key); |
335 | |
} |
336 | |
|
337 | |
protected void doRemove() { |
338 | 0 | String key = getCurrentKey(); |
339 | 0 | Object value = getValueFromMap(key); |
340 | 0 | removeValueFromMap(value); |
341 | 0 | } |
342 | |
|
343 | |
} |
344 | |
|
345 | |
protected static class ImmutableEntry implements Map.Entry { |
346 | |
|
347 | |
private final Object key_; |
348 | |
|
349 | |
private final Object value_; |
350 | |
|
351 | 2 | public ImmutableEntry(Object key, Object value) { |
352 | 2 | key_ = key; |
353 | 2 | value_ = value; |
354 | 2 | } |
355 | |
|
356 | |
public Object getKey() { |
357 | 0 | return key_; |
358 | |
} |
359 | |
|
360 | |
public Object getValue() { |
361 | 0 | return value_; |
362 | |
} |
363 | |
|
364 | |
public Object setValue(Object arg0) { |
365 | 0 | throw new UnsupportedOperationException("Immutable entry."); |
366 | |
} |
367 | |
|
368 | |
public boolean equals(Object obj) { |
369 | 0 | if (obj == null || !(obj instanceof ImmutableEntry)) { |
370 | 0 | return false; |
371 | |
} |
372 | 0 | ImmutableEntry entry = (ImmutableEntry) obj; |
373 | 0 | Object key = entry.getKey(); |
374 | 0 | Object value = entry.getValue(); |
375 | |
|
376 | 0 | return (key == key_ || (key != null && key.equals(key_))) |
377 | |
&& (value == value_ || (value != null && value |
378 | |
.equals(value_))); |
379 | |
} |
380 | |
|
381 | |
public int hashCode() { |
382 | 0 | return ((key_ != null) ? key_.hashCode() : 0) |
383 | |
^ ((value_ != null) ? value_.hashCode() : 0); |
384 | |
} |
385 | |
} |
386 | |
|
387 | |
} |