1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
26
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
38
39
40
41
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
64
65
66
67
68 @SuppressWarnings("unused")
69 protected void validateArgs(Object... args) {
70
71 }
72
73
74
75
76
77
78 public final String name() {
79 return name;
80 }
81
82
83
84
85
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 }