1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.beans;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.springframework.util.StringUtils;
27
28
29
30
31
32
33
34
35
36
37
38 @SuppressWarnings("serial")
39 public class MutablePropertyValues implements PropertyValues, Serializable {
40
41 private final List<PropertyValue> propertyValueList;
42
43 private Set<String> processedProperties;
44
45 private volatile boolean converted = false;
46
47
48
49
50
51
52
53 public MutablePropertyValues() {
54 this.propertyValueList = new ArrayList<PropertyValue>(0);
55 }
56
57
58
59
60
61
62
63
64 public MutablePropertyValues(PropertyValues original) {
65
66
67 if (original != null) {
68 PropertyValue[] pvs = original.getPropertyValues();
69 this.propertyValueList = new ArrayList<PropertyValue>(pvs.length);
70 for (PropertyValue pv : pvs) {
71 this.propertyValueList.add(new PropertyValue(pv));
72 }
73 }
74 else {
75 this.propertyValueList = new ArrayList<PropertyValue>(0);
76 }
77 }
78
79
80
81
82
83
84 public MutablePropertyValues(Map<?, ?> original) {
85
86
87 if (original != null) {
88 this.propertyValueList = new ArrayList<PropertyValue>(original.size());
89 for (Map.Entry<?, ?> entry : original.entrySet()) {
90 this.propertyValueList.add(new PropertyValue(entry.getKey().toString(), entry.getValue()));
91 }
92 }
93 else {
94 this.propertyValueList = new ArrayList<PropertyValue>(0);
95 }
96 }
97
98
99
100
101
102
103
104
105 public MutablePropertyValues(List<PropertyValue> propertyValueList) {
106 this.propertyValueList =
107 (propertyValueList != null ? propertyValueList : new ArrayList<PropertyValue>());
108 }
109
110
111
112
113
114
115
116
117 public List<PropertyValue> getPropertyValueList() {
118 return this.propertyValueList;
119 }
120
121
122
123
124 public int size() {
125 return this.propertyValueList.size();
126 }
127
128
129
130
131
132
133
134
135 public MutablePropertyValues addPropertyValues(PropertyValues other) {
136 if (other != null) {
137 PropertyValue[] pvs = other.getPropertyValues();
138 for (PropertyValue pv : pvs) {
139 addPropertyValue(new PropertyValue(pv));
140 }
141 }
142 return this;
143 }
144
145
146
147
148
149
150
151 public MutablePropertyValues addPropertyValues(Map<?, ?> other) {
152 if (other != null) {
153 for (Map.Entry<?, ?> entry : other.entrySet()) {
154 addPropertyValue(new PropertyValue(entry.getKey().toString(), entry.getValue()));
155 }
156 }
157 return this;
158 }
159
160
161
162
163
164
165
166 public MutablePropertyValues addPropertyValue(PropertyValue pv) {
167 for (int i = 0; i < this.propertyValueList.size(); i++) {
168 PropertyValue currentPv = this.propertyValueList.get(i);
169 if (currentPv.getName().equals(pv.getName())) {
170 pv = mergeIfRequired(pv, currentPv);
171 setPropertyValueAt(pv, i);
172 return this;
173 }
174 }
175 this.propertyValueList.add(pv);
176 return this;
177 }
178
179
180
181
182
183
184
185
186
187
188 public void addPropertyValue(String propertyName, Object propertyValue) {
189 addPropertyValue(new PropertyValue(propertyName, propertyValue));
190 }
191
192
193
194
195
196
197
198
199 public MutablePropertyValues add(String propertyName, Object propertyValue) {
200 addPropertyValue(new PropertyValue(propertyName, propertyValue));
201 return this;
202 }
203
204
205
206
207
208 public void setPropertyValueAt(PropertyValue pv, int i) {
209 this.propertyValueList.set(i, pv);
210 }
211
212
213
214
215
216
217 private PropertyValue mergeIfRequired(PropertyValue newPv, PropertyValue currentPv) {
218 Object value = newPv.getValue();
219 if (value instanceof Mergeable) {
220 Mergeable mergeable = (Mergeable) value;
221 if (mergeable.isMergeEnabled()) {
222 Object merged = mergeable.merge(currentPv.getValue());
223 return new PropertyValue(newPv.getName(), merged);
224 }
225 }
226 return newPv;
227 }
228
229
230
231
232
233 public void removePropertyValue(PropertyValue pv) {
234 this.propertyValueList.remove(pv);
235 }
236
237
238
239
240
241
242 public void removePropertyValue(String propertyName) {
243 this.propertyValueList.remove(getPropertyValue(propertyName));
244 }
245
246
247 @Override
248 public PropertyValue[] getPropertyValues() {
249 return this.propertyValueList.toArray(new PropertyValue[this.propertyValueList.size()]);
250 }
251
252 @Override
253 public PropertyValue getPropertyValue(String propertyName) {
254 for (PropertyValue pv : this.propertyValueList) {
255 if (pv.getName().equals(propertyName)) {
256 return pv;
257 }
258 }
259 return null;
260 }
261
262
263
264
265
266
267
268
269
270 public Object get(String propertyName) {
271 PropertyValue pv = getPropertyValue(propertyName);
272 return (pv != null ? pv.getValue() : null);
273 }
274
275 @Override
276 public PropertyValues changesSince(PropertyValues old) {
277 MutablePropertyValues changes = new MutablePropertyValues();
278 if (old == this) {
279 return changes;
280 }
281
282
283 for (PropertyValue newPv : this.propertyValueList) {
284
285 PropertyValue pvOld = old.getPropertyValue(newPv.getName());
286 if (pvOld == null) {
287 changes.addPropertyValue(newPv);
288 }
289 else if (!pvOld.equals(newPv)) {
290
291 changes.addPropertyValue(newPv);
292 }
293 }
294 return changes;
295 }
296
297 @Override
298 public boolean contains(String propertyName) {
299 return (getPropertyValue(propertyName) != null ||
300 (this.processedProperties != null && this.processedProperties.contains(propertyName)));
301 }
302
303 @Override
304 public boolean isEmpty() {
305 return this.propertyValueList.isEmpty();
306 }
307
308
309
310
311
312
313
314
315
316
317 public void registerProcessedProperty(String propertyName) {
318 if (this.processedProperties == null) {
319 this.processedProperties = new HashSet<String>();
320 }
321 this.processedProperties.add(propertyName);
322 }
323
324
325
326
327
328 public void clearProcessedProperty(String propertyName) {
329 if (this.processedProperties != null) {
330 this.processedProperties.remove(propertyName);
331 }
332 }
333
334
335
336
337
338 public void setConverted() {
339 this.converted = true;
340 }
341
342
343
344
345
346 public boolean isConverted() {
347 return this.converted;
348 }
349
350
351 @Override
352 public boolean equals(Object other) {
353 if (this == other) {
354 return true;
355 }
356 if (!(other instanceof MutablePropertyValues)) {
357 return false;
358 }
359 MutablePropertyValues that = (MutablePropertyValues) other;
360 return this.propertyValueList.equals(that.propertyValueList);
361 }
362
363 @Override
364 public int hashCode() {
365 return this.propertyValueList.hashCode();
366 }
367
368 @Override
369 public String toString() {
370 PropertyValue[] pvs = getPropertyValues();
371 StringBuilder sb = new StringBuilder("PropertyValues: length=").append(pvs.length);
372 if (pvs.length > 0) {
373 sb.append("; ").append(StringUtils.arrayToDelimitedString(pvs, "; "));
374 }
375 return sb.toString();
376 }
377
378 }