• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 
22 import java.util.concurrent.atomic.AtomicLong;
23 import javax.annotation.Nullable;
24 
25 /**
26  * An internal class. Do not use.
27  *
28  *<p>An object that has an ID that is unique within the JVM, primarily for debug logging.
29  */
30 @Internal
31 public final class InternalLogId {
32 
33   private static final AtomicLong idAlloc = new AtomicLong();
34 
35   /**
36    * Creates a log id.
37    *
38    * @param type the "Type" to be used when logging this id.   The short name of this class will be
39    *     used, or else a default if the class is anonymous.
40    * @param details a short, human readable string that describes the object the id is attached to.
41    *     Typically this will be an address or target.
42    */
allocate(Class<?> type, @Nullable String details)43   public static InternalLogId allocate(Class<?> type, @Nullable String details) {
44     return allocate(getClassName(type), details);
45   }
46 
47   /**
48    * Creates a log id.
49    *
50    * @param typeName the "Type" to be used when logging this id.
51    * @param details a short, human readable string that describes the object the id is attached to.
52    *     Typically this will be an address or target.
53    */
allocate(String typeName, @Nullable String details)54   public static InternalLogId allocate(String typeName, @Nullable String details) {
55     return new InternalLogId(typeName, details, getNextId());
56   }
57 
getNextId()58   static long getNextId() {
59     return idAlloc.incrementAndGet();
60   }
61 
62   private final String typeName;
63   @Nullable
64   private final String details;
65   private final long id;
66 
InternalLogId(String typeName, String details, long id)67   InternalLogId(String typeName, String details, long id) {
68     checkNotNull(typeName, "typeName");
69     checkArgument(!typeName.isEmpty(), "empty type");
70     this.typeName = typeName;
71     this.details = details;
72     this.id = id;
73   }
74 
getTypeName()75   public String getTypeName() {
76     return typeName;
77   }
78 
79   @Nullable
getDetails()80   public String getDetails() {
81     return details;
82   }
83 
getId()84   public long getId() {
85     return id;
86   }
87 
88   @Override
toString()89   public String toString() {
90     StringBuilder sb = new StringBuilder();
91     sb.append(shortName());
92     if (details != null) {
93       sb.append(": (");
94       sb.append(details);
95       sb.append(')');
96     }
97     return sb.toString();
98   }
99 
getClassName(Class<?> type)100   private static String getClassName(Class<?> type) {
101     String className = checkNotNull(type, "type").getSimpleName();
102     if (!className.isEmpty()) {
103       return className;
104     }
105     // + 1 removes the separating '.'
106     return type.getName().substring(type.getPackage().getName().length() + 1);
107   }
108 
shortName()109   public String shortName() {
110     return typeName + "<" + id + ">";
111   }
112 }
113