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 io.netty.util.internal.PlatformDependent;
19  import io.netty.util.internal.ThreadLocalRandom;
20  
21  import java.nio.ByteBuffer;
22  
23  /**
24   * Base implementation of {@link Constant}.
25   */
26  public abstract class AbstractConstant<T extends AbstractConstant<T>> implements Constant<T> {
27  
28      private final int id;
29      private final String name;
30      private volatile long uniquifier;
31      private ByteBuffer directBuffer;
32  
33      /**
34       * Creates a new instance.
35       */
36      protected AbstractConstant(int id, String name) {
37          this.id = id;
38          this.name = name;
39      }
40  
41      @Override
42      public final String name() {
43          return name;
44      }
45  
46      @Override
47      public final int id() {
48          return id;
49      }
50  
51      @Override
52      public final String toString() {
53          return name();
54      }
55  
56      @Override
57      public final int hashCode() {
58          return super.hashCode();
59      }
60  
61      @Override
62      public final boolean equals(Object obj) {
63          return super.equals(obj);
64      }
65  
66      @Override
67      public final int compareTo(T o) {
68          if (this == o) {
69              return 0;
70          }
71  
72          @SuppressWarnings("UnnecessaryLocalVariable")
73          AbstractConstant<T> other = o;
74          int returnCode;
75  
76          returnCode = hashCode() - other.hashCode();
77          if (returnCode != 0) {
78              return returnCode;
79          }
80  
81          long thisUV = uniquifier();
82          long otherUV = other.uniquifier();
83          if (thisUV < otherUV) {
84              return -1;
85          }
86          if (thisUV > otherUV) {
87              return 1;
88          }
89  
90          throw new Error("failed to compare two different constants");
91      }
92  
93      private long uniquifier() {
94          long uniquifier;
95          if ((uniquifier = this.uniquifier) == 0) {
96              synchronized (this) {
97                  while ((uniquifier = this.uniquifier) == 0) {
98                      if (PlatformDependent.hasUnsafe()) {
99                          directBuffer = ByteBuffer.allocateDirect(1);
100                         this.uniquifier = PlatformDependent.directBufferAddress(directBuffer);
101                     } else {
102                         directBuffer = null;
103                         this.uniquifier = ThreadLocalRandom.current().nextLong();
104                     }
105                 }
106             }
107         }
108 
109         return uniquifier;
110     }
111 }