1 16 17 package org.springframework.beans; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.springframework.util.StringUtils; 26 27 37 public class MutablePropertyValues implements PropertyValues, Serializable { 38 39 40 private final List propertyValueList; 41 42 43 49 public MutablePropertyValues() { 50 this.propertyValueList = new ArrayList (); 51 } 52 53 60 public MutablePropertyValues(PropertyValues original) { 61 if (original != null) { 64 PropertyValue[] pvs = original.getPropertyValues(); 65 this.propertyValueList = new ArrayList (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 (0); 73 } 74 } 75 76 81 public MutablePropertyValues(Map original) { 82 if (original != null) { 85 this.propertyValueList = new ArrayList (original.size()); 86 Iterator it = original.entrySet().iterator(); 87 while (it.hasNext()) { 88 Map.Entry entry = (Map.Entry ) it.next(); 89 PropertyValue newPv = new PropertyValue((String ) entry.getKey(), entry.getValue()); 90 this.propertyValueList.add(newPv); 91 } 92 } 93 else { 94 this.propertyValueList = new ArrayList (0); 95 } 96 } 97 98 99 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 125 public MutablePropertyValues addPropertyValues(Map other) { 126 if (other != null) { 127 Iterator it = other.entrySet().iterator(); 128 while (it.hasNext()) { 129 Map.Entry entry = (Map.Entry ) it.next(); 130 PropertyValue newPv = new PropertyValue((String ) entry.getKey(), entry.getValue()); 131 addPropertyValue(newPv); 132 } 133 } 134 return this; 135 } 136 137 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 164 public void addPropertyValue(String propertyName, Object propertyValue) { 165 addPropertyValue(new PropertyValue(propertyName, propertyValue)); 166 } 167 168 172 public void setPropertyValueAt(PropertyValue pv, int i) { 173 this.propertyValueList.set(i, pv); 174 } 175 176 181 private PropertyValue mergeIfRequired(PropertyValue newPv, PropertyValue currentPv) { 182 Object value = newPv.getValue(); 183 if (value instanceof Mergeable) { 184 Mergeable mergeable = (Mergeable) value; 185 if (mergeable.isMergeEnabled()) { 186 Object merged = mergeable.merge(currentPv.getValue()); 187 return new PropertyValue(newPv.getName(), merged); 188 } 189 } 190 return newPv; 191 } 192 193 198 public void removePropertyValue(String propertyName) { 199 removePropertyValue(getPropertyValue(propertyName)); 200 } 201 202 206 public void removePropertyValue(PropertyValue pv) { 207 this.propertyValueList.remove(pv); 208 } 209 210 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 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 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 (Iterator it = this.propertyValueList.iterator(); it.hasNext();) { 253 PropertyValue newPv = (PropertyValue) it.next(); 254 PropertyValue pvOld = old.getPropertyValue(newPv.getName()); 256 if (pvOld == null) { 257 changes.addPropertyValue(newPv); 258 } 259 else if (!pvOld.equals(newPv)) { 260 changes.addPropertyValue(newPv); 262 } 263 } 264 return changes; 265 } 266 267 268 public boolean equals(Object 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 toString() { 284 PropertyValue[] pvs = getPropertyValues(); 285 StringBuffer sb = new StringBuffer ("PropertyValues: length=" + pvs.length + "; "); 286 sb.append(StringUtils.arrayToDelimitedString(pvs, "; ")); 287 return sb.toString(); 288 } 289 290 } 291 | Popular Tags |