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    *   https://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 static io.netty.util.internal.ObjectUtil.checkNotNull;
20  import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
21  
22  import io.netty.util.internal.PlatformDependent;
23  
24  import java.util.concurrent.ConcurrentMap;
25  import java.util.concurrent.atomic.AtomicInteger;
26  
27  /**
28   * A pool of {@link Constant}s.
29   *
30   * @param <T> the type of the constant
31   */
32  public abstract class ConstantPool<T extends Constant<T>> {
33  
34      private final ConcurrentMap<String, T> constants = PlatformDependent.newConcurrentHashMap();
35  
36      private final AtomicInteger nextId = new AtomicInteger(1);
37  
38      /**
39       * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
40       */
41      public T valueOf(Class<?> firstNameComponent, String secondNameComponent) {
42          return valueOf(
43                  checkNotNull(firstNameComponent, "firstNameComponent").getName() +
44                  '#' +
45                  checkNotNull(secondNameComponent, "secondNameComponent"));
46      }
47  
48      /**
49       * Returns the {@link Constant} which is assigned to the specified {@code name}.
50       * If there's no such {@link Constant}, a new one will be created and returned.
51       * Once created, the subsequent calls with the same {@code name} will always return the previously created one
52       * (i.e. singleton.)
53       *
54       * @param name the name of the {@link Constant}
55       */
56      public T valueOf(String name) {
57          return getOrCreate(checkNonEmpty(name, "name"));
58      }
59  
60      /**
61       * Get existing constant by name or creates new one if not exists. Threadsafe
62       *
63       * @param name the name of the {@link Constant}
64       */
65      private T getOrCreate(String name) {
66          T constant = constants.get(name);
67          if (constant == null) {
68              final T tempConstant = newConstant(nextId(), name);
69              constant = constants.putIfAbsent(name, tempConstant);
70              if (constant == null) {
71                  return tempConstant;
72              }
73          }
74  
75          return constant;
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          return constants.containsKey(checkNonEmpty(name, "name"));
83      }
84  
85      /**
86       * Creates a new {@link Constant} for the given {@code name} or fail with an
87       * {@link IllegalArgumentException} if a {@link Constant} for the given {@code name} exists.
88       */
89      public T newInstance(String name) {
90          return createOrThrow(checkNonEmpty(name, "name"));
91      }
92  
93      /**
94       * Creates constant by name or throws exception. Threadsafe
95       *
96       * @param name the name of the {@link Constant}
97       */
98      private T createOrThrow(String name) {
99          T constant = constants.get(name);
100         if (constant == null) {
101             final T tempConstant = newConstant(nextId(), name);
102             constant = constants.putIfAbsent(name, tempConstant);
103             if (constant == null) {
104                 return tempConstant;
105             }
106         }
107 
108         throw new IllegalArgumentException(String.format("'%s' is already in use", name));
109     }
110 
111     protected abstract T newConstant(int id, String name);
112 
113     @Deprecated
114     public final int nextId() {
115         return nextId.getAndIncrement();
116     }
117 }