1 16 package org.springframework.webflow.core.collection; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.springframework.binding.collection.MapAccessor; 27 import org.springframework.core.style.StylerUtils; 28 import org.springframework.util.Assert; 29 30 35 public class LocalAttributeMap implements MutableAttributeMap, Serializable { 36 37 40 private Map attributes; 41 42 46 private transient MapAccessor attributeAccessor; 47 48 51 public LocalAttributeMap() { 52 initAttributes(createTargetMap()); 53 } 54 55 60 public LocalAttributeMap(int size, int loadFactor) { 61 initAttributes(createTargetMap(size, loadFactor)); 62 } 63 64 67 public LocalAttributeMap(String attributeName, Object attributeValue) { 68 initAttributes(createTargetMap(1, 1)); 69 put(attributeName, attributeValue); 70 } 71 72 75 public LocalAttributeMap(Map map) { 76 Assert.notNull(map, "The target map is required"); 77 initAttributes(map); 78 } 79 80 82 public Map asMap() { 83 return attributeAccessor.asMap(); 84 } 85 86 public int size() { 87 return attributes.size(); 88 } 89 90 public Object get(String attributeName) { 91 return attributes.get(attributeName); 92 } 93 94 public boolean isEmpty() { 95 return attributes.isEmpty(); 96 } 97 98 public boolean contains(String attributeName) { 99 return attributes.containsKey(attributeName); 100 } 101 102 public boolean contains(String attributeName, Class requiredType) throws IllegalArgumentException { 103 return attributeAccessor.containsKey(attributeName, requiredType); 104 } 105 106 public Object get(String attributeName, Object defaultValue) { 107 return attributeAccessor.get(attributeName, defaultValue); 108 } 109 110 public Object get(String attributeName, Class requiredType) throws IllegalArgumentException { 111 return attributeAccessor.get(attributeName, requiredType); 112 } 113 114 public Object get(String attributeName, Class requiredType, Object defaultValue) throws IllegalStateException { 115 return attributeAccessor.get(attributeName, requiredType, defaultValue); 116 } 117 118 public Object getRequired(String attributeName) throws IllegalArgumentException { 119 return attributeAccessor.getRequired(attributeName); 120 } 121 122 public Object getRequired(String attributeName, Class requiredType) throws IllegalArgumentException { 123 return attributeAccessor.getRequired(attributeName, requiredType); 124 } 125 126 public String getString(String attributeName) throws IllegalArgumentException { 127 return attributeAccessor.getString(attributeName); 128 } 129 130 public String getString(String attributeName, String defaultValue) throws IllegalArgumentException { 131 return attributeAccessor.getString(attributeName, defaultValue); 132 } 133 134 public String getRequiredString(String attributeName) throws IllegalArgumentException { 135 return attributeAccessor.getRequiredString(attributeName); 136 } 137 138 public Collection getCollection(String attributeName) throws IllegalArgumentException { 139 return attributeAccessor.getCollection(attributeName); 140 } 141 142 public Collection getCollection(String attributeName, Class requiredType) throws IllegalArgumentException { 143 return attributeAccessor.getCollection(attributeName, requiredType); 144 } 145 146 public Collection getRequiredCollection(String attributeName) throws IllegalArgumentException { 147 return attributeAccessor.getRequiredCollection(attributeName); 148 } 149 150 public Collection getRequiredCollection(String attributeName, Class requiredType) throws IllegalArgumentException { 151 return attributeAccessor.getRequiredCollection(attributeName, requiredType); 152 } 153 154 public Object [] getArray(String attributeName, Class requiredType) throws IllegalArgumentException { 155 return attributeAccessor.getArray(attributeName, requiredType); 156 } 157 158 public Object [] getRequiredArray(String attributeName, Class requiredType) throws IllegalArgumentException { 159 return attributeAccessor.getRequiredArray(attributeName, requiredType); 160 } 161 162 public Number getNumber(String attributeName, Class requiredType) throws IllegalArgumentException { 163 return attributeAccessor.getNumber(attributeName, requiredType); 164 } 165 166 public Number getNumber(String attributeName, Class requiredType, Number defaultValue) 167 throws IllegalArgumentException { 168 return attributeAccessor.getNumber(attributeName, requiredType, defaultValue); 169 } 170 171 public Number getRequiredNumber(String attributeName, Class requiredType) throws IllegalArgumentException { 172 return attributeAccessor.getRequiredNumber(attributeName, requiredType); 173 } 174 175 public Integer getInteger(String attributeName) throws IllegalArgumentException { 176 return attributeAccessor.getInteger(attributeName); 177 } 178 179 public Integer getInteger(String attributeName, Integer defaultValue) throws IllegalArgumentException { 180 return attributeAccessor.getInteger(attributeName, defaultValue); 181 } 182 183 public Integer getRequiredInteger(String attributeName) throws IllegalArgumentException { 184 return attributeAccessor.getRequiredInteger(attributeName); 185 } 186 187 public Long getLong(String attributeName) throws IllegalArgumentException { 188 return attributeAccessor.getLong(attributeName); 189 } 190 191 public Long getLong(String attributeName, Long defaultValue) throws IllegalArgumentException { 192 return attributeAccessor.getLong(attributeName, defaultValue); 193 } 194 195 public Long getRequiredLong(String attributeName) throws IllegalArgumentException { 196 return attributeAccessor.getRequiredLong(attributeName); 197 } 198 199 public Boolean getBoolean(String attributeName) throws IllegalArgumentException { 200 return attributeAccessor.getBoolean(attributeName); 201 } 202 203 public Boolean getBoolean(String attributeName, Boolean defaultValue) throws IllegalArgumentException { 204 return attributeAccessor.getBoolean(attributeName, defaultValue); 205 } 206 207 public Boolean getRequiredBoolean(String attributeName) throws IllegalArgumentException { 208 return attributeAccessor.getRequiredBoolean(attributeName); 209 } 210 211 public AttributeMap union(AttributeMap attributes) { 212 if (attributes == null) { 213 return new LocalAttributeMap(getMapInternal()); 214 } 215 else { 216 Map map = createTargetMap(); 217 map.putAll(getMapInternal()); 218 map.putAll(attributes.asMap()); 219 return new LocalAttributeMap(map); 220 } 221 } 222 223 225 public Object put(String attributeName, Object attributeValue) { 226 return getMapInternal().put(attributeName, attributeValue); 227 } 228 229 public MutableAttributeMap putAll(AttributeMap attributes) { 230 if (attributes == null) { 231 return this; 232 } 233 getMapInternal().putAll(attributes.asMap()); 234 return this; 235 } 236 237 public Object remove(String attributeName) { 238 return getMapInternal().remove(attributeName); 239 } 240 241 public MutableAttributeMap clear() throws UnsupportedOperationException { 242 getMapInternal().clear(); 243 return this; 244 } 245 246 public MutableAttributeMap replaceWith(AttributeMap attributes) throws UnsupportedOperationException { 247 clear(); 248 putAll(attributes); 249 return this; 250 } 251 252 254 258 protected void initAttributes(Map attributes) { 259 this.attributes = attributes; 260 attributeAccessor = new MapAccessor(this.attributes); 261 } 262 263 266 protected Map getMapInternal() { 267 return attributes; 268 } 269 270 272 277 protected Map createTargetMap() { 278 return new HashMap (); 279 } 280 281 288 protected Map createTargetMap(int size, int loadFactor) { 289 return new HashMap (size, loadFactor); 290 } 291 292 public boolean equals(Object o) { 293 if (!(o instanceof LocalAttributeMap)) { 294 return false; 295 } 296 LocalAttributeMap other = (LocalAttributeMap)o; 297 return getMapInternal().equals(other.getMapInternal()); 298 } 299 300 public int hashCode() { 301 return getMapInternal().hashCode(); 302 } 303 304 306 private void writeObject(ObjectOutputStream out) throws IOException { 307 out.defaultWriteObject(); 308 } 309 310 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 311 in.defaultReadObject(); 312 attributeAccessor = new MapAccessor(attributes); 313 } 314 315 public String toString() { 316 return StylerUtils.style(attributes); 317 } 318 } | Popular Tags |