01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.support;
18:
19: import java.util.LinkedHashMap;
20: import java.util.Map;
21:
22: import org.springframework.beans.BeanMetadataElement;
23: import org.springframework.beans.Mergeable;
24:
25: /**
26: * Tag collection class used to hold managed Map values, which may
27: * include runtime bean references (to be resolved into bean objects).
28: *
29: * @author Juergen Hoeller
30: * @author Rob Harrop
31: * @since 27.05.2003
32: */
33: public class ManagedMap extends LinkedHashMap implements Mergeable,
34: BeanMetadataElement {
35:
36: private Object source;
37:
38: private boolean mergeEnabled;
39:
40: public ManagedMap() {
41: }
42:
43: public ManagedMap(int initialCapacity) {
44: super (initialCapacity);
45: }
46:
47: /**
48: * Set the configuration source <code>Object</code> for this metadata element.
49: * <p>The exact type of the object will depend on the configuration mechanism used.
50: */
51: public void setSource(Object source) {
52: this .source = source;
53: }
54:
55: public Object getSource() {
56: return this .source;
57: }
58:
59: /**
60: * Set whether merging should be enabled for this collection,
61: * in case of a 'parent' collection value being present.
62: */
63: public void setMergeEnabled(boolean mergeEnabled) {
64: this .mergeEnabled = mergeEnabled;
65: }
66:
67: public boolean isMergeEnabled() {
68: return this .mergeEnabled;
69: }
70:
71: public Object merge(Object parent) {
72: if (!this .mergeEnabled) {
73: throw new IllegalStateException(
74: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
75: }
76: if (parent == null) {
77: return this ;
78: }
79: if (!(parent instanceof Map)) {
80: throw new IllegalArgumentException(
81: "Cannot merge with object of type ["
82: + parent.getClass() + "]");
83: }
84: Map merged = new ManagedMap();
85: merged.putAll((Map) parent);
86: merged.putAll(this);
87: return merged;
88: }
89:
90: }
|