001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.core;
018:
019: import java.io.Serializable;
020: import java.util.LinkedHashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.springframework.util.Assert;
025:
026: /**
027: * Support class for {@link AttributeAccessor AttributeAccessors}, providing
028: * a base implementation of all methods. To be extended by subclasses.
029: *
030: * <p>{@link Serializable} if subclasses and all attribute values are {@link Serializable}.
031: *
032: * @author Rob Harrop
033: * @author Juergen Hoeller
034: * @since 2.0
035: */
036: public abstract class AttributeAccessorSupport implements
037: AttributeAccessor, Serializable {
038:
039: /** Map with String keys and Object values */
040: private final Map attributes = new LinkedHashMap();
041:
042: public void setAttribute(String name, Object value) {
043: Assert.notNull(name, "Name must not be null");
044: if (value != null) {
045: this .attributes.put(name, value);
046: } else {
047: removeAttribute(name);
048: }
049: }
050:
051: public Object getAttribute(String name) {
052: Assert.notNull(name, "Name must not be null");
053: return this .attributes.get(name);
054: }
055:
056: public Object removeAttribute(String name) {
057: Assert.notNull(name, "Name must not be null");
058: return this .attributes.remove(name);
059: }
060:
061: public boolean hasAttribute(String name) {
062: Assert.notNull(name, "Name must not be null");
063: return this .attributes.containsKey(name);
064: }
065:
066: public String[] attributeNames() {
067: Set attributeNames = this .attributes.keySet();
068: return (String[]) attributeNames
069: .toArray(new String[attributeNames.size()]);
070: }
071:
072: /**
073: * Copy the attributes from the supplied AttributeAccessor to this accessor.
074: * @param source the AttributeAccessor to copy from
075: */
076: protected void copyAttributesFrom(AttributeAccessor source) {
077: Assert.notNull(source, "Source must not be null");
078: String[] attributeNames = source.attributeNames();
079: for (int i = 0; i < attributeNames.length; i++) {
080: String attributeName = attributeNames[i];
081: setAttribute(attributeName, source
082: .getAttribute(attributeName));
083: }
084: }
085:
086: public boolean equals(Object other) {
087: if (this == other) {
088: return true;
089: }
090: if (!(other instanceof AttributeAccessorSupport)) {
091: return false;
092: }
093: AttributeAccessorSupport that = (AttributeAccessorSupport) other;
094: return this .attributes.equals(that.attributes);
095: }
096:
097: public int hashCode() {
098: return this.attributes.hashCode();
099: }
100:
101: }
|