KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > MapFactoryBean


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.beans.factory.config;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.springframework.beans.BeanUtils;
23 import org.springframework.beans.TypeConverter;
24 import org.springframework.core.CollectionFactory;
25 import org.springframework.core.GenericCollectionTypeResolver;
26 import org.springframework.core.JdkVersion;
27
28 /**
29  * Simple factory for shared Map instances. Allows for central setup
30  * of Maps via the "map" element in XML bean definitions.
31  *
32  * @author Juergen Hoeller
33  * @since 09.12.2003
34  * @see SetFactoryBean
35  * @see ListFactoryBean
36  */

37 public class MapFactoryBean extends AbstractFactoryBean {
38
39     private Map JavaDoc sourceMap;
40
41     private Class JavaDoc targetMapClass;
42
43
44     /**
45      * Set the source Map, typically populated via XML "map" elements.
46      */

47     public void setSourceMap(Map JavaDoc sourceMap) {
48         this.sourceMap = sourceMap;
49     }
50
51     /**
52      * Set the class to use for the target Map. Can be populated with a fully
53      * qualified class name when defined in a Spring application context.
54      * <p>Default is a linked HashMap, keeping the registration order.
55      * If no linked Map implementation is available, a plain HashMap will
56      * be used as fallback (not keeping the registration order).
57      * @see org.springframework.core.CollectionFactory#createLinkedMapIfPossible
58      */

59     public void setTargetMapClass(Class JavaDoc targetMapClass) {
60         if (targetMapClass == null) {
61             throw new IllegalArgumentException JavaDoc("'targetMapClass' must not be null");
62         }
63         if (!Map JavaDoc.class.isAssignableFrom(targetMapClass)) {
64             throw new IllegalArgumentException JavaDoc("'targetMapClass' must implement [java.util.Map]");
65         }
66         this.targetMapClass = targetMapClass;
67     }
68
69
70     public Class JavaDoc getObjectType() {
71         return Map JavaDoc.class;
72     }
73
74     protected Object JavaDoc createInstance() {
75         if (this.sourceMap == null) {
76             throw new IllegalArgumentException JavaDoc("'sourceMap' is required");
77         }
78         Map JavaDoc result = null;
79         if (this.targetMapClass != null) {
80             result = (Map JavaDoc) BeanUtils.instantiateClass(this.targetMapClass);
81         }
82         else {
83             result = CollectionFactory.createLinkedMapIfPossible(this.sourceMap.size());
84         }
85         Class JavaDoc keyType = null;
86         Class JavaDoc valueType = null;
87         if (this.targetMapClass != null && JdkVersion.isAtLeastJava15()) {
88             keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass);
89             valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass);
90         }
91         if (keyType != null || valueType != null) {
92             TypeConverter converter = getBeanTypeConverter();
93             for (Iterator JavaDoc it = this.sourceMap.entrySet().iterator(); it.hasNext();) {
94                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
95                 Object JavaDoc convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
96                 Object JavaDoc convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
97                 result.put(convertedKey, convertedValue);
98             }
99         }
100         else {
101             result.putAll(this.sourceMap);
102         }
103         return result;
104     }
105
106 }
107
Popular Tags