KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > collection > StringKeyedMapAdapter


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.binding.collection;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * Base class for map adapters whose keys are String values. Concrete
26  * classes need only implement the abstract hook methods defined by this class.
27  *
28  * @author Keith Donald
29  */

30 public abstract class StringKeyedMapAdapter implements Map JavaDoc {
31     
32     private Set JavaDoc keySet;
33
34     private Collection JavaDoc values;
35
36     private Set JavaDoc entrySet;
37     
38     // implementing Map
39

40     public void clear() {
41         for (Iterator JavaDoc it = getAttributeNames(); it.hasNext();) {
42             removeAttribute((String JavaDoc)it.next());
43         }
44     }
45
46     public boolean containsKey(Object JavaDoc key) {
47         return getAttribute(key.toString()) != null;
48     }
49
50     public boolean containsValue(Object JavaDoc value) {
51         if (value == null) {
52             return false;
53         }
54         for (Iterator JavaDoc it = getAttributeNames(); it.hasNext();) {
55             Object JavaDoc aValue = getAttribute((String JavaDoc)it.next());
56             if (value.equals(aValue)) {
57                 return true;
58             }
59         }
60         return false;
61     }
62
63     public Set JavaDoc entrySet() {
64         return (entrySet != null) ? entrySet : (entrySet = new EntrySet());
65     }
66
67     public Object JavaDoc get(Object JavaDoc key) {
68         return getAttribute(key.toString());
69     }
70
71     public boolean isEmpty() {
72         return !getAttributeNames().hasNext();
73     }
74
75     public Set JavaDoc keySet() {
76         return (keySet != null) ? keySet : (keySet = new KeySet());
77     }
78
79     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
80         String JavaDoc stringKey = String.valueOf(key);
81         Object JavaDoc previousValue = getAttribute(stringKey);
82         setAttribute(stringKey, value);
83         return previousValue;
84     }
85
86     public void putAll(Map JavaDoc map) {
87         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext();) {
88             Entry entry = (Entry)it.next();
89             setAttribute(entry.getKey().toString(), entry.getValue());
90         }
91     }
92
93     public Object JavaDoc remove(Object JavaDoc key) {
94         String JavaDoc stringKey = key.toString();
95         Object JavaDoc retval = getAttribute(stringKey);
96         removeAttribute(stringKey);
97         return retval;
98     }
99
100     public int size() {
101         int size = 0;
102         for (Iterator JavaDoc it = getAttributeNames(); it.hasNext();) {
103             size++;
104             it.next();
105         }
106         return size;
107     }
108
109     public Collection JavaDoc values() {
110         return (values != null) ? values : (values = new Values());
111     }
112     
113     // hook methods
114

115     /**
116      * Hook method that needs to be implemented by concrete subclasses.
117      * Gets a value associated with a key.
118      * @param key the key to lookup
119      * @return the associated value, or null if none
120      */

121     protected abstract Object JavaDoc getAttribute(String JavaDoc key);
122
123     /**
124      * Hook method that needs to be implemented by concrete subclasses.
125      * Puts a key-value pair in the map, overwriting any possible earlier
126      * value associated with the same key.
127      * @param key the key to associate the value with
128      * @param value the value to associate with the key
129      */

130     protected abstract void setAttribute(String JavaDoc key, Object JavaDoc value);
131
132     /**
133      * Hook method that needs to be implemented by concrete subclasses.
134      * Removes a key and its associated value from the map.
135      * @param key the key to remove
136      */

137     protected abstract void removeAttribute(String JavaDoc key);
138
139     /**
140      * Hook method that needs to be implemented by concrete subclasses.
141      * Returns an enumeration listing all keys known to the map.
142      * @return the key enumeration
143      */

144     protected abstract Iterator JavaDoc getAttributeNames();
145     
146     // internal helper classes
147

148     private abstract class AbstractSet extends java.util.AbstractSet JavaDoc {
149         public boolean isEmpty() {
150             return StringKeyedMapAdapter.this.isEmpty();
151         }
152
153         public int size() {
154             return StringKeyedMapAdapter.this.size();
155         }
156
157         public void clear() {
158             StringKeyedMapAdapter.this.clear();
159         }
160     }
161
162     private class KeySet extends AbstractSet {
163         public Iterator JavaDoc iterator() {
164             return new KeyIterator();
165         }
166
167         public boolean contains(Object JavaDoc o) {
168             return StringKeyedMapAdapter.this.containsKey(o);
169         }
170
171         public boolean remove(Object JavaDoc o) {
172             return StringKeyedMapAdapter.this.remove(o) != null;
173         }
174     }
175
176     private class KeyIterator implements Iterator JavaDoc {
177         protected final Iterator JavaDoc it = getAttributeNames();
178
179         protected Object JavaDoc currentKey;
180
181         public void remove() {
182             if (currentKey == null) {
183                 throw new NoSuchElementException JavaDoc("You must call next() at least once");
184             }
185             StringKeyedMapAdapter.this.remove(currentKey);
186         }
187
188         public boolean hasNext() {
189             return it.hasNext();
190         }
191
192         public Object JavaDoc next() {
193             return currentKey = it.next();
194         }
195     }
196
197     private class Values extends AbstractSet {
198         public Iterator JavaDoc iterator() {
199             return new ValuesIterator();
200         }
201
202         public boolean contains(Object JavaDoc o) {
203             return StringKeyedMapAdapter.this.containsValue(o);
204         }
205
206         public boolean remove(Object JavaDoc o) {
207             if (o == null) {
208                 return false;
209             }
210             for (Iterator JavaDoc it = iterator(); it.hasNext();) {
211                 if (o.equals(it.next())) {
212                     it.remove();
213                     return true;
214                 }
215             }
216             return false;
217         }
218     }
219
220     private class ValuesIterator extends KeyIterator {
221         public Object JavaDoc next() {
222             super.next();
223             return StringKeyedMapAdapter.this.get(currentKey);
224         }
225     }
226
227     private class EntrySet extends AbstractSet {
228         public Iterator JavaDoc iterator() {
229             return new EntryIterator();
230         }
231
232         public boolean contains(Object JavaDoc o) {
233             if (!(o instanceof Entry)) {
234                 return false;
235             }
236             Entry entry = (Entry)o;
237             Object JavaDoc key = entry.getKey();
238             Object JavaDoc value = entry.getValue();
239             if (key == null || value == null) {
240                 return false;
241             }
242             return value.equals(StringKeyedMapAdapter.this.get(key));
243         }
244
245         public boolean remove(Object JavaDoc o) {
246             if (!(o instanceof Entry)) {
247                 return false;
248             }
249             Entry entry = (Entry)o;
250             Object JavaDoc key = entry.getKey();
251             Object JavaDoc value = entry.getValue();
252             if (key == null || value == null || !value.equals(StringKeyedMapAdapter.this.get(key))) {
253                 return false;
254             }
255             return StringKeyedMapAdapter.this.remove(((Entry)o).getKey()) != null;
256         }
257     }
258
259     private class EntryIterator extends KeyIterator {
260         public Object JavaDoc next() {
261             super.next();
262             return new EntrySetEntry(currentKey);
263         }
264     }
265
266     private class EntrySetEntry implements Entry {
267         private final Object JavaDoc currentKey;
268
269         public EntrySetEntry(Object JavaDoc currentKey) {
270             this.currentKey = currentKey;
271         }
272
273         public Object JavaDoc getKey() {
274             return currentKey;
275         }
276
277         public Object JavaDoc getValue() {
278             return StringKeyedMapAdapter.this.get(currentKey);
279         }
280
281         public Object JavaDoc setValue(Object JavaDoc value) {
282             return StringKeyedMapAdapter.this.put(currentKey, value);
283         }
284     }
285 }
Popular Tags