001/* 002 * Copyright 2002-2012 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.support; 018 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.springframework.beans.BeanMetadataElement; 023import org.springframework.beans.Mergeable; 024 025/** 026 * Tag collection class used to hold managed Map values, which may 027 * include runtime bean references (to be resolved into bean objects). 028 * 029 * @author Juergen Hoeller 030 * @author Rob Harrop 031 * @since 27.05.2003 032 */ 033@SuppressWarnings("serial") 034public class ManagedMap<K, V> extends LinkedHashMap<K, V> implements Mergeable, BeanMetadataElement { 035 036 private Object source; 037 038 private String keyTypeName; 039 040 private String valueTypeName; 041 042 private boolean mergeEnabled; 043 044 045 public ManagedMap() { 046 } 047 048 public ManagedMap(int initialCapacity) { 049 super(initialCapacity); 050 } 051 052 053 /** 054 * Set the configuration source {@code Object} for this metadata element. 055 * <p>The exact type of the object will depend on the configuration mechanism used. 056 */ 057 public void setSource(Object source) { 058 this.source = source; 059 } 060 061 @Override 062 public Object getSource() { 063 return this.source; 064 } 065 066 /** 067 * Set the default key type name (class name) to be used for this map. 068 */ 069 public void setKeyTypeName(String keyTypeName) { 070 this.keyTypeName = keyTypeName; 071 } 072 073 /** 074 * Return the default key type name (class name) to be used for this map. 075 */ 076 public String getKeyTypeName() { 077 return this.keyTypeName; 078 } 079 080 /** 081 * Set the default value type name (class name) to be used for this map. 082 */ 083 public void setValueTypeName(String valueTypeName) { 084 this.valueTypeName = valueTypeName; 085 } 086 087 /** 088 * Return the default value type name (class name) to be used for this map. 089 */ 090 public String getValueTypeName() { 091 return this.valueTypeName; 092 } 093 094 /** 095 * Set whether merging should be enabled for this collection, 096 * in case of a 'parent' collection value being present. 097 */ 098 public void setMergeEnabled(boolean mergeEnabled) { 099 this.mergeEnabled = mergeEnabled; 100 } 101 102 @Override 103 public boolean isMergeEnabled() { 104 return this.mergeEnabled; 105 } 106 107 @Override 108 @SuppressWarnings("unchecked") 109 public Object merge(Object parent) { 110 if (!this.mergeEnabled) { 111 throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'"); 112 } 113 if (parent == null) { 114 return this; 115 } 116 if (!(parent instanceof Map)) { 117 throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]"); 118 } 119 Map<K, V> merged = new ManagedMap<K, V>(); 120 merged.putAll((Map<K, V>) parent); 121 merged.putAll(this); 122 return merged; 123 } 124 125}