View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  /**
19   * Key which can be used to access {@link Attribute} out of the {@link AttributeMap}. Be aware that it is not be
20   * possible to have multiple keys with the same name.
21   *
22   * @param <T>   the type of the {@link Attribute} which can be accessed via this {@link AttributeKey}.
23   */
24  @SuppressWarnings("UnusedDeclaration") // 'T' is used only at compile time
25  public final class AttributeKey<T> extends AbstractConstant<AttributeKey<T>> {
26  
27      private static final ConstantPool<AttributeKey<Object>> pool = new ConstantPool<AttributeKey<Object>>() {
28          @Override
29          protected AttributeKey<Object> newConstant(int id, String name) {
30              return new AttributeKey<Object>(id, name);
31          }
32      };
33  
34      /**
35       * Returns the singleton instance of the {@link AttributeKey} which has the specified {@code name}.
36       */
37      @SuppressWarnings("unchecked")
38      public static <T> AttributeKey<T> valueOf(String name) {
39          return (AttributeKey<T>) pool.valueOf(name);
40      }
41  
42      /**
43       * Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
44       */
45      public static boolean exists(String name) {
46          return pool.exists(name);
47      }
48  
49      /**
50       * Creates a new {@link AttributeKey} for the given {@param name} or fail with an
51       * {@link IllegalArgumentException} if a {@link AttributeKey} for the given {@param name} exists.
52       */
53      @SuppressWarnings("unchecked")
54      public static <T> AttributeKey<T> newInstance(String name) {
55          return (AttributeKey<T>) pool.newInstance(name);
56      }
57  
58      @SuppressWarnings("unchecked")
59      public static <T> AttributeKey<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
60          return (AttributeKey<T>) pool.valueOf(firstNameComponent, secondNameComponent);
61      }
62  
63      private AttributeKey(int id, String name) {
64          super(id, name);
65      }
66  }