KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > core > collection > LocalAttributeMap


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.webflow.core.collection;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.springframework.binding.collection.MapAccessor;
27 import org.springframework.core.style.StylerUtils;
28 import org.springframework.util.Assert;
29
30 /**
31  * A generic, mutable attribute map with string keys.
32  *
33  * @author Keith Donald
34  */

35 public class LocalAttributeMap implements MutableAttributeMap, Serializable JavaDoc {
36
37     /**
38      * The backing map storing the attributes.
39      */

40     private Map JavaDoc attributes;
41
42     /**
43      * A helper for accessing attributes. Marked transient and restored on
44      * deserialization.
45      */

46     private transient MapAccessor attributeAccessor;
47
48     /**
49      * Creates a new attribute map, initially empty.
50      */

51     public LocalAttributeMap() {
52         initAttributes(createTargetMap());
53     }
54
55     /**
56      * Creates a new attribute map, initially empty.
57      * @param size the initial size
58      * @param loadFactor the load factor
59      */

60     public LocalAttributeMap(int size, int loadFactor) {
61         initAttributes(createTargetMap(size, loadFactor));
62     }
63
64     /**
65      * Creates a new attribute map with a single entry.
66      */

67     public LocalAttributeMap(String JavaDoc attributeName, Object JavaDoc attributeValue) {
68         initAttributes(createTargetMap(1, 1));
69         put(attributeName, attributeValue);
70     }
71
72     /**
73      * Creates a new attribute map wrapping the specified map.
74      */

75     public LocalAttributeMap(Map JavaDoc map) {
76         Assert.notNull(map, "The target map is required");
77         initAttributes(map);
78     }
79
80     // implementing attribute map
81

82     public Map JavaDoc asMap() {
83         return attributeAccessor.asMap();
84     }
85
86     public int size() {
87         return attributes.size();
88     }
89
90     public Object JavaDoc get(String JavaDoc attributeName) {
91         return attributes.get(attributeName);
92     }
93
94     public boolean isEmpty() {
95         return attributes.isEmpty();
96     }
97
98     public boolean contains(String JavaDoc attributeName) {
99         return attributes.containsKey(attributeName);
100     }
101
102     public boolean contains(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
103         return attributeAccessor.containsKey(attributeName, requiredType);
104     }
105
106     public Object JavaDoc get(String JavaDoc attributeName, Object JavaDoc defaultValue) {
107         return attributeAccessor.get(attributeName, defaultValue);
108     }
109
110     public Object JavaDoc get(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
111         return attributeAccessor.get(attributeName, requiredType);
112     }
113
114     public Object JavaDoc get(String JavaDoc attributeName, Class JavaDoc requiredType, Object JavaDoc defaultValue) throws IllegalStateException JavaDoc {
115         return attributeAccessor.get(attributeName, requiredType, defaultValue);
116     }
117
118     public Object JavaDoc getRequired(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
119         return attributeAccessor.getRequired(attributeName);
120     }
121
122     public Object JavaDoc getRequired(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
123         return attributeAccessor.getRequired(attributeName, requiredType);
124     }
125
126     public String JavaDoc getString(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
127         return attributeAccessor.getString(attributeName);
128     }
129
130     public String JavaDoc getString(String JavaDoc attributeName, String JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
131         return attributeAccessor.getString(attributeName, defaultValue);
132     }
133
134     public String JavaDoc getRequiredString(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
135         return attributeAccessor.getRequiredString(attributeName);
136     }
137
138     public Collection JavaDoc getCollection(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
139         return attributeAccessor.getCollection(attributeName);
140     }
141
142     public Collection JavaDoc getCollection(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
143         return attributeAccessor.getCollection(attributeName, requiredType);
144     }
145
146     public Collection JavaDoc getRequiredCollection(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
147         return attributeAccessor.getRequiredCollection(attributeName);
148     }
149
150     public Collection JavaDoc getRequiredCollection(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
151         return attributeAccessor.getRequiredCollection(attributeName, requiredType);
152     }
153
154     public Object JavaDoc[] getArray(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
155         return attributeAccessor.getArray(attributeName, requiredType);
156     }
157
158     public Object JavaDoc[] getRequiredArray(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
159         return attributeAccessor.getRequiredArray(attributeName, requiredType);
160     }
161
162     public Number JavaDoc getNumber(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
163         return attributeAccessor.getNumber(attributeName, requiredType);
164     }
165
166     public Number JavaDoc getNumber(String JavaDoc attributeName, Class JavaDoc requiredType, Number JavaDoc defaultValue)
167             throws IllegalArgumentException JavaDoc {
168         return attributeAccessor.getNumber(attributeName, requiredType, defaultValue);
169     }
170
171     public Number JavaDoc getRequiredNumber(String JavaDoc attributeName, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
172         return attributeAccessor.getRequiredNumber(attributeName, requiredType);
173     }
174
175     public Integer JavaDoc getInteger(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
176         return attributeAccessor.getInteger(attributeName);
177     }
178
179     public Integer JavaDoc getInteger(String JavaDoc attributeName, Integer JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
180         return attributeAccessor.getInteger(attributeName, defaultValue);
181     }
182
183     public Integer JavaDoc getRequiredInteger(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
184         return attributeAccessor.getRequiredInteger(attributeName);
185     }
186
187     public Long JavaDoc getLong(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
188         return attributeAccessor.getLong(attributeName);
189     }
190
191     public Long JavaDoc getLong(String JavaDoc attributeName, Long JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
192         return attributeAccessor.getLong(attributeName, defaultValue);
193     }
194
195     public Long JavaDoc getRequiredLong(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
196         return attributeAccessor.getRequiredLong(attributeName);
197     }
198
199     public Boolean JavaDoc getBoolean(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
200         return attributeAccessor.getBoolean(attributeName);
201     }
202
203     public Boolean JavaDoc getBoolean(String JavaDoc attributeName, Boolean JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
204         return attributeAccessor.getBoolean(attributeName, defaultValue);
205     }
206
207     public Boolean JavaDoc getRequiredBoolean(String JavaDoc attributeName) throws IllegalArgumentException JavaDoc {
208         return attributeAccessor.getRequiredBoolean(attributeName);
209     }
210
211     public AttributeMap union(AttributeMap attributes) {
212         if (attributes == null) {
213             return new LocalAttributeMap(getMapInternal());
214         }
215         else {
216             Map JavaDoc map = createTargetMap();
217             map.putAll(getMapInternal());
218             map.putAll(attributes.asMap());
219             return new LocalAttributeMap(map);
220         }
221     }
222
223     // implementing MutableAttributeMap
224

225     public Object JavaDoc put(String JavaDoc attributeName, Object JavaDoc attributeValue) {
226         return getMapInternal().put(attributeName, attributeValue);
227     }
228
229     public MutableAttributeMap putAll(AttributeMap attributes) {
230         if (attributes == null) {
231             return this;
232         }
233         getMapInternal().putAll(attributes.asMap());
234         return this;
235     }
236
237     public Object JavaDoc remove(String JavaDoc attributeName) {
238         return getMapInternal().remove(attributeName);
239     }
240
241     public MutableAttributeMap clear() throws UnsupportedOperationException JavaDoc {
242         getMapInternal().clear();
243         return this;
244     }
245
246     public MutableAttributeMap replaceWith(AttributeMap attributes) throws UnsupportedOperationException JavaDoc {
247         clear();
248         putAll(attributes);
249         return this;
250     }
251
252     // helpers for subclasses
253

254     /**
255      * Initializes this attribute map.
256      * @param attributes the attributes
257      */

258     protected void initAttributes(Map JavaDoc attributes) {
259         this.attributes = attributes;
260         attributeAccessor = new MapAccessor(this.attributes);
261     }
262
263     /**
264      * Returns the wrapped, modifiable map implementation.
265      */

266     protected Map JavaDoc getMapInternal() {
267         return attributes;
268     }
269
270     // helpers
271

272     /**
273      * Factory method that returns the target map storing the data in this
274      * attribute map.
275      * @return the target map
276      */

277     protected Map JavaDoc createTargetMap() {
278         return new HashMap JavaDoc();
279     }
280
281     /**
282      * Factory method that returns the target map storing the data in this
283      * attribute map.
284      * @param size the initial size of the map
285      * @param loadFactor the load factor
286      * @return the target map
287      */

288     protected Map JavaDoc createTargetMap(int size, int loadFactor) {
289         return new HashMap JavaDoc(size, loadFactor);
290     }
291
292     public boolean equals(Object JavaDoc o) {
293         if (!(o instanceof LocalAttributeMap)) {
294             return false;
295         }
296         LocalAttributeMap other = (LocalAttributeMap)o;
297         return getMapInternal().equals(other.getMapInternal());
298     }
299
300     public int hashCode() {
301         return getMapInternal().hashCode();
302     }
303
304     // custom serialization
305

306     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
307         out.defaultWriteObject();
308     }
309
310     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
311         in.defaultReadObject();
312         attributeAccessor = new MapAccessor(attributes);
313     }
314
315     public String JavaDoc toString() {
316         return StylerUtils.style(attributes);
317     }
318 }
Popular Tags