KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > beans > MapWrapper


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.beans;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.springframework.beans.BeanUtils;
30 import org.springframework.beans.BeansException;
31 import org.springframework.beans.PropertyValue;
32 import org.springframework.beans.PropertyValues;
33 import org.springframework.util.Assert;
34
35 /**
36  * PropertyAccessor implemenation that works on maps.
37  */

38 public class MapWrapper implements ObjectWrapper {
39
40     private Map JavaDoc map;
41
42     private Class JavaDoc valueClass;
43
44     private Class JavaDoc mapClass = HashMap JavaDoc.class;
45
46     public MapWrapper(Map JavaDoc map) {
47         this.map = map;
48     }
49
50     public MapWrapper(Class JavaDoc mapClass) {
51         this.mapClass = mapClass;
52     }
53
54     public void setMapClass(Class JavaDoc mapClass) {
55         this.mapClass = mapClass;
56     }
57
58     public void setValueClass(Class JavaDoc valueClass) {
59         this.valueClass = valueClass;
60     }
61
62     public boolean isReadableProperty(String JavaDoc propertyName) {
63         return true;
64     }
65
66     public boolean isWritableProperty(String JavaDoc propertyName) {
67         return true;
68     }
69
70     public Class JavaDoc getPropertyType(String JavaDoc propertyName) {
71         if (valueClass != null) {
72             return valueClass;
73         }
74         Object JavaDoc value = getPropertyValue(propertyName);
75         if (value != null) {
76             return value.getClass();
77         }
78         return Object JavaDoc.class;
79     }
80
81     public void setObject(Object JavaDoc object) {
82         Assert.isInstanceOf(Map JavaDoc.class, object);
83         map = (Map JavaDoc) object;
84     }
85
86     public Object JavaDoc getObject() {
87         return getMap();
88     }
89
90     public Class JavaDoc getObjectClass() {
91         return mapClass;
92     }
93
94     protected Map JavaDoc getMap() {
95         if (map == null) {
96             map = (Map JavaDoc) BeanUtils.instantiateClass(mapClass);
97         }
98         return map;
99     }
100
101     public Object JavaDoc getPropertyValue(String JavaDoc propertyName) {
102         return getMap().get(propertyName);
103     }
104
105     public void setPropertyValue(String JavaDoc propertyName, Object JavaDoc value) {
106         getMap().put(propertyName, value);
107     }
108
109     public void setPropertyValue(PropertyValue pv) {
110         setPropertyValue(pv.getName(), pv.getValue());
111     }
112
113     public void setPropertyValues(Map JavaDoc map) {
114         getMap().putAll(map);
115     }
116
117     public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown,
118             boolean ignoreInvalid) throws BeansException {
119
120         setPropertyValues(pvs);
121     }
122
123     public void setPropertyValues(PropertyValues pvs) {
124         PropertyValue[] pv = pvs.getPropertyValues();
125         for (int i = 0; i < pv.length; i++) {
126             setPropertyValue(pv[i]);
127         }
128     }
129
130     public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown) {
131         setPropertyValues(pvs);
132     }
133
134 }
135
Popular Tags