View Javadoc
1   /*
2    * Copyright 2013 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  
17  package io.netty.util;
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /**
25   * A pool of {@link Constant}s.
26   *
27   * @param <T> the type of the constant
28   */
29  public abstract class ConstantPool<T extends Constant<T>> {
30  
31      private final Map<String, T> constants = new HashMap<String, T>();
32  
33      private int nextId = 1;
34  
35      /**
36       * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
37       */
38      public T valueOf(Class<?> firstNameComponent, String secondNameComponent) {
39          if (firstNameComponent == null) {
40              throw new NullPointerException("firstNameComponent");
41          }
42          if (secondNameComponent == null) {
43              throw new NullPointerException("secondNameComponent");
44          }
45  
46          return valueOf(firstNameComponent.getName() + '#' + secondNameComponent);
47      }
48  
49      /**
50       * Returns the {@link Constant} which is assigned to the specified {@code name}.
51       * If there's no such {@link Constant}, a new one will be created and returned.
52       * Once created, the subsequent calls with the same {@code name} will always return the previously created one
53       * (i.e. singleton.)
54       *
55       * @param name the name of the {@link Constant}
56       */
57      public T valueOf(String name) {
58          if (name == null) {
59              throw new NullPointerException("name");
60          }
61  
62          if (name.isEmpty()) {
63              throw new IllegalArgumentException("empty name");
64          }
65  
66          synchronized (constants) {
67              T c = constants.get(name);
68              if (c == null) {
69                  c = newConstant(nextId, name);
70                  constants.put(name, c);
71                  nextId ++;
72              }
73  
74              return c;
75          }
76      }
77  
78      /**
79       * Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
80       */
81      public boolean exists(String name) {
82          ObjectUtil.checkNotNull(name, "name");
83          synchronized (constants) {
84              return constants.containsKey(name);
85          }
86      }
87  
88      /**
89       * Creates a new {@link Constant} for the given {@param name} or fail with an
90       * {@link IllegalArgumentException} if a {@link Constant} for the given {@param name} exists.
91       */
92      @SuppressWarnings("unchecked")
93      public T newInstance(String name) {
94          if (name == null) {
95              throw new NullPointerException("name");
96          }
97  
98          if (name.isEmpty()) {
99              throw new IllegalArgumentException("empty name");
100         }
101 
102         synchronized (constants) {
103             T c = constants.get(name);
104             if (c == null) {
105                 c = newConstant(nextId, name);
106                 constants.put(name, c);
107                 nextId ++;
108             } else {
109                 throw new IllegalArgumentException(String.format("'%s' is already in use", name));
110             }
111             return c;
112         }
113     }
114 
115     protected abstract T newConstant(int id, String name);
116 }