1 /*
2  * Copyright 2018, OpenCensus 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.opencensus.common;
18 
19 import java.util.TreeMap;
20 import javax.annotation.Nullable;
21 
22 /**
23  * A Enum representation for Ids and Size for attributes of {@code ServerStats}.
24  *
25  * <p>See <a
26  * href="https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/CensusServerStatsEncoding.md">opencensus-server-stats-specs</a>
27  * for the field ids and their length defined for Server Stats
28  *
29  * @since 0.16
30  */
31 public final class ServerStatsFieldEnums {
32 
33   /**
34    * Available Ids for {@code ServerStats} attributes.
35    *
36    * @since 0.16
37    */
38   public enum Id {
39     /**
40      * Id for Latency observed at Load Balancer.
41      *
42      * @since 0.16
43      */
44     SERVER_STATS_LB_LATENCY_ID(0),
45     /**
46      * Id for Latency observed at Server.
47      *
48      * @since 0.16
49      */
50     SERVER_STATS_SERVICE_LATENCY_ID(1),
51     /**
52      * Id for Trace options.
53      *
54      * @since 0.16
55      */
56     SERVER_STATS_TRACE_OPTION_ID(2);
57 
58     private final int value;
59 
Id(int value)60     private Id(int value) {
61       this.value = value;
62     }
63 
64     /**
65      * Returns the numerical value of the {@link Id}.
66      *
67      * @return the numerical value of the {@code Id}.
68      * @since 0.16
69      */
value()70     public int value() {
71       return value;
72     }
73 
74     private static final TreeMap<Integer, Id> map = new TreeMap<Integer, Id>();
75 
76     static {
77       for (Id id : Id.values()) {
map.put(id.value, id)78         map.put(id.value, id);
79       }
80     }
81 
82     /**
83      * Returns the {@link Id} representing the value value of the id.
84      *
85      * @param value integer value for which {@code Id} is being requested.
86      * @return the numerical value of the id. null if the id is not valid
87      * @since 0.16
88      */
89     @Nullable
valueOf(int value)90     public static Id valueOf(int value) {
91       return map.get(value);
92     }
93   }
94 
95   /**
96    * Size for each attributes in {@code ServerStats}.
97    *
98    * @since 0.16
99    */
100   public enum Size {
101     /**
102      * Number of bytes used to represent latency observed at Load Balancer.
103      *
104      * @since 0.16
105      */
106     SERVER_STATS_LB_LATENCY_SIZE(8),
107     /**
108      * Number of bytes used to represent latency observed at Server.
109      *
110      * @since 0.16
111      */
112     SERVER_STATS_SERVICE_LATENCY_SIZE(8),
113     /**
114      * Number of bytes used to represent Trace option.
115      *
116      * @since 0.16
117      */
118     SERVER_STATS_TRACE_OPTION_SIZE(1);
119 
120     private final int value;
121 
Size(int value)122     private Size(int value) {
123       this.value = value;
124     }
125 
126     /**
127      * Returns the numerical value of the {@link Size}.
128      *
129      * @return the numerical value of the {@code Size}.
130      * @since 0.16
131      */
value()132     public int value() {
133       return value;
134     }
135   }
136 
137   private static final int TOTALSIZE = computeTotalSize();
138 
ServerStatsFieldEnums()139   private ServerStatsFieldEnums() {}
140 
computeTotalSize()141   private static int computeTotalSize() {
142     int sum = 0;
143     for (Size sizeValue : Size.values()) {
144       sum += sizeValue.value();
145       sum += 1; // For Id
146     }
147     return sum;
148   }
149 
150   /**
151    * Returns the total size required to encode the {@code ServerStats}.
152    *
153    * @return the total size required to encode all fields in {@code ServerStats}.
154    * @since 0.16
155    */
getTotalSize()156   public static int getTotalSize() {
157     return TOTALSIZE;
158   }
159 }
160