KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > beans > metadata > plugins > AbstractMapMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.beans.metadata.plugins;
23
24 import java.util.*;
25
26 import org.jboss.beans.info.spi.BeanInfo;
27 import org.jboss.beans.metadata.spi.MetaDataVisitor;
28 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
29 import org.jboss.beans.metadata.spi.ValueMetaData;
30 import org.jboss.joinpoint.spi.Joinpoint;
31 import org.jboss.reflect.spi.ClassInfo;
32 import org.jboss.reflect.spi.TypeInfo;
33 import org.jboss.util.JBossStringBuilder;
34
35 /**
36  * Map metadata.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 56338 $
40  */

41 public class AbstractMapMetaData extends AbstractTypeMetaData implements Map<MetaDataVisitorNode, MetaDataVisitorNode>
42 {
43    /** The map */
44    private HashMap<MetaDataVisitorNode, MetaDataVisitorNode> map = new HashMap<MetaDataVisitorNode, MetaDataVisitorNode>();
45
46    /** The key type */
47    protected String JavaDoc keyType;
48
49    /** The value type */
50    protected String JavaDoc valueType;
51
52    /**
53     * Create a new map value
54     */

55    public AbstractMapMetaData()
56    {
57    }
58
59    /**
60     * Get the key type
61     *
62     * @return the key type
63     */

64    public String JavaDoc getKeyType()
65    {
66       return keyType;
67    }
68
69    /**
70     * Set the key type
71     *
72     * @param keyType the key type
73     */

74    public void setKeyType(String JavaDoc keyType)
75    {
76       this.keyType = keyType;
77    }
78
79    /**
80     * Get the value type
81     *
82     * @return the value type
83     */

84    public String JavaDoc getValueType()
85    {
86       return valueType;
87    }
88
89    /**
90     * Set the value type
91     *
92     * @param valueType the value type
93     */

94    public void setValueType(String JavaDoc valueType)
95    {
96       this.valueType = valueType;
97    }
98
99    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
100    {
101       Map<Object JavaDoc, Object JavaDoc> result = getMapInstance(info, cl, Map.class);
102       if (result == null)
103          result = getDefaultMapInstance();
104
105       TypeInfo keyTypeInfo = getKeyClassInfo(cl);
106       TypeInfo valueTypeInfo = getValueClassInfo(cl);
107
108       if (map.size() > 0)
109       {
110          for (Iterator i = map.entrySet().iterator(); i.hasNext();)
111          {
112             Map.Entry entry = (Map.Entry) i.next();
113             ValueMetaData key = (ValueMetaData) entry.getKey();
114             ValueMetaData value = (ValueMetaData) entry.getValue();
115             Object JavaDoc keyValue = key.getValue(keyTypeInfo, cl);
116             Object JavaDoc valueValue = value.getValue(valueTypeInfo, cl);
117             result.put(keyValue, valueValue);
118          }
119       }
120       return result;
121    }
122
123    public void clear()
124    {
125       map.clear();
126    }
127
128    public boolean containsKey(Object JavaDoc key)
129    {
130       return map.containsKey(key);
131    }
132
133    public boolean containsValue(Object JavaDoc value)
134    {
135       return map.containsValue(value);
136    }
137
138    public Set<Entry<MetaDataVisitorNode, MetaDataVisitorNode>> entrySet()
139    {
140       return map.entrySet();
141    }
142
143    public MetaDataVisitorNode get(Object JavaDoc key)
144    {
145       return map.get(key);
146    }
147
148    public boolean isEmpty()
149    {
150       return map.isEmpty();
151    }
152
153    public Set<MetaDataVisitorNode> keySet()
154    {
155       return map.keySet();
156    }
157
158    public MetaDataVisitorNode put(MetaDataVisitorNode key, MetaDataVisitorNode value)
159    {
160       return map.put(key, value);
161    }
162
163    public void putAll(Map<? extends MetaDataVisitorNode, ? extends MetaDataVisitorNode> t)
164    {
165       putAll(t);
166
167    }
168
169    public MetaDataVisitorNode remove(Object JavaDoc key)
170    {
171       return map.remove(key);
172    }
173
174    public int size()
175    {
176       return map.size();
177    }
178
179    public Collection<MetaDataVisitorNode> values()
180    {
181       return map.values();
182    }
183
184    public Iterator<? extends MetaDataVisitorNode> getChildren()
185    {
186       ArrayList<MetaDataVisitorNode> children = new ArrayList<MetaDataVisitorNode>(keySet());
187       children.addAll(values());
188       return children.iterator();
189    }
190
191    public Class JavaDoc getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable JavaDoc
192    {
193       if (keyType != null)
194       {
195          for(MetaDataVisitorNode key : keySet())
196          {
197             if (previous.equals(key))
198             {
199                return getClass(visitor, keyType);
200             }
201          }
202       }
203       if (valueType != null)
204       {
205          for(MetaDataVisitorNode v : values())
206          {
207             if (previous.equals(v))
208             {
209                return getClass(visitor, valueType);
210             }
211          }
212       }
213       return super.getType(visitor, previous);
214    }
215
216    public void toString(JBossStringBuilder buffer)
217    {
218       super.toString(buffer);
219    }
220
221    /**
222     * Create the default map instance
223     *
224     * @return the class instance
225     * @throws Throwable for any error
226     */

227    protected Map<Object JavaDoc, Object JavaDoc> getDefaultMapInstance() throws Throwable JavaDoc
228    {
229       return new HashMap<Object JavaDoc, Object JavaDoc>();
230    }
231
232    /**
233     * Create the map instance
234     *
235     * @param info the request type
236     * @param cl the classloader
237     * @param expected the expected class
238     * @return the class instance
239     * @throws Throwable for any error
240     */

241    @SuppressWarnings JavaDoc("unchecked")
242    protected Map<Object JavaDoc, Object JavaDoc> getMapInstance(TypeInfo info, ClassLoader JavaDoc cl, Class JavaDoc<?> expected) throws Throwable JavaDoc
243    {
244       Object JavaDoc result = preinstantiatedLookup(cl, expected);
245       if (result == null)
246       {
247          TypeInfo typeInfo = getClassInfo(cl);
248
249          if (typeInfo != null && typeInfo instanceof ClassInfo == false)
250             throw new IllegalArgumentException JavaDoc(typeInfo.getName() + " is not a class");
251
252          if (typeInfo != null && ((ClassInfo) typeInfo).isInterface())
253             throw new IllegalArgumentException JavaDoc(typeInfo.getName() + " is an interface");
254
255          if (typeInfo == null)
256          {
257             // No type specified
258
if (info == null)
259                return null;
260             // Not a class
261
if (info instanceof ClassInfo == false)
262                return null;
263             // Not an interface
264
if (((ClassInfo) info).isInterface())
265                return null;
266             // Type is too general
267
if (Object JavaDoc.class.getName().equals(info.getName()))
268                return null;
269             // Try to use the passed type
270
typeInfo = info;
271          }
272
273          BeanInfo beanInfo = configurator.getBeanInfo(typeInfo);
274          Joinpoint constructor = configurator.getConstructorJoinPoint(beanInfo);
275          result = constructor.dispatch();
276          if (expected.isAssignableFrom(result.getClass()) == false)
277             throw new ClassCastException JavaDoc(result.getClass() + " is not a " + expected.getName());
278       }
279       return (Map<Object JavaDoc, Object JavaDoc>) result;
280    }
281
282    /**
283     * Get the class info for the key type
284     *
285     * @param cl the classloader
286     * @return the class info
287     * @throws Throwable for any error
288     */

289    protected ClassInfo getKeyClassInfo(ClassLoader JavaDoc cl) throws Throwable JavaDoc
290    {
291       if (keyType == null)
292          return null;
293
294       return configurator.getClassInfo(keyType, cl);
295    }
296
297    /**
298     * Get the class info for the value type
299     *
300     * @param cl the classloader
301     * @return the class info
302     * @throws Throwable for any error
303     */

304    protected ClassInfo getValueClassInfo(ClassLoader JavaDoc cl) throws Throwable JavaDoc
305    {
306       if (valueType == null)
307          return null;
308
309       return configurator.getClassInfo(valueType, cl);
310    }
311 }
Popular Tags