Spring框架之基础类—AttributeAccessorSupport抽象类

来源:互联网 发布:股票短线王软件 编辑:程序博客网 时间:2024/06/18 06:55

一、AttributeAccessorSupport简介

实现AttributeAccessor接口的抽象类,提供AttributeAccessor接口中所有方法的简单实现,用于被子类扩展。

二、AttributeAccessorSupport源码详解

@SuppressWarnings("serial")public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {    /**      * 缓存属性信息的Map结构     */    private final Map<String, Object> attributes = new LinkedHashMap<String, Object>(0);    /**      * 设置属性信息     */    @Override    public void setAttribute(String name, Object value) {        Assert.notNull(name, "Name must not be null");        if (value != null) {            this.attributes.put(name, value);        }        else {            removeAttribute(name);        }    }    /**      * 获取属性信息     */    @Override    public Object getAttribute(String name) {        Assert.notNull(name, "Name must not be null");        return this.attributes.get(name);    }    /**      * 删除属性信息     */    @Override    public Object removeAttribute(String name) {        Assert.notNull(name, "Name must not be null");        return this.attributes.remove(name);    }    /**      * 判断是否包含属性信息     */    @Override    public boolean hasAttribute(String name) {        Assert.notNull(name, "Name must not be null");        return this.attributes.containsKey(name);    }    /**      * 获取所有的属性名     */    @Override    public String[] attributeNames() {        return this.attributes.keySet().toArray(new String[this.attributes.size()]);    }    /**     * 从其他属性访问器中的属性所属对象中获取属性信息     * @param source the AttributeAccessor to copy from     */    protected void copyAttributesFrom(AttributeAccessor source) {        Assert.notNull(source, "Source must not be null");        String[] attributeNames = source.attributeNames();        for (String attributeName : attributeNames) {            setAttribute(attributeName, source.getAttribute(attributeName));        }    }    @Override    public boolean equals(Object other) {        if (this == other) {            return true;        }        if (!(other instanceof AttributeAccessorSupport)) {            return false;        }        AttributeAccessorSupport that = (AttributeAccessorSupport) other;        return this.attributes.equals(that.attributes);    }    @Override    public int hashCode() {        return this.attributes.hashCode();    }}
原创粉丝点击