1 16 17 package org.springframework.core; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.springframework.util.Assert; 25 26 36 public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable { 37 38 39 private final Map attributes = new HashMap (); 40 41 42 public void setAttribute(String name, Object value) { 43 Assert.notNull(name, "Name must not be null"); 44 if (value != null) { 45 this.attributes.put(name, value); 46 } 47 else { 48 removeAttribute(name); 49 } 50 } 51 52 public Object getAttribute(String name) { 53 Assert.notNull(name, "Name must not be null"); 54 return this.attributes.get(name); 55 } 56 57 public Object removeAttribute(String name) { 58 Assert.notNull(name, "Name must not be null"); 59 return this.attributes.remove(name); 60 } 61 62 public boolean hasAttribute(String name) { 63 Assert.notNull(name, "Name must not be null"); 64 return this.attributes.containsKey(name); 65 } 66 67 public String [] attributeNames() { 68 Set attributeNames = this.attributes.keySet(); 69 return (String []) attributeNames.toArray(new String [attributeNames.size()]); 70 } 71 72 73 77 protected void copyAttributesFrom(AttributeAccessor source) { 78 Assert.notNull(source, "Source must not be null"); 79 String [] attributeNames = source.attributeNames(); 80 for (int i = 0; i < attributeNames.length; i++) { 81 String attributeName = attributeNames[i]; 82 setAttribute(attributeName, source.getAttribute(attributeName)); 83 } 84 } 85 86 87 public boolean equals(Object other) { 88 if (this == other) { 89 return true; 90 } 91 if (!(other instanceof AttributeAccessorSupport)) { 92 return false; 93 } 94 AttributeAccessorSupport that = (AttributeAccessorSupport) other; 95 return this.attributes.equals(that.attributes); 96 } 97 98 public int hashCode() { 99 return this.attributes.hashCode(); 100 } 101 102 } 103 | Popular Tags |