KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > context > AbstractAttributeMap


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 /*
35  * Created on May 18, 2005
36  */

37 package com.icesoft.faces.context;
38
39 import java.util.AbstractCollection JavaDoc;
40 import java.util.AbstractSet JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.NoSuchElementException JavaDoc;
48 import java.util.Set JavaDoc;
49
50 public abstract class AbstractAttributeMap implements Map JavaDoc {
51
52     private Collection JavaDoc values;
53     private Set JavaDoc keySet;
54     private Set JavaDoc entrySet;
55
56     /* (
57       * @see java.util.Map#size()
58       */

59     public int size() {
60         int size = 0;
61         Enumeration JavaDoc e = getAttributeNames();
62         while (e.hasMoreElements()) {
63             e.nextElement();
64             size++;
65         }
66         return size;
67     }
68
69     /*
70       * @see java.util.Map#clear()
71       */

72     public void clear() {
73         Enumeration JavaDoc e = getAttributeNames();
74         List JavaDoc keys = new ArrayList JavaDoc();
75         while (e.hasMoreElements())
76             keys.add(e.nextElement());
77
78         Iterator JavaDoc iterator = keys.iterator();
79         while (iterator.hasNext())
80             removeAttribute((String JavaDoc) iterator.next());
81     }
82
83     /*
84       * @see java.util.Map#isEmpty()
85       */

86     public boolean isEmpty() {
87         return !getAttributeNames().hasMoreElements();
88     }
89
90     /* (non-Javadoc)
91       * @see java.util.Map#containsKey(java.lang.Object)
92       */

93     public boolean containsKey(Object JavaDoc key) {
94         return getAttribute(key.toString()) != null;
95     }
96
97     /* (non-Javadoc)
98       * @see java.util.Map#containsValue(java.lang.Object)
99       */

100     public boolean containsValue(Object JavaDoc value) {
101         if (value == null)
102             return false;
103
104         Enumeration JavaDoc e = getAttributeNames();
105         while (e.hasMoreElements()) {
106             if (value.equals(getAttribute(e.nextElement().toString()))) {
107                 return true;
108             }
109         }
110         return false;
111     }
112
113     /* (non-Javadoc)
114       * @see java.util.Map#values()
115       */

116     public Collection JavaDoc values() {
117         return (values != null) ? values : (values = new Values());
118     }
119
120     /* (non-Javadoc)
121       * @see java.util.Map#putAll(java.util.Map)
122       */

123     public void putAll(Map JavaDoc map) {
124         Iterator JavaDoc iterator = map.entrySet().iterator();
125         while (iterator.hasNext()) {
126             Entry entry = (Entry) iterator.next();
127             setAttribute(entry.getKey().toString(), entry.getValue());
128         }
129     }
130
131     /* (non-Javadoc)
132       * @see java.util.Map#entrySet()
133       */

134     public Set JavaDoc entrySet() {
135         return (entrySet != null) ? entrySet : (entrySet = new EntrySet());
136     }
137
138     /* (non-Javadoc)
139       * @see java.util.Map#keySet()
140       */

141     public Set JavaDoc keySet() {
142         return (keySet != null) ? keySet : (keySet = new KeySet());
143     }
144
145     /* (non-Javadoc)
146       * @see java.util.Map#get(java.lang.Object)
147       */

148     public Object JavaDoc get(Object JavaDoc key) {
149         return getAttribute(key.toString());
150     }
151
152     /*
153       * @see java.util.Map#remove(java.lang.Object)
154       */

155     public Object JavaDoc remove(Object JavaDoc key) {
156         Object JavaDoc associatedValue = getAttribute(key.toString());
157         removeAttribute(key.toString());
158         return associatedValue;
159     }
160
161     /* (non-Javadoc)
162       * @see java.util.Map#put(java.lang.Object, java.lang.Object)
163       */

164     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
165         Object JavaDoc previousValue = getAttribute(key.toString());
166         setAttribute(key.toString(), value);
167         return previousValue;
168     }
169
170     abstract protected Object JavaDoc getAttribute(String JavaDoc key);
171
172     abstract protected void setAttribute(String JavaDoc key, Object JavaDoc value);
173
174     abstract protected void removeAttribute(String JavaDoc key);
175
176     abstract protected Enumeration JavaDoc getAttributeNames();
177
178     private class KeySet extends AbstractSet JavaDoc {
179
180         /*
181            * @see java.util.AbstractCollection#iterator()
182            */

183         public Iterator JavaDoc iterator() {
184             return new KeyIterator();
185         }
186
187         /*
188            * @see java.util.AbstractCollection#size()
189            */

190         public int size() {
191             return AbstractAttributeMap.this.size();
192         }
193
194         public boolean isEmpty() {
195             return AbstractAttributeMap.this.isEmpty();
196         }
197
198         public boolean contains(Object JavaDoc key) {
199             return AbstractAttributeMap.this.containsKey(key);
200         }
201
202         public boolean remove(Object JavaDoc key) {
203             return AbstractAttributeMap.this.remove(key) != null;
204         }
205
206         public void clear() {
207             AbstractAttributeMap.this.clear();
208         }
209     }
210
211     private class KeyIterator implements Iterator JavaDoc {
212
213         protected final Enumeration JavaDoc e = getAttributeNames();
214         protected Object JavaDoc currentKey;
215
216         /*
217            * @see java.util.Iterator#hasNext()
218            */

219         public boolean hasNext() {
220             return e.hasMoreElements();
221         }
222
223         /*
224            * @see java.util.Iterator#next()
225            */

226         public Object JavaDoc next() {
227             return currentKey = e.nextElement();
228         }
229
230         /*
231            * @see java.util.Iterator#remove()
232            */

233         public void remove() {
234             if (currentKey == null) {
235                 throw new NoSuchElementException JavaDoc(
236                         "No element is pointed for remove operation");
237             }
238             AbstractAttributeMap.this.remove(currentKey);
239         }
240     }
241
242     private class Values extends AbstractCollection JavaDoc {
243
244         public Iterator JavaDoc iterator() {
245             return new ValuesIterator();
246         }
247
248         public boolean contains(Object JavaDoc value) {
249             return containsValue(value);
250         }
251
252         public boolean remove(Object JavaDoc value) {
253             if (value == null)
254                 return false;
255
256             Iterator JavaDoc iterator = iterator();
257             while (iterator.hasNext()) {
258                 if (value.equals(iterator.next())) {
259                     iterator.remove();
260                     return true;
261                 }
262             }
263             return false;
264         }
265
266         public int size() {
267             return AbstractAttributeMap.this.size();
268         }
269
270         public boolean isEmpty() {
271             return AbstractAttributeMap.this.isEmpty();
272         }
273
274         public void clear() {
275             AbstractAttributeMap.this.clear();
276         }
277     }
278
279     private class ValuesIterator extends KeyIterator {
280         public Object JavaDoc next() {
281             super.next();
282             return AbstractAttributeMap.this.get(currentKey);
283         }
284     }
285
286     private class EntrySet extends KeySet {
287         public Iterator JavaDoc iterator() {
288             return new EntryIterator();
289         }
290
291         public boolean contains(Object JavaDoc object) {
292             if (object == null)
293                 return false;
294
295             if (!(object instanceof Entry))
296                 return false;
297
298             Entry entry = (Entry) object;
299             Object JavaDoc key = entry.getKey();
300             Object JavaDoc value = entry.getValue();
301
302             if (key == null || value == null)
303                 return false;
304
305             return value.equals(AbstractAttributeMap.this.get(key));
306         }
307
308         public boolean remove(Object JavaDoc object) {
309             if (object == null)
310                 return false;
311
312             if (!(object instanceof Entry))
313                 return false;
314
315             Entry entry = (Entry) object;
316             Object JavaDoc key = entry.getKey();
317             Object JavaDoc value = entry.getValue();
318             if (!value.equals(AbstractAttributeMap.this.get(key)) ||
319                 key == null || value == null)
320                 return false;
321
322             return AbstractAttributeMap.this.remove(key) != null;
323         }
324     }
325
326     private class EntryIterator extends KeyIterator {
327         public Object JavaDoc next() {
328             super.next();
329             return new EntrySetEntry(currentKey);
330         }
331     }
332
333     private class EntrySetEntry implements Entry {
334         private final Object JavaDoc currentKey;
335
336         public EntrySetEntry(Object JavaDoc currentKey) {
337             this.currentKey = currentKey;
338         }
339
340         public Object JavaDoc getKey() {
341             return currentKey;
342         }
343
344         public Object JavaDoc getValue() {
345             return AbstractAttributeMap.this.get(currentKey);
346         }
347
348         public Object JavaDoc setValue(Object JavaDoc value) {
349             return AbstractAttributeMap.this.put(currentKey, value);
350         }
351     }
352 }
353
Popular Tags