This project has retired. For details please refer to its
Attic page.
ServletParamValuesMap 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.servlet.context;
22
23 import org.apache.tiles.context.MapEntry;
24
25 import javax.servlet.ServletRequest;
26 import java.util.*;
27
28
29 /***
30 * <p>Private implementation of <code>Map</code> for servlet parameter
31 * name-values[].</p>
32 *
33 * @version $Rev: 581987 $ $Date: 2007-10-04 21:03:29 +0200 (Thu, 04 Oct 2007) $
34 */
35
36 final class ServletParamValuesMap implements Map<String, String[]> {
37
38
39 /***
40 * Constructor.
41 *
42 * @param request The request object to use.
43 */
44 public ServletParamValuesMap(ServletRequest request) {
45 this.request = request;
46 }
47
48
49 /***
50 * The request object to use.
51 */
52 private ServletRequest request = null;
53
54
55 /*** {@inheritDoc} */
56 public void clear() {
57 throw new UnsupportedOperationException();
58 }
59
60
61 /*** {@inheritDoc} */
62 public boolean containsKey(Object key) {
63 return (request.getParameter(key(key)) != null);
64 }
65
66
67 /*** {@inheritDoc} */
68 public boolean containsValue(Object value) {
69 if (!(value instanceof String[])) {
70 return (false);
71 }
72 String[] test = (String[]) value;
73 Iterator<String[]> values = values().iterator();
74 while (values.hasNext()) {
75 String[] actual = values.next();
76 if (test.length == actual.length) {
77 boolean matched = true;
78 for (int i = 0; i < test.length; i++) {
79 if (!test[i].equals(actual[i])) {
80 matched = false;
81 break;
82 }
83 }
84 if (matched) {
85 return (true);
86 }
87 }
88 }
89 return (false);
90 }
91
92
93 /*** {@inheritDoc} */
94 @SuppressWarnings("unchecked")
95 public Set<Map.Entry<String, String[]>> entrySet() {
96 Set<Map.Entry<String, String[]>> set = new HashSet<Map.Entry<String, String[]>>();
97 Enumeration<String> keys = request.getParameterNames();
98 String key;
99 while (keys.hasMoreElements()) {
100 key = keys.nextElement();
101 set.add(new MapEntry<String, String[]>(key, request
102 .getParameterValues(key), false));
103 }
104 return (set);
105 }
106
107
108 /*** {@inheritDoc} */
109 @SuppressWarnings("unchecked")
110 public boolean equals(Object o) {
111 ServletRequest otherRequest = ((ServletParamValuesMap) o).request;
112 boolean retValue = true;
113 synchronized (request) {
114 for (Enumeration<String> attribs = request.getParameterNames(); attribs
115 .hasMoreElements()
116 && retValue;) {
117 String parameterName = attribs.nextElement();
118 retValue = request.getParameterValues(parameterName).equals(
119 otherRequest.getParameterValues(parameterName));
120 }
121 }
122
123 return retValue;
124 }
125
126
127 /*** {@inheritDoc} */
128 public String[] get(Object key) {
129 return (request.getParameterValues(key(key)));
130 }
131
132
133 /*** {@inheritDoc} */
134 public int hashCode() {
135 return (request.hashCode());
136 }
137
138
139 /*** {@inheritDoc} */
140 public boolean isEmpty() {
141 return (size() < 1);
142 }
143
144
145 /*** {@inheritDoc} */
146 @SuppressWarnings("unchecked")
147 public Set<String> keySet() {
148 Set<String> set = new HashSet<String>();
149 Enumeration<String> keys = request.getParameterNames();
150 while (keys.hasMoreElements()) {
151 set.add(keys.nextElement());
152 }
153 return (set);
154 }
155
156
157 /*** {@inheritDoc} */
158 public String[] put(String key, String[] value) {
159 throw new UnsupportedOperationException();
160 }
161
162
163 /*** {@inheritDoc} */
164 public void putAll(Map<? extends String, ? extends String[]> map) {
165 throw new UnsupportedOperationException();
166 }
167
168
169 /*** {@inheritDoc} */
170 public String[] remove(Object key) {
171 throw new UnsupportedOperationException();
172 }
173
174
175 /*** {@inheritDoc} */
176 @SuppressWarnings("unchecked")
177 public int size() {
178 int n = 0;
179 Enumeration<String> keys = request.getParameterNames();
180 while (keys.hasMoreElements()) {
181 keys.nextElement();
182 n++;
183 }
184 return (n);
185 }
186
187
188 /*** {@inheritDoc} */
189 @SuppressWarnings("unchecked")
190 public Collection<String[]> values() {
191 List<String[]> list = new ArrayList<String[]>();
192 Enumeration<String> keys = request.getParameterNames();
193 while (keys.hasMoreElements()) {
194 list.add(request.getParameterValues(keys.nextElement()));
195 }
196 return (list);
197 }
198
199
200 /***
201 * Returns the string representation of the key.
202 *
203 * @param key The key.
204 * @return The string representation of the key.
205 * @throws IllegalArgumentException If the key is <code>null</code>.
206 */
207 private String key(Object key) {
208 if (key == null) {
209 throw new IllegalArgumentException();
210 } else if (key instanceof String) {
211 return ((String) key);
212 } else {
213 return (key.toString());
214 }
215 }
216
217
218 }