KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > servlet > AbstractAttributeMap


1 /*
2  * Copyright 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.myfaces.context.servlet;
17
18 import java.util.AbstractSet JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.Set JavaDoc;
27
28
29 /**
30  * Helper Map implementation for use with different Attribute Maps.
31  *
32  * @author Anton Koinov (latest modification by $Author: matze $)
33  * @version $Revision: 1.9 $ $Date: 2004/10/13 11:51:00 $
34  *
35  * $Log: AbstractAttributeMap.java,v $
36  * Revision 1.9 2004/10/13 11:51:00 matze
37  * renamed packages to org.apache
38  *
39  * Revision 1.8 2004/07/03 18:29:18 dave0000
40  * Remove redundant code, small fixes.
41  *
42  * Revision 1.7 2004/07/01 22:05:04 mwessendorf
43  * ASF switch
44  *
45  * Revision 1.6 2004/03/30 05:34:56 dave0000
46  * change entrySet() to not use HashMap and avoid copying data
47  *
48  */

49 public abstract class AbstractAttributeMap
50     implements Map JavaDoc
51 {
52     private Set JavaDoc _keySet;
53     private Collection JavaDoc _values;
54     private Set JavaDoc _entrySet;
55
56     public void clear()
57     {
58         List JavaDoc names = new ArrayList JavaDoc();
59         for (Enumeration JavaDoc e = getAttributeNames(); e.hasMoreElements();)
60         {
61             names.add(e.nextElement());
62         }
63
64         for (Iterator JavaDoc it = names.iterator(); it.hasNext();)
65         {
66             removeAttribute((String JavaDoc) it.next());
67         }
68     }
69
70     public boolean containsKey(Object JavaDoc key)
71     {
72         return getAttribute(key.toString()) != null;
73     }
74
75     public boolean containsValue(Object JavaDoc findValue)
76     {
77         if (findValue == null)
78         {
79             return false;
80         }
81
82         for (Enumeration JavaDoc e = getAttributeNames(); e.hasMoreElements();)
83         {
84             Object JavaDoc value = getAttribute((String JavaDoc) e.nextElement());
85             if (findValue.equals(value))
86             {
87                 return true;
88             }
89         }
90
91         return false;
92     }
93
94     public Set JavaDoc entrySet()
95     {
96         return (_entrySet != null) ? _entrySet : (_entrySet = new EntrySet());
97     }
98
99     public Object JavaDoc get(Object JavaDoc key)
100     {
101         return getAttribute(key.toString());
102     }
103
104     public boolean isEmpty()
105     {
106         return !getAttributeNames().hasMoreElements();
107     }
108
109     public Set JavaDoc keySet()
110     {
111         return (_keySet != null) ? _keySet : (_keySet = new KeySet());
112     }
113
114     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
115     {
116         String JavaDoc key_ = key.toString();
117         Object JavaDoc retval = getAttribute(key_);
118         setAttribute(key_, value);
119         return retval;
120     }
121
122     public void putAll(Map JavaDoc t)
123     {
124         for (Iterator JavaDoc it = t.entrySet().iterator(); it.hasNext();)
125         {
126             Entry entry = (Entry) it.next();
127             setAttribute(entry.getKey().toString(), entry.getValue());
128         }
129     }
130
131     public Object JavaDoc remove(Object JavaDoc key)
132     {
133         String JavaDoc key_ = key.toString();
134         Object JavaDoc retval = getAttribute(key_);
135         removeAttribute(key_);
136         return retval;
137     }
138
139     public int size()
140     {
141         int size = 0;
142         for (Enumeration JavaDoc e = getAttributeNames(); e.hasMoreElements();)
143         {
144             size++;
145             e.nextElement();
146         }
147         return size;
148     }
149
150     public Collection JavaDoc values()
151     {
152         return (_values != null) ? _values : (_values = new Values());
153     }
154
155
156     abstract protected Object JavaDoc getAttribute(String JavaDoc key);
157
158     abstract protected void setAttribute(String JavaDoc key, Object JavaDoc value);
159
160     abstract protected void removeAttribute(String JavaDoc key);
161
162     abstract protected Enumeration JavaDoc getAttributeNames();
163
164
165     private class KeySet extends AbstractSet JavaDoc
166     {
167         public Iterator JavaDoc iterator()
168         {
169             return new KeyIterator();
170         }
171
172         public boolean isEmpty()
173         {
174             return AbstractAttributeMap.this.isEmpty();
175         }
176
177         public int size()
178         {
179             return AbstractAttributeMap.this.size();
180         }
181
182         public boolean contains(Object JavaDoc o)
183         {
184             return AbstractAttributeMap.this.containsKey(o);
185         }
186
187         public boolean remove(Object JavaDoc o)
188         {
189             return AbstractAttributeMap.this.remove(o) != null;
190         }
191
192         public void clear()
193         {
194             AbstractAttributeMap.this.clear();
195         }
196     }
197
198     private class KeyIterator
199         implements Iterator JavaDoc
200     {
201         protected final Enumeration JavaDoc _e = getAttributeNames();
202         protected Object JavaDoc _currentKey;
203
204         public void remove()
205         {
206             // remove() may cause ConcurrentModificationException.
207
// We could throw an exception here, but not throwing an exception
208
// allows one call to remove() to succeed
209
if (_currentKey == null)
210             {
211                 throw new NoSuchElementException JavaDoc(
212                     "You must call next() at least once");
213             }
214             AbstractAttributeMap.this.remove(_currentKey);
215         }
216
217         public boolean hasNext()
218         {
219             return _e.hasMoreElements();
220         }
221
222         public Object JavaDoc next()
223         {
224             return _currentKey = _e.nextElement();
225         }
226     }
227
228     private class Values extends KeySet
229     {
230         public Iterator JavaDoc iterator()
231         {
232             return new ValuesIterator();
233         }
234
235         public boolean contains(Object JavaDoc o)
236         {
237             return AbstractAttributeMap.this.containsValue(o);
238         }
239         
240         public boolean remove(Object JavaDoc o)
241         {
242             if (o == null)
243             {
244                 return false;
245             }
246             
247             for (Iterator JavaDoc it = iterator(); it.hasNext();)
248             {
249                 if (o.equals(it.next()))
250                 {
251                     it.remove();
252                     return true;
253                 }
254             }
255             
256             return false;
257         }
258     }
259
260     private class ValuesIterator extends KeyIterator
261     {
262         public Object JavaDoc next()
263         {
264             super.next();
265             return AbstractAttributeMap.this.get(_currentKey);
266         }
267     }
268
269     private class EntrySet extends KeySet
270     {
271         public Iterator JavaDoc iterator() {
272             return new EntryIterator();
273         }
274         
275         public boolean contains(Object JavaDoc o) {
276             if (!(o instanceof Entry))
277             {
278                 return false;
279             }
280             
281             Entry entry = (Entry) o;
282             Object JavaDoc key = entry.getKey();
283             Object JavaDoc value = entry.getValue();
284             if (key == null || value == null)
285             {
286                 return false;
287             }
288             
289             return value.equals(AbstractAttributeMap.this.get(key));
290         }
291         
292         public boolean remove(Object JavaDoc o) {
293             if (!(o instanceof Entry))
294             {
295                 return false;
296             }
297             
298             Entry entry = (Entry) o;
299             Object JavaDoc key = entry.getKey();
300             Object JavaDoc value = entry.getValue();
301             if (key == null || value == null
302                 || !value.equals(AbstractAttributeMap.this.get(key)))
303             {
304                 return false;
305             }
306             
307             return AbstractAttributeMap.this.remove(((Entry) o).getKey()) != null;
308         }
309     }
310
311     /**
312      * Not very efficient since it generates a new instance of <code>Entry</code>
313      * for each element and still internaly uses the <code>KeyIterator</code>.
314      * It is more efficient to use the <code>KeyIterator</code> directly.
315      */

316     private class EntryIterator extends KeyIterator
317     {
318         public Object JavaDoc next()
319         {
320             super.next();
321             // Must create new Entry every time--value of the entry must stay
322
// linked to the same attribute name
323
return new EntrySetEntry(_currentKey);
324         }
325     }
326
327     private class EntrySetEntry implements Entry
328     {
329         private final Object JavaDoc _currentKey;
330         
331         public EntrySetEntry(Object JavaDoc currentKey)
332         {
333             _currentKey = currentKey;
334         }
335         
336         public Object JavaDoc getKey()
337         {
338             return _currentKey;
339         }
340
341         public Object JavaDoc getValue()
342         {
343             return AbstractAttributeMap.this.get(_currentKey);
344         }
345
346         public Object JavaDoc setValue(Object JavaDoc value)
347         {
348             return AbstractAttributeMap.this.put(_currentKey, value);
349         }
350     }
351 }
352
Popular Tags