001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.core.session;
021
022import java.io.Serializable;
023
024/**
025 * Creates a Key from a class name and an attribute name. The resulting Key will
026 * be stored in the session Map.<br>
027 * For instance, we can create a 'processor' AttributeKey this way :
028 * 
029 * <pre>
030 * private static final AttributeKey PROCESSOR = new AttributeKey(
031 *      SimpleIoProcessorPool.class, &quot;processor&quot;);
032 * </pre>
033 * 
034 * This will create the <b>SimpleIoProcessorPool.processor@7DE45C99</b> key
035 * which will be stored in the session map.<br>
036 * Such an attributeKey is mainly useful for debug purposes.
037 * 
038 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
039 */
040public final class AttributeKey implements Serializable {
041    /** The serial version UID */
042    private static final long serialVersionUID = -583377473376683096L;
043
044    /** The attribute's name */
045    private final String name;
046
047    /**
048     * Creates a new instance. It's built from :
049     * <ul>
050     * <li>the class' name</li>
051     * <li>the attribute's name</li>
052     * <li>this attribute hashCode</li>
053     * </ul>
054     * 
055     * @param source The class this AttributeKey will be attached to
056     * @param name The Attribute name
057     */
058    public AttributeKey(Class<?> source, String name) {
059        this.name = source.getName() + '.' + name + '@' + Integer.toHexString(this.hashCode());
060    }
061
062    /**
063     * The String representation of this object.
064     */
065    @Override
066    public String toString() {
067        return name;
068    }
069
070    @Override
071    public int hashCode() {
072        int h = 17 * 37 + ((name == null) ? 0 : name.hashCode());
073        return h;
074    }
075
076    @Override
077    public boolean equals(Object obj) {
078        if (this == obj) {
079            return true;
080        }
081
082        if (!(obj instanceof AttributeKey)) {
083            return false;
084        }
085
086        AttributeKey other = (AttributeKey) obj;
087
088        return name.equals(other.name);
089    }
090}