001/*
002 * Copyright 2002-2017 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 *      https://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
017package org.springframework.beans.factory.config;
018
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.springframework.beans.BeanUtils;
023import org.springframework.beans.TypeConverter;
024import org.springframework.core.ResolvableType;
025import org.springframework.lang.Nullable;
026
027/**
028 * Simple factory for shared Map instances. Allows for central setup
029 * of Maps via the "map" element in XML bean definitions.
030 *
031 * @author Juergen Hoeller
032 * @since 09.12.2003
033 * @see SetFactoryBean
034 * @see ListFactoryBean
035 */
036public class MapFactoryBean extends AbstractFactoryBean<Map<Object, Object>> {
037
038        @Nullable
039        private Map<?, ?> sourceMap;
040
041        @SuppressWarnings("rawtypes")
042        @Nullable
043        private Class<? extends Map> targetMapClass;
044
045
046        /**
047         * Set the source Map, typically populated via XML "map" elements.
048         */
049        public void setSourceMap(Map<?, ?> sourceMap) {
050                this.sourceMap = sourceMap;
051        }
052
053        /**
054         * Set the class to use for the target Map. Can be populated with a fully
055         * qualified class name when defined in a Spring application context.
056         * <p>Default is a linked HashMap, keeping the registration order.
057         * @see java.util.LinkedHashMap
058         */
059        @SuppressWarnings("rawtypes")
060        public void setTargetMapClass(@Nullable Class<? extends Map> targetMapClass) {
061                if (targetMapClass == null) {
062                        throw new IllegalArgumentException("'targetMapClass' must not be null");
063                }
064                if (!Map.class.isAssignableFrom(targetMapClass)) {
065                        throw new IllegalArgumentException("'targetMapClass' must implement [java.util.Map]");
066                }
067                this.targetMapClass = targetMapClass;
068        }
069
070
071        @Override
072        @SuppressWarnings("rawtypes")
073        public Class<Map> getObjectType() {
074                return Map.class;
075        }
076
077        @Override
078        @SuppressWarnings("unchecked")
079        protected Map<Object, Object> createInstance() {
080                if (this.sourceMap == null) {
081                        throw new IllegalArgumentException("'sourceMap' is required");
082                }
083                Map<Object, Object> result = null;
084                if (this.targetMapClass != null) {
085                        result = BeanUtils.instantiateClass(this.targetMapClass);
086                }
087                else {
088                        result = new LinkedHashMap<>(this.sourceMap.size());
089                }
090                Class<?> keyType = null;
091                Class<?> valueType = null;
092                if (this.targetMapClass != null) {
093                        ResolvableType mapType = ResolvableType.forClass(this.targetMapClass).asMap();
094                        keyType = mapType.resolveGeneric(0);
095                        valueType = mapType.resolveGeneric(1);
096                }
097                if (keyType != null || valueType != null) {
098                        TypeConverter converter = getBeanTypeConverter();
099                        for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {
100                                Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
101                                Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
102                                result.put(convertedKey, convertedValue);
103                        }
104                }
105                else {
106                        result.putAll(this.sourceMap);
107                }
108                return result;
109        }
110
111}