KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > portlet > PortletApplicationScopeMap


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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.portlet.PortletContext;
28
29
30 /**
31  * <p>Private implementation of <code>Map</code> for portlet context
32  * attributes.</p>
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.4 $ $Date: 2004/11/30 05:52:23 $
36  */

37
38 final class PortletApplicationScopeMap implements Map JavaDoc {
39
40
41     public PortletApplicationScopeMap(PortletContext context) {
42         this.context = context;
43     }
44
45
46     private PortletContext context = null;
47
48
49     public void clear() {
50         Iterator JavaDoc keys = keySet().iterator();
51         while (keys.hasNext()) {
52             context.removeAttribute((String JavaDoc) keys.next());
53         }
54     }
55
56
57     public boolean containsKey(Object JavaDoc key) {
58         return (context.getAttribute(key(key)) != null);
59     }
60
61
62     public boolean containsValue(Object JavaDoc value) {
63         if (value == null) {
64             return (false);
65         }
66         Enumeration JavaDoc keys = context.getAttributeNames();
67         while (keys.hasMoreElements()) {
68             Object JavaDoc next = context.getAttribute((String JavaDoc) keys.nextElement());
69             if (next == value) {
70                 return (true);
71             }
72         }
73         return (false);
74     }
75
76
77     public Set JavaDoc entrySet() {
78         Set JavaDoc set = new HashSet JavaDoc();
79         Enumeration JavaDoc keys = context.getAttributeNames();
80         while (keys.hasMoreElements()) {
81             set.add(context.getAttribute((String JavaDoc) keys.nextElement()));
82         }
83         return (set);
84     }
85
86
87     public boolean equals(Object JavaDoc o) {
88         return (context.equals(o));
89     }
90
91
92     public Object JavaDoc get(Object JavaDoc key) {
93         return (context.getAttribute(key(key)));
94     }
95
96
97     public int hashCode() {
98         return (context.hashCode());
99     }
100
101
102     public boolean isEmpty() {
103         return (size() < 1);
104     }
105
106
107     public Set JavaDoc keySet() {
108         Set JavaDoc set = new HashSet JavaDoc();
109         Enumeration JavaDoc keys = context.getAttributeNames();
110         while (keys.hasMoreElements()) {
111             set.add(keys.nextElement());
112         }
113         return (set);
114     }
115
116
117     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
118         if (value == null) {
119             return (remove(key));
120         }
121         String JavaDoc skey = key(key);
122         Object JavaDoc previous = context.getAttribute(skey);
123         context.setAttribute(skey, value);
124         return (previous);
125     }
126
127
128     public void putAll(Map JavaDoc map) {
129         Iterator JavaDoc keys = map.keySet().iterator();
130         while (keys.hasNext()) {
131             String JavaDoc key = (String JavaDoc) keys.next();
132             context.setAttribute(key, map.get(key));
133         }
134     }
135
136
137     public Object JavaDoc remove(Object JavaDoc key) {
138         String JavaDoc skey = key(key);
139         Object JavaDoc previous = context.getAttribute(skey);
140         context.removeAttribute(skey);
141         return (previous);
142     }
143
144
145     public int size() {
146         int n = 0;
147         Enumeration JavaDoc keys = context.getAttributeNames();
148         while (keys.hasMoreElements()) {
149             keys.nextElement();
150             n++;
151         }
152         return (n);
153     }
154
155
156     public Collection JavaDoc values() {
157         List JavaDoc list = new ArrayList JavaDoc();
158         Enumeration JavaDoc keys = context.getAttributeNames();
159         while (keys.hasMoreElements()) {
160             list.add(context.getAttribute((String JavaDoc) keys.nextElement()));
161         }
162         return (list);
163     }
164
165
166     private String JavaDoc key(Object JavaDoc key) {
167         if (key == null) {
168             throw new IllegalArgumentException JavaDoc();
169         } else if (key instanceof String JavaDoc) {
170             return ((String JavaDoc) key);
171         } else {
172             return (key.toString());
173         }
174     }
175
176
177 }
178
Popular Tags