This project has retired. For details please refer to its Attic page.
ReadOnlyEnumerationMap xref
View Javadoc

1   /*
2    * $Id: ReadOnlyEnumerationMap.java 1306435 2012-03-28 15:39:11Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Wraps an {@link HasKeys} object into a read-only map.
38   *
39   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
40   * @param <V> The type of the values.
41   */
42  public class ReadOnlyEnumerationMap<V> implements Map<String, V> {
43  
44      /**
45       * The request.
46       */
47      protected HasKeys<V> request;
48  
49      /**
50       * Constructor.
51       *
52       * @param request The request object to use.
53       */
54      public ReadOnlyEnumerationMap(HasKeys<V> request) {
55          this.request = request;
56      }
57  
58      /** {@inheritDoc} */
59      public void clear() {
60          throw new UnsupportedOperationException();
61      }
62  
63  
64      /** {@inheritDoc} */
65      public boolean containsKey(Object key) {
66          return (request.getValue(key(key)) != null);
67      }
68  
69  
70      /** {@inheritDoc} */
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      /** {@inheritDoc} */
84      public Set<Map.Entry<String, V>> entrySet() {
85          return new ReadOnlyEnumerationMapEntrySet();
86      }
87  
88  
89      /** {@inheritDoc} */
90      public V get(Object key) {
91          return (request.getValue(key(key)));
92      }
93  
94  
95      /** {@inheritDoc} */
96      public boolean isEmpty() {
97          return !request.getKeys().hasMoreElements();
98      }
99  
100 
101     /** {@inheritDoc} */
102     public Set<String> keySet() {
103         return new KeySet(request);
104     }
105 
106 
107     /** {@inheritDoc} */
108     public V put(String key, V value) {
109         throw new UnsupportedOperationException();
110     }
111 
112 
113     /** {@inheritDoc} */
114     public void putAll(Map<? extends String, ? extends V> map) {
115         throw new UnsupportedOperationException();
116     }
117 
118 
119     /** {@inheritDoc} */
120     public V remove(Object key) {
121         throw new UnsupportedOperationException();
122     }
123 
124 
125     /** {@inheritDoc} */
126     public int size() {
127         return enumerationSize(request.getKeys());
128     }
129 
130 
131     /** {@inheritDoc} */
132     public Collection<V> values() {
133         return new ReadOnlyEnumerationMapValuesCollection();
134     }
135 
136 
137     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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      * Entry set implementation for {@link ReadOnlyEnumerationMap}.
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          * Checks whether the entry is present.
255          *
256          * @param entry The entry to check.
257          * @return <code>true</code> if the entry is present.
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          * Turns this set into a list.
266          *
267          * @return The list.
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          * Returns the next entry, given the enumeration.
280          *
281          * @param names The enumeration to get the next key from.
282          * @return The next entry.
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          * Iterates entries of {@link ReadOnlyEnumerationMap}.
293          */
294         private class ReadOnlyEnumerationMapEntrySetIterator implements Iterator<Map.Entry<String, V>> {
295 
296             /**
297              * Enumerates keys.
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      * Values collection for {@link ReadOnlyEnumerationMap}.
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          * Turns this collection into a list.
400          *
401          * @return The list.
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          * Iterates values of {@link ReadOnlyEnumerationMap}.
414          */
415         private class ReadOnlyEnumerationMapValuesCollectionIterator implements Iterator<V> {
416 
417             /**
418              * Enumerates attribute keys.
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 }