001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.springframework.beans.BeanUtils;
023: import org.springframework.beans.TypeConverter;
024: import org.springframework.core.CollectionFactory;
025: import org.springframework.core.GenericCollectionTypeResolver;
026: import org.springframework.core.JdkVersion;
027:
028: /**
029: * Simple factory for shared Map instances. Allows for central setup
030: * of Maps via the "map" element in XML bean definitions.
031: *
032: * @author Juergen Hoeller
033: * @since 09.12.2003
034: * @see SetFactoryBean
035: * @see ListFactoryBean
036: */
037: public class MapFactoryBean extends AbstractFactoryBean {
038:
039: private Map sourceMap;
040:
041: private Class targetMapClass;
042:
043: /**
044: * Set the source Map, typically populated via XML "map" elements.
045: */
046: public void setSourceMap(Map sourceMap) {
047: this .sourceMap = sourceMap;
048: }
049:
050: /**
051: * Set the class to use for the target Map. Can be populated with a fully
052: * qualified class name when defined in a Spring application context.
053: * <p>Default is a linked HashMap, keeping the registration order.
054: * If no linked Map implementation is available, a plain HashMap will
055: * be used as fallback (not keeping the registration order).
056: * @see org.springframework.core.CollectionFactory#createLinkedMapIfPossible
057: */
058: public void setTargetMapClass(Class targetMapClass) {
059: if (targetMapClass == null) {
060: throw new IllegalArgumentException(
061: "'targetMapClass' must not be null");
062: }
063: if (!Map.class.isAssignableFrom(targetMapClass)) {
064: throw new IllegalArgumentException(
065: "'targetMapClass' must implement [java.util.Map]");
066: }
067: this .targetMapClass = targetMapClass;
068: }
069:
070: public Class getObjectType() {
071: return Map.class;
072: }
073:
074: protected Object createInstance() {
075: if (this .sourceMap == null) {
076: throw new IllegalArgumentException(
077: "'sourceMap' is required");
078: }
079: Map result = null;
080: if (this .targetMapClass != null) {
081: result = (Map) BeanUtils
082: .instantiateClass(this .targetMapClass);
083: } else {
084: result = CollectionFactory
085: .createLinkedMapIfPossible(this .sourceMap.size());
086: }
087: Class keyType = null;
088: Class valueType = null;
089: if (this .targetMapClass != null && JdkVersion.isAtLeastJava15()) {
090: keyType = GenericCollectionTypeResolver
091: .getMapKeyType(this .targetMapClass);
092: valueType = GenericCollectionTypeResolver
093: .getMapValueType(this .targetMapClass);
094: }
095: if (keyType != null || valueType != null) {
096: TypeConverter converter = getBeanTypeConverter();
097: for (Iterator it = this .sourceMap.entrySet().iterator(); it
098: .hasNext();) {
099: Map.Entry entry = (Map.Entry) it.next();
100: Object convertedKey = converter.convertIfNecessary(
101: entry.getKey(), keyType);
102: Object convertedValue = converter.convertIfNecessary(
103: entry.getValue(), valueType);
104: result.put(convertedKey, convertedValue);
105: }
106: } else {
107: result.putAll(this.sourceMap);
108: }
109: return result;
110: }
111:
112: }
|