查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.session;
21  
22  import java.io.Serializable;
23  
24  /**
25   * Creates a Key from a class name and an attribute name. The resulting Key will
26   * be stored in the session Map.<br>
27   * For instance, we can create a 'processor' AttributeKey this way :
28   * 
29   * <pre>
30   * private static final AttributeKey PROCESSOR = new AttributeKey(
31   * 	SimpleIoProcessorPool.class, &quot;processor&quot;);
32   * </pre>
33   * 
34   * This will create the <b>SimpleIoProcessorPool.processor@7DE45C99</b> key
35   * which will be stored in the session map.<br>
36   * Such an attributeKey is mainly useful for debug purposes.
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public final class AttributeKey implements Serializable {
41      /** The serial version UID */
42      private static final long serialVersionUID = -583377473376683096L;
43  
44      /** The attribute's name */
45      private final String name;
46  
47      /**
48       * Creates a new instance. It's built from :
49       * <ul>
50       * <li>the class' name</li>
51       * <li>the attribute's name</li>
52       * <li>this attribute hashCode</li>
53       * </ul>
54       * 
55       * @param source The class this AttributeKey will be attached to
56       * @param name The Attribute name
57       */
58      public AttributeKey(Class<?> source, String name) {
59          this.name = source.getName() + '.' + name + '@' + Integer.toHexString(this.hashCode());
60      }
61  
62      /**
63       * The String representation of this object.
64       */
65      @Override
66      public String toString() {
67          return name;
68      }
69  
70      @Override
71      public int hashCode() {
72          int h = 17 * 37 + ((name == null) ? 0 : name.hashCode());
73          return h;
74      }
75  
76      @Override
77      public boolean equals(Object obj) {
78          if (this == obj) {
79              return true;
80          }
81  
82          if (!(obj instanceof AttributeKey)) {
83              return false;
84          }
85  
86          AttributeKey other = (AttributeKey) obj;
87  
88          return name.equals(other.name);
89      }
90  }