KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > ManagedMap


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.Collection JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.springframework.beans.BeanMetadataElement;
24 import org.springframework.beans.Mergeable;
25 import org.springframework.core.CollectionFactory;
26
27 /**
28  * Tag collection class used to hold managed Map values, which may
29  * include runtime bean references (to be resolved into bean objects).
30  *
31  * <p>Wraps a target Map, which will be a linked map if possible
32  * (that is, if running on JDK 1.4 or if Commons Collections 3.x is available).
33  *
34  * @author Juergen Hoeller
35  * @author Rob Harrop
36  * @since 27.05.2003
37  * @see org.springframework.core.CollectionFactory#createLinkedMapIfPossible
38  */

39 public class ManagedMap implements Map JavaDoc, Mergeable, BeanMetadataElement {
40
41     private final Map JavaDoc targetMap;
42
43     private Object JavaDoc source;
44
45     private boolean mergeEnabled;
46
47
48     public ManagedMap() {
49         this(16);
50     }
51
52     public ManagedMap(int initialCapacity) {
53         this.targetMap = CollectionFactory.createLinkedMapIfPossible(initialCapacity);
54     }
55
56     public ManagedMap(Map JavaDoc targetMap) {
57         this.targetMap = targetMap;
58     }
59
60
61     /**
62      * Set the configuration source <code>Object</code> for this metadata element.
63      * <p>The exact type of the object will depend on the configuration mechanism used.
64      */

65     public void setSource(Object JavaDoc source) {
66         this.source = source;
67     }
68
69     public Object JavaDoc getSource() {
70         return this.source;
71     }
72
73     /**
74      * Set whether merging should be enabled for this collection,
75      * in case of a 'parent' collection value being present.
76      */

77     public void setMergeEnabled(boolean mergeEnabled) {
78         this.mergeEnabled = mergeEnabled;
79     }
80
81     public boolean isMergeEnabled() {
82         return this.mergeEnabled;
83     }
84
85     public Object JavaDoc merge(Object JavaDoc parent) {
86         if (!this.mergeEnabled) {
87             throw new IllegalStateException JavaDoc("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
88         }
89         if (parent == null) {
90             return this;
91         }
92         if (!(parent instanceof Map JavaDoc)) {
93             throw new IllegalArgumentException JavaDoc("Cannot merge with object of type [" + parent.getClass() + "]");
94         }
95         Map JavaDoc merged = new ManagedMap();
96         merged.putAll((Map JavaDoc) parent);
97         merged.putAll(this);
98         return merged;
99     }
100
101
102     public int size() {
103         return this.targetMap.size();
104     }
105
106     public boolean isEmpty() {
107         return this.targetMap.isEmpty();
108     }
109
110     public boolean containsKey(Object JavaDoc key) {
111         return this.targetMap.containsKey(key);
112     }
113
114     public boolean containsValue(Object JavaDoc value) {
115         return this.targetMap.containsValue(value);
116     }
117
118     public Object JavaDoc get(Object JavaDoc key) {
119         return this.targetMap.get(key);
120     }
121
122     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
123         return this.targetMap.put(key, value);
124     }
125
126     public Object JavaDoc remove(Object JavaDoc key) {
127         return this.targetMap.remove(key);
128     }
129
130     public void putAll(Map JavaDoc t) {
131         this.targetMap.putAll(t);
132     }
133
134     public void clear() {
135         this.targetMap.clear();
136     }
137
138     public Set JavaDoc keySet() {
139         return this.targetMap.keySet();
140     }
141
142     public Collection JavaDoc values() {
143         return this.targetMap.values();
144     }
145
146     public Set JavaDoc entrySet() {
147         return this.targetMap.entrySet();
148     }
149
150     public int hashCode() {
151         return this.targetMap.hashCode();
152     }
153
154     public boolean equals(Object JavaDoc obj) {
155         return this.targetMap.equals(obj);
156     }
157
158     public String JavaDoc toString() {
159         return this.targetMap.toString();
160     }
161
162 }
163
Popular Tags