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

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