KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > MutablePropertyValues


1 /*
2  * Copyright 2002-2006 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;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.springframework.util.StringUtils;
26
27 /**
28  * Default implementation of the PropertyValues interface.
29  * Allows simple manipulation of properties, and provides constructors
30  * to support deep copy and construction from a Map.
31  *
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  * @author Rob Harrop
35  * @since 13 May 2001
36  */

37 public class MutablePropertyValues implements PropertyValues, Serializable JavaDoc {
38
39     /** List of PropertyValue objects */
40     private final List JavaDoc propertyValueList;
41
42
43     /**
44      * Creates a new empty MutablePropertyValues object.
45      * Property values can be added with the <code>addPropertyValue</code> methods.
46      * @see #addPropertyValue(PropertyValue)
47      * @see #addPropertyValue(String, Object)
48      */

49     public MutablePropertyValues() {
50         this.propertyValueList = new ArrayList JavaDoc();
51     }
52
53     /**
54      * Deep copy constructor. Guarantees PropertyValue references
55      * are independent, although it can't deep copy objects currently
56      * referenced by individual PropertyValue objects.
57      * @param original the PropertyValues to copy
58      * @see #addPropertyValues(PropertyValues)
59      */

60     public MutablePropertyValues(PropertyValues original) {
61         // We can optimize this because it's all new:
62
// There is no replacement of existing property values.
63
if (original != null) {
64             PropertyValue[] pvs = original.getPropertyValues();
65             this.propertyValueList = new ArrayList JavaDoc(pvs.length);
66             for (int i = 0; i < pvs.length; i++) {
67                 PropertyValue newPv = new PropertyValue(pvs[i]);
68                 this.propertyValueList.add(newPv);
69             }
70         }
71         else {
72             this.propertyValueList = new ArrayList JavaDoc(0);
73         }
74     }
75
76     /**
77      * Construct a new PropertyValues object from a Map.
78      * @param original Map with property values keyed by property name Strings
79      * @see #addPropertyValues(Map)
80      */

81     public MutablePropertyValues(Map JavaDoc original) {
82         // We can optimize this because it's all new:
83
// There is no replacement of existing property values.
84
if (original != null) {
85             this.propertyValueList = new ArrayList JavaDoc(original.size());
86             Iterator JavaDoc it = original.entrySet().iterator();
87             while (it.hasNext()) {
88                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
89                 PropertyValue newPv = new PropertyValue((String JavaDoc) entry.getKey(), entry.getValue());
90                 this.propertyValueList.add(newPv);
91             }
92         }
93         else {
94             this.propertyValueList = new ArrayList JavaDoc(0);
95         }
96     }
97
98
99     /**
100      * Copy all given PropertyValues into this object. Guarantees PropertyValue
101      * references are independent, although it can't deep copy objects currently
102      * referenced by individual PropertyValue objects.
103      * @param other the PropertyValues to copy
104      * @return this object to allow creating objects, adding multiple PropertyValues
105      * in a single statement
106      */

107     public MutablePropertyValues addPropertyValues(PropertyValues other) {
108         if (other != null) {
109             PropertyValue[] pvs = other.getPropertyValues();
110             for (int i = 0; i < pvs.length; i++) {
111                 PropertyValue newPv = new PropertyValue(pvs[i]);
112                 addPropertyValue(newPv);
113             }
114         }
115         return this;
116     }
117
118     /**
119      * Add all property values from the given Map.
120      * @param other Map with property values keyed by property name,
121      * which must be a String
122      * @return this object to allow creating objects, adding multiple
123      * PropertyValues in a single statement
124      */

125     public MutablePropertyValues addPropertyValues(Map JavaDoc other) {
126         if (other != null) {
127             Iterator JavaDoc it = other.entrySet().iterator();
128             while (it.hasNext()) {
129                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
130                 PropertyValue newPv = new PropertyValue((String JavaDoc) entry.getKey(), entry.getValue());
131                 addPropertyValue(newPv);
132             }
133         }
134         return this;
135     }
136
137     /**
138      * Add a PropertyValue object, replacing any existing one
139      * for the corresponding property.
140      * @param pv PropertyValue object to add
141      * @return this object to allow creating objects, adding multiple
142      * PropertyValues in a single statement
143      */

144     public MutablePropertyValues addPropertyValue(PropertyValue pv) {
145         for (int i = 0; i < this.propertyValueList.size(); i++) {
146             PropertyValue currentPv = (PropertyValue) this.propertyValueList.get(i);
147             if (currentPv.getName().equals(pv.getName())) {
148                 pv = mergeIfRequired(pv, currentPv);
149                 setPropertyValueAt(pv, i);
150                 return this;
151             }
152         }
153         this.propertyValueList.add(pv);
154         return this;
155     }
156
157     /**
158      * Overloaded version of <code>addPropertyValue</code> that takes
159      * a property name and a property value.
160      * @param propertyName name of the property
161      * @param propertyValue value of the property
162      * @see #addPropertyValue(PropertyValue)
163      */

164     public void addPropertyValue(String JavaDoc propertyName, Object JavaDoc propertyValue) {
165         addPropertyValue(new PropertyValue(propertyName, propertyValue));
166     }
167
168     /**
169      * Modify a PropertyValue object held in this object.
170      * Indexed from 0.
171      */

172     public void setPropertyValueAt(PropertyValue pv, int i) {
173         this.propertyValueList.set(i, pv);
174     }
175
176     /**
177      * Merges the value of the supplied 'new' {@link PropertyValue} with that of
178      * the current {@link PropertyValue} if merging is supported and enabled.
179      * @see Mergeable
180      */

181     private PropertyValue mergeIfRequired(PropertyValue newPv, PropertyValue currentPv) {
182         Object JavaDoc value = newPv.getValue();
183         if (value instanceof Mergeable) {
184             Mergeable mergeable = (Mergeable) value;
185             if (mergeable.isMergeEnabled()) {
186                 Object JavaDoc merged = mergeable.merge(currentPv.getValue());
187                 return new PropertyValue(newPv.getName(), merged);
188             }
189         }
190         return newPv;
191     }
192
193     /**
194      * Overloaded version of <code>removePropertyValue</code> that takes a property name.
195      * @param propertyName name of the property
196      * @see #removePropertyValue(PropertyValue)
197      */

198     public void removePropertyValue(String JavaDoc propertyName) {
199         removePropertyValue(getPropertyValue(propertyName));
200     }
201
202     /**
203      * Remove the given PropertyValue, if contained.
204      * @param pv the PropertyValue to remove
205      */

206     public void removePropertyValue(PropertyValue pv) {
207         this.propertyValueList.remove(pv);
208     }
209
210     /**
211      * Clear this holder, removing all PropertyValues.
212      */

213     public void clear() {
214         this.propertyValueList.clear();
215     }
216
217
218     public PropertyValue[] getPropertyValues() {
219         return (PropertyValue[])
220                 this.propertyValueList.toArray(new PropertyValue[this.propertyValueList.size()]);
221     }
222
223     public PropertyValue getPropertyValue(String JavaDoc propertyName) {
224         for (int i = 0; i < this.propertyValueList.size(); i++) {
225             PropertyValue pv = (PropertyValue) propertyValueList.get(i);
226             if (pv.getName().equals(propertyName)) {
227                 return pv;
228             }
229         }
230         return null;
231     }
232
233     public boolean contains(String JavaDoc propertyName) {
234         return (getPropertyValue(propertyName) != null);
235     }
236
237     public boolean isEmpty() {
238         return this.propertyValueList.isEmpty();
239     }
240
241     public int size() {
242         return this.propertyValueList.size();
243     }
244
245     public PropertyValues changesSince(PropertyValues old) {
246         MutablePropertyValues changes = new MutablePropertyValues();
247         if (old == this) {
248             return changes;
249         }
250
251         // for each property value in the new set
252
for (Iterator JavaDoc it = this.propertyValueList.iterator(); it.hasNext();) {
253             PropertyValue newPv = (PropertyValue) it.next();
254             // if there wasn't an old one, add it
255
PropertyValue pvOld = old.getPropertyValue(newPv.getName());
256             if (pvOld == null) {
257                 changes.addPropertyValue(newPv);
258             }
259             else if (!pvOld.equals(newPv)) {
260                 // it's changed
261
changes.addPropertyValue(newPv);
262             }
263         }
264         return changes;
265     }
266
267
268     public boolean equals(Object JavaDoc other) {
269         if (this == other) {
270             return true;
271         }
272         if (!(other instanceof MutablePropertyValues)) {
273             return false;
274         }
275         MutablePropertyValues that = (MutablePropertyValues) other;
276         return this.propertyValueList.equals(that.propertyValueList);
277     }
278
279     public int hashCode() {
280         return this.propertyValueList.hashCode();
281     }
282
283     public String JavaDoc toString() {
284         PropertyValue[] pvs = getPropertyValues();
285         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("PropertyValues: length=" + pvs.length + "; ");
286         sb.append(StringUtils.arrayToDelimitedString(pvs, "; "));
287         return sb.toString();
288     }
289
290 }
291
Popular Tags