KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > AttributeAccessorSupport


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.core;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.springframework.util.Assert;
25
26 /**
27  * Support class for {@link AttributeAccessor AttributeAccessors}, providing
28  * a base implementation of all methods. To be extended by subclasses.
29  *
30  * <p>{@link Serializable} if subclasses and all attribute values are {@link Serializable}.
31  *
32  * @author Rob Harrop
33  * @author Juergen Hoeller
34  * @since 2.0
35  */

36 public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable JavaDoc {
37
38     /** Map with String keys and Object values */
39     private final Map JavaDoc attributes = new HashMap JavaDoc();
40
41
42     public void setAttribute(String JavaDoc name, Object JavaDoc 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 JavaDoc getAttribute(String JavaDoc name) {
53         Assert.notNull(name, "Name must not be null");
54         return this.attributes.get(name);
55     }
56
57     public Object JavaDoc removeAttribute(String JavaDoc name) {
58         Assert.notNull(name, "Name must not be null");
59         return this.attributes.remove(name);
60     }
61
62     public boolean hasAttribute(String JavaDoc name) {
63         Assert.notNull(name, "Name must not be null");
64         return this.attributes.containsKey(name);
65     }
66
67     public String JavaDoc[] attributeNames() {
68         Set JavaDoc attributeNames = this.attributes.keySet();
69         return (String JavaDoc[]) attributeNames.toArray(new String JavaDoc[attributeNames.size()]);
70     }
71
72
73     /**
74      * Copy the attributes from the supplied AttributeAccessor to this accessor.
75      * @param source the AttributeAccessor to copy from
76      */

77     protected void copyAttributesFrom(AttributeAccessor source) {
78         Assert.notNull(source, "Source must not be null");
79         String JavaDoc[] attributeNames = source.attributeNames();
80         for (int i = 0; i < attributeNames.length; i++) {
81             String JavaDoc attributeName = attributeNames[i];
82             setAttribute(attributeName, source.getAttribute(attributeName));
83         }
84     }
85
86
87     public boolean equals(Object JavaDoc 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