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  import java.util.concurrent.ConcurrentMap;
19  import java.util.concurrent.atomic.AtomicInteger;
20  
21  import static io.netty.util.internal.ObjectUtil.checkNotNull;
22  
23  /**
24   * @deprecated Known to have problems with class loaders.
25   *
26   * Defines a name that must be unique in the map that is provided during construction.
27   */
28  @Deprecated
29  public class UniqueName implements Comparable<UniqueName> {
30  
31      private static final AtomicInteger nextId = new AtomicInteger();
32  
33      private final int id;
34      private final String name;
35  
36      /**
37       * Constructs a new {@link UniqueName}
38       *
39       * @param map the map of names to compare with
40       * @param name the name of this {@link UniqueName}
41       * @param args the arguments to process
42       */
43      public UniqueName(ConcurrentMap<String, Boolean> map, String name, Object... args) {
44          checkNotNull(map, "map");
45  
46          if (args != null && args.length > 0) {
47              validateArgs(args);
48          }
49  
50          if (map.putIfAbsent(name, Boolean.TRUE) != null) {
51              throw new IllegalArgumentException(String.format("'%s' is already in use", name));
52          }
53          this.name = checkNotNull(name, "name");
54          id = nextId.incrementAndGet();
55      }
56  
57      protected UniqueName(String name) {
58          this.name = checkNotNull(name, "name");
59          id = nextId.incrementAndGet();
60      }
61  
62      /**
63       * Validates the given arguments.  This method does not do anything on its own, but must be
64       * overridden by its subclasses.
65       *
66       * @param args arguments to validate
67       */
68      @SuppressWarnings("unused")
69      protected void validateArgs(Object... args) {
70          // Subclasses will override.
71      }
72  
73      /**
74       * Returns this {@link UniqueName}'s name
75       *
76       * @return the name
77       */
78      public final String name() {
79          return name;
80      }
81  
82      /**
83       * Returns this {@link UniqueName}'s ID
84       *
85       * @return the id
86       */
87      public final int id() {
88          return id;
89      }
90  
91      @Override
92      public final int hashCode() {
93          return super.hashCode();
94      }
95  
96      @Override
97      public final boolean equals(Object o) {
98          return super.equals(o);
99      }
100 
101     @Override
102     public int compareTo(UniqueName other) {
103         if (this == other) {
104             return 0;
105         }
106 
107         int returnCode = name.compareTo(other.name);
108         if (returnCode != 0) {
109             return returnCode;
110         }
111 
112         return ((Integer) id).compareTo(other.id);
113     }
114 
115     @Override
116     public String toString() {
117         return name();
118     }
119 }