View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.web.portlet;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import javax.portlet.PortletRequest;
28  import org.apache.commons.chain.web.MapEntry;
29  
30  
31  /***
32   * <p>Private implementation of <code>Map</code> for portlet parameter
33   * name-value.</p>
34   *
35   * @author Craig R. McClanahan
36   * @version $Revision: 412719 $ $Date: 2006-06-08 11:49:18 +0100 (Thu, 08 Jun 2006) $
37   */
38  
39  final class PortletParamMap implements Map {
40  
41  
42      public PortletParamMap(PortletRequest request) {
43          this.request = request;
44      }
45  
46  
47      private PortletRequest request = null;
48  
49  
50      public void clear() {
51          throw new UnsupportedOperationException();
52      }
53  
54  
55      public boolean containsKey(Object key) {
56          return (request.getParameter(key(key)) != null);
57      }
58  
59  
60      public boolean containsValue(Object value) {
61          Iterator values = values().iterator();
62          while (values.hasNext()) {
63              if (value.equals(values.next())) {
64                  return (true);
65              }
66          }
67          return (false);
68      }
69  
70  
71      public Set entrySet() {
72          Set set = new HashSet();
73          Enumeration keys = request.getParameterNames();
74          String key;
75          while (keys.hasMoreElements()) {
76              key = (String) keys.nextElement();
77              set.add(new MapEntry(key, request.getParameter(key), false));
78          }
79          return (set);
80      }
81  
82  
83      public boolean equals(Object o) {
84          return (request.equals(o));
85      }
86  
87  
88      public Object get(Object key) {
89          return (request.getParameter(key(key)));
90      }
91  
92  
93      public int hashCode() {
94          return (request.hashCode());
95      }
96  
97  
98      public boolean isEmpty() {
99          return (size() < 1);
100     }
101 
102 
103     public Set keySet() {
104         Set set = new HashSet();
105         Enumeration keys = request.getParameterNames();
106         while (keys.hasMoreElements()) {
107             set.add(keys.nextElement());
108         }
109         return (set);
110     }
111 
112 
113     public Object put(Object key, Object value) {
114         throw new UnsupportedOperationException();
115     }
116 
117 
118     public void putAll(Map map) {
119         throw new UnsupportedOperationException();
120     }
121 
122 
123     public Object remove(Object key) {
124         throw new UnsupportedOperationException();
125     }
126 
127 
128     public int size() {
129         int n = 0;
130         Enumeration keys = request.getParameterNames();
131         while (keys.hasMoreElements()) {
132             keys.nextElement();
133             n++;
134         }
135         return (n);
136     }
137 
138 
139     public Collection values() {
140         List list = new ArrayList();
141         Enumeration keys = request.getParameterNames();
142         while (keys.hasMoreElements()) {
143             list.add(request.getParameter((String) keys.nextElement()));
144         }
145         return (list);
146     }
147 
148 
149     private String key(Object key) {
150         if (key == null) {
151             throw new IllegalArgumentException();
152         } else if (key instanceof String) {
153             return ((String) key);
154         } else {
155             return (key.toString());
156         }
157     }
158 
159 
160 }