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