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.support;
018:
019: import java.util.Collection;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.springframework.beans.BeanMetadataElement;
024: import org.springframework.beans.Mergeable;
025: import org.springframework.core.CollectionFactory;
026:
027: /**
028: * Tag collection class used to hold managed Map values, which may
029: * include runtime bean references (to be resolved into bean objects).
030: *
031: * <p>Wraps a target Map, which will be a linked map if possible
032: * (that is, if running on JDK 1.4 or if Commons Collections 3.x is available).
033: *
034: * @author Juergen Hoeller
035: * @author Rob Harrop
036: * @since 27.05.2003
037: * @see org.springframework.core.CollectionFactory#createLinkedMapIfPossible
038: */
039: public class ManagedMap implements Map, Mergeable, BeanMetadataElement {
040:
041: private final Map targetMap;
042:
043: private Object source;
044:
045: private boolean mergeEnabled;
046:
047: public ManagedMap() {
048: this (16);
049: }
050:
051: public ManagedMap(int initialCapacity) {
052: this .targetMap = CollectionFactory
053: .createLinkedMapIfPossible(initialCapacity);
054: }
055:
056: public ManagedMap(Map targetMap) {
057: this .targetMap = targetMap;
058: }
059:
060: /**
061: * Set the configuration source <code>Object</code> for this metadata element.
062: * <p>The exact type of the object will depend on the configuration mechanism used.
063: */
064: public void setSource(Object source) {
065: this .source = source;
066: }
067:
068: public Object getSource() {
069: return this .source;
070: }
071:
072: /**
073: * Set whether merging should be enabled for this collection,
074: * in case of a 'parent' collection value being present.
075: */
076: public void setMergeEnabled(boolean mergeEnabled) {
077: this .mergeEnabled = mergeEnabled;
078: }
079:
080: public boolean isMergeEnabled() {
081: return this .mergeEnabled;
082: }
083:
084: public Object merge(Object parent) {
085: if (!this .mergeEnabled) {
086: throw new IllegalStateException(
087: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
088: }
089: if (parent == null) {
090: return this ;
091: }
092: if (!(parent instanceof Map)) {
093: throw new IllegalArgumentException(
094: "Cannot merge with object of type ["
095: + parent.getClass() + "]");
096: }
097: Map merged = new ManagedMap();
098: merged.putAll((Map) parent);
099: merged.putAll(this );
100: return merged;
101: }
102:
103: public int size() {
104: return this .targetMap.size();
105: }
106:
107: public boolean isEmpty() {
108: return this .targetMap.isEmpty();
109: }
110:
111: public boolean containsKey(Object key) {
112: return this .targetMap.containsKey(key);
113: }
114:
115: public boolean containsValue(Object value) {
116: return this .targetMap.containsValue(value);
117: }
118:
119: public Object get(Object key) {
120: return this .targetMap.get(key);
121: }
122:
123: public Object put(Object key, Object value) {
124: return this .targetMap.put(key, value);
125: }
126:
127: public Object remove(Object key) {
128: return this .targetMap.remove(key);
129: }
130:
131: public void putAll(Map t) {
132: this .targetMap.putAll(t);
133: }
134:
135: public void clear() {
136: this .targetMap.clear();
137: }
138:
139: public Set keySet() {
140: return this .targetMap.keySet();
141: }
142:
143: public Collection values() {
144: return this .targetMap.values();
145: }
146:
147: public Set entrySet() {
148: return this .targetMap.entrySet();
149: }
150:
151: public int hashCode() {
152: return this .targetMap.hashCode();
153: }
154:
155: public boolean equals(Object obj) {
156: return this .targetMap.equals(obj);
157: }
158:
159: public String toString() {
160: return this.targetMap.toString();
161: }
162:
163: }
|