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.session;
21
22 import static org.apache.mina.util.Assert.assertNotNull;
23
24 import org.apache.mina.api.IoSession;
25
26 /**
27 * Represents the Key for an attribute-value of an {@link IoSession}. A key
28 * consists of the Type of the referenced attribute value and a name.<br>
29 * <br>
30 * Two {@link AttributeKey}'s are equal if the have the same attribute-type and
31 * attribute-name.
32 *
33 * @param <T> Type of the attribute-value this key is referring to
34 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35 */
36 public final class AttributeKey<T> {
37 /** the {@link Class} of the referenced attribute-value */
38 private final Class<T> attributeType;
39
40 /** the name of this key */
41 private final String attributeName;
42
43 /** the cached hash code of this instance */
44 private final int hashCode;
45
46 /**
47 * Creates a new {@link AttributeKey} with the given parameters. A
48 * {@link IllegalArgumentException} will be thrown if any parameter is
49 * <code>null</code>.
50 *
51 * @param attributeType
52 * type of the referenced attribute-value, must not be
53 * <code>null</code>
54 * @param attributeName
55 * name of this key, must not be <code>null</code>
56 * @exception IllegalArgumentException
57 * if any parameter is <code>null</code>
58 * @see #createKey(Class, String)
59 */
60 public AttributeKey(Class<T> attributeType, String attributeName) {
61 this.attributeType = assertNotNull(attributeType, "attributeType");
62 this.attributeName = assertNotNull(attributeName, "attributeName");
63
64 this.hashCode = createHashCode();
65 }
66
67 /**
68 * Creates a new {@link AttributeKey} with the given parameters. A
69 * {@link IllegalArgumentException} will be thrown if any parameter is
70 * <code>null</code>. <br>
71 * This call is equal to {@link AttributeKey#AttributeKey(Class, String)}
72 *
73 * @param attributeType
74 * type of the referenced attribute-value, must not be
75 * <code>null</code>
76 * @param attributeName
77 * name of this key, must not be <code>null</code>
78 * @exception IllegalArgumentException
79 * if any parameter is <code>null</code>
80 * @see #AttributeKey(Class, String)
81 */
82 public static <T> AttributeKey<T> createKey(Class<T> attributeType, String attributeName) {
83 return new AttributeKey<T>(attributeType, attributeName);
84 }
85
86 /**
87 * Creates the hash code for this instance
88 *
89 * @return
90 */
91 private int createHashCode() {
92 final int prime = 31;
93 int result = prime + attributeName.hashCode();
94 result = prime * result + attributeType.hashCode();
95
96 return result;
97 }
98
99 /**
100 * Returns the name of this key.
101 *
102 * @return name of this key, never <code>null</code>
103 */
104 public String getName() {
105 return attributeName;
106 }
107
108 /**
109 * Returns the type of this key.
110 *
111 * @return type of this key, never <code>null</code>
112 */
113 public Class<T> getType() {
114 return attributeType;
115 }
116
117 @Override
118 public int hashCode() {
119 return hashCode;
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127
128 if (obj == null) {
129 return false;
130 }
131
132 if (getClass() != obj.getClass()) {
133 return false;
134 }
135
136 AttributeKey<?> other = (AttributeKey<?>) obj;
137
138 return hashCode == other.hashCode;
139 }
140 }