This project has retired. For details please refer to its
Attic page.
ReadOnlyEnumerationMap xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.request.collection;
22
23 import static org.apache.tiles.request.collection.CollectionUtil.*;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import org.apache.tiles.request.attribute.HasKeys;
35
36
37
38
39
40
41
42 public class ReadOnlyEnumerationMap<V> implements Map<String, V> {
43
44
45
46
47 protected HasKeys<V> request;
48
49
50
51
52
53
54 public ReadOnlyEnumerationMap(HasKeys<V> request) {
55 this.request = request;
56 }
57
58
59 public void clear() {
60 throw new UnsupportedOperationException();
61 }
62
63
64
65 public boolean containsKey(Object key) {
66 return (request.getValue(key(key)) != null);
67 }
68
69
70
71 @SuppressWarnings("unchecked")
72 public boolean containsValue(Object value) {
73 V realValue = (V) value;
74 for (Enumeration<String> keysIt = request.getKeys(); keysIt.hasMoreElements();) {
75 if (realValue.equals(request.getValue(keysIt.nextElement()))) {
76 return true;
77 }
78 }
79 return false;
80 }
81
82
83
84 public Set<Map.Entry<String, V>> entrySet() {
85 return new ReadOnlyEnumerationMapEntrySet();
86 }
87
88
89
90 public V get(Object key) {
91 return (request.getValue(key(key)));
92 }
93
94
95
96 public boolean isEmpty() {
97 return !request.getKeys().hasMoreElements();
98 }
99
100
101
102 public Set<String> keySet() {
103 return new KeySet(request);
104 }
105
106
107
108 public V put(String key, V value) {
109 throw new UnsupportedOperationException();
110 }
111
112
113
114 public void putAll(Map<? extends String, ? extends V> map) {
115 throw new UnsupportedOperationException();
116 }
117
118
119
120 public V remove(Object key) {
121 throw new UnsupportedOperationException();
122 }
123
124
125
126 public int size() {
127 return enumerationSize(request.getKeys());
128 }
129
130
131
132 public Collection<V> values() {
133 return new ReadOnlyEnumerationMapValuesCollection();
134 }
135
136
137
138 @SuppressWarnings("unchecked")
139 @Override
140 public boolean equals(Object o) {
141 HasKeys<V> otherRequest = ((ReadOnlyEnumerationMap<V>) o).request;
142 boolean retValue = true;
143 Set<String> otherKeys = new HashSet<String>();
144 for (Enumeration<String> attribs = otherRequest.getKeys(); attribs
145 .hasMoreElements();) {
146 otherKeys.add(attribs.nextElement());
147 }
148 for (Enumeration<String> attribs = request.getKeys(); attribs
149 .hasMoreElements()
150 && retValue;) {
151 String parameterName = attribs.nextElement();
152 retValue = request.getValue(parameterName).equals(
153 otherRequest.getValue(parameterName));
154 otherKeys.remove(parameterName);
155 }
156
157 return retValue && otherKeys.isEmpty();
158 }
159
160
161 @Override
162 public int hashCode() {
163 int retValue = 0;
164 for (Enumeration<String> attribs = request.getKeys(); attribs
165 .hasMoreElements();) {
166 String parameterName = attribs.nextElement();
167 V value = request.getValue(parameterName);
168 retValue += parameterName.hashCode() ^ (value == null ? 0 : value.hashCode());
169 }
170 return retValue;
171 }
172
173
174
175
176 class ReadOnlyEnumerationMapEntrySet implements Set<Map.Entry<String, V>> {
177
178 @Override
179 public boolean add(java.util.Map.Entry<String, V> e) {
180 throw new UnsupportedOperationException();
181 }
182
183 @Override
184 public boolean addAll(
185 Collection<? extends java.util.Map.Entry<String, V>> c) {
186 throw new UnsupportedOperationException();
187 }
188
189 @Override
190 public void clear() {
191 throw new UnsupportedOperationException();
192 }
193
194 @SuppressWarnings("unchecked")
195 @Override
196 public boolean contains(Object o) {
197 return containsEntry((java.util.Map.Entry<String, V>) o);
198 }
199
200 @SuppressWarnings("unchecked")
201 @Override
202 public boolean containsAll(Collection<?> c) {
203 Collection<Map.Entry<String, V>> realCollection =
204 (Collection<Map.Entry<String, V>>) c;
205 for (Map.Entry<String, V> entry : realCollection) {
206 if (!containsEntry(entry)) {
207 return false;
208 }
209 }
210 return true;
211 }
212
213 @Override
214 public boolean isEmpty() {
215 return ReadOnlyEnumerationMap.this.isEmpty();
216 }
217
218 @Override
219 public Iterator<java.util.Map.Entry<String, V>> iterator() {
220 return new ReadOnlyEnumerationMapEntrySetIterator();
221 }
222
223 @Override
224 public boolean remove(Object o) {
225 throw new UnsupportedOperationException();
226 }
227
228 @Override
229 public boolean removeAll(Collection<?> c) {
230 throw new UnsupportedOperationException();
231 }
232
233 @Override
234 public boolean retainAll(Collection<?> c) {
235 throw new UnsupportedOperationException();
236 }
237
238 @Override
239 public int size() {
240 return ReadOnlyEnumerationMap.this.size();
241 }
242
243 @Override
244 public Object[] toArray() {
245 return toList().toArray();
246 }
247
248 @Override
249 public <T> T[] toArray(T[] a) {
250 return toList().toArray(a);
251 }
252
253
254
255
256
257
258
259 protected boolean containsEntry(Map.Entry<String, V> entry) {
260 V storedValue = request.getValue(key(entry.getKey()));
261 return storedValue != null && storedValue.equals(entry.getValue());
262 }
263
264
265
266
267
268
269 private List<Map.Entry<String, V>> toList() {
270 List<Map.Entry<String, V>> entries = new ArrayList<Map.Entry<String, V>>();
271 Enumeration<String> names = request.getKeys();
272 while (names.hasMoreElements()) {
273 entries.add(extractNextEntry(names));
274 }
275 return entries;
276 }
277
278
279
280
281
282
283
284 private MapEntry<String, V> extractNextEntry(
285 Enumeration<String> names) {
286 String name = names.nextElement();
287 return new MapEntry<String, V>(name, request.getValue(name),
288 false);
289 }
290
291
292
293
294 private class ReadOnlyEnumerationMapEntrySetIterator implements Iterator<Map.Entry<String, V>> {
295
296
297
298
299 private Enumeration<String> namesEnumeration = request.getKeys();
300
301 @Override
302 public boolean hasNext() {
303 return namesEnumeration.hasMoreElements();
304 }
305
306 @Override
307 public java.util.Map.Entry<String, V> next() {
308 return extractNextEntry(namesEnumeration);
309 }
310
311 @Override
312 public void remove() {
313 throw new UnsupportedOperationException();
314 }
315
316 }
317 }
318
319
320
321
322 private class ReadOnlyEnumerationMapValuesCollection implements Collection<V> {
323
324 @Override
325 public boolean add(V e) {
326 throw new UnsupportedOperationException();
327 }
328
329 @Override
330 public boolean addAll(Collection<? extends V> c) {
331 throw new UnsupportedOperationException();
332 }
333
334 @Override
335 public void clear() {
336 throw new UnsupportedOperationException();
337 }
338
339 @Override
340 public boolean contains(Object o) {
341 return containsValue(o);
342 }
343
344 @SuppressWarnings("unchecked")
345 @Override
346 public boolean containsAll(Collection<?> c) {
347 Collection<String> realCollection = (Collection<String>) c;
348 List<String> valueList = new ArrayList<String>(realCollection);
349 for (Enumeration<String> keysEnum = request.getKeys(); keysEnum.hasMoreElements();) {
350 valueList.remove(request.getValue(keysEnum.nextElement()));
351 if (valueList.isEmpty()) {
352 return true;
353 }
354 }
355 return false;
356 }
357
358 @Override
359 public boolean isEmpty() {
360 return ReadOnlyEnumerationMap.this.isEmpty();
361 }
362
363 @Override
364 public Iterator<V> iterator() {
365 return new ReadOnlyEnumerationMapValuesCollectionIterator();
366 }
367
368 @Override
369 public boolean remove(Object o) {
370 throw new UnsupportedOperationException();
371 }
372
373 @Override
374 public boolean removeAll(Collection<?> c) {
375 throw new UnsupportedOperationException();
376 }
377
378 @Override
379 public boolean retainAll(Collection<?> c) {
380 throw new UnsupportedOperationException();
381 }
382
383 @Override
384 public int size() {
385 return ReadOnlyEnumerationMap.this.size();
386 }
387
388 @Override
389 public Object[] toArray() {
390 return toList().toArray();
391 }
392
393 @Override
394 public <T> T[] toArray(T[] a) {
395 return toList().toArray(a);
396 }
397
398
399
400
401
402
403 private List<V> toList() {
404 List<V> entries = new ArrayList<V>();
405 Enumeration<String> names = request.getKeys();
406 while (names.hasMoreElements()) {
407 entries.add(request.getValue(names.nextElement()));
408 }
409 return entries;
410 }
411
412
413
414
415 private class ReadOnlyEnumerationMapValuesCollectionIterator implements Iterator<V> {
416
417
418
419
420 private Enumeration<String> namesEnumeration = request.getKeys();
421
422 @Override
423 public boolean hasNext() {
424 return namesEnumeration.hasMoreElements();
425 }
426
427 @Override
428 public V next() {
429 return request.getValue(namesEnumeration.nextElement());
430 }
431
432 @Override
433 public void remove() {
434 throw new UnsupportedOperationException();
435 }
436 }
437 }
438 }