forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeValue.java
More file actions
224 lines (190 loc) · 6.11 KB
/
Copy pathAttributeValue.java
File metadata and controls
224 lines (190 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.trace;
import com.google.auto.value.AutoValue;
import io.opentelemetry.internal.Utils;
import javax.annotation.concurrent.Immutable;
/**
* A class that represents all the possible values for an attribute. An attribute can have 4 types
* of values: {@code String}, {@code boolean}, {@code long} or {@code double}, represented through
* {@code AttributeValue.Type}.
*
* @since 0.1.0
*/
@Immutable
public abstract class AttributeValue {
/**
* An enum that represents all the possible value types for an {@code AttributeValue}.
*
* @since 0.1.0
*/
public enum Type {
STRING,
BOOLEAN,
LONG,
DOUBLE
}
/**
* Returns an {@code AttributeValue} with a string value.
*
* @param stringValue The new value.
* @return an {@code AttributeValue} with a string value.
* @throws NullPointerException if {@code stringValue} is {@code null}.
* @since 0.1.0
*/
public static AttributeValue stringAttributeValue(String stringValue) {
return AttributeValueString.create(stringValue);
}
/**
* Returns an {@code AttributeValue} with a boolean value.
*
* @param booleanValue The new value.
* @return an {@code AttributeValue} with a boolean value.
* @since 0.1.0
*/
public static AttributeValue booleanAttributeValue(boolean booleanValue) {
return AttributeValueBoolean.create(booleanValue);
}
/**
* Returns an {@code AttributeValue} with a long value.
*
* @param longValue The new value.
* @return an {@code AttributeValue} with a long value.
* @since 0.1.0
*/
public static AttributeValue longAttributeValue(long longValue) {
return AttributeValueLong.create(longValue);
}
/**
* Returns an {@code AttributeValue} with a double value.
*
* @param doubleValue The new value.
* @return an {@code AttributeValue} with a double value.
* @since 0.1.0
*/
public static AttributeValue doubleAttributeValue(double doubleValue) {
return AttributeValueDouble.create(doubleValue);
}
AttributeValue() {}
/**
* Returns the string value of this {@code AttributeValue}. An UnsupportedOperationException will
* be thrown if getType() is not {@link Type#STRING}.
*
* @return the string value of this {@code AttributeValue}.
* @since 0.1.0
*/
public String getStringValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}
/**
* Returns the boolean value of this {@code AttributeValue}. An UnsupportedOperationException will
* be thrown if getType() is not {@link Type#BOOLEAN}.
*
* @return the boolean value of this {@code AttributeValue}.
* @since 0.1.0
*/
public boolean getBooleanValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}
/**
* Returns the long value of this {@code AttributeValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link Type#LONG}.
*
* @return the long value of this {@code AttributeValue}.
* @since 0.1.0
*/
public long getLongValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}
/**
* Returns the double value of this {@code AttributeValue}. An UnsupportedOperationException will
* be thrown if getType() is not {@link Type#DOUBLE}.
*
* @return the double value of this {@code AttributeValue}.
* @since 0.1.0
*/
public double getDoubleValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}
/**
* Returns a {@code Type} corresponding to the underlying value of this {@code AttributeValue}.
*
* @return the {@code Type} for the value of this {@code AttributeValue}.
* @since 0.1.0
*/
public abstract Type getType();
@Immutable
@AutoValue
abstract static class AttributeValueString extends AttributeValue {
AttributeValueString() {}
static AttributeValue create(String stringValue) {
return new AutoValue_AttributeValue_AttributeValueString(
Utils.checkNotNull(stringValue, "stringValue"));
}
@Override
public final Type getType() {
return Type.STRING;
}
@Override
public abstract String getStringValue();
}
@Immutable
@AutoValue
abstract static class AttributeValueBoolean extends AttributeValue {
AttributeValueBoolean() {}
static AttributeValue create(boolean booleanValue) {
return new AutoValue_AttributeValue_AttributeValueBoolean(booleanValue);
}
@Override
public final Type getType() {
return Type.BOOLEAN;
}
@Override
public abstract boolean getBooleanValue();
}
@Immutable
@AutoValue
abstract static class AttributeValueLong extends AttributeValue {
AttributeValueLong() {}
static AttributeValue create(long longValue) {
return new AutoValue_AttributeValue_AttributeValueLong(longValue);
}
@Override
public final Type getType() {
return Type.LONG;
}
@Override
public abstract long getLongValue();
}
@Immutable
@AutoValue
abstract static class AttributeValueDouble extends AttributeValue {
AttributeValueDouble() {}
static AttributeValue create(double doubleValue) {
return new AutoValue_AttributeValue_AttributeValueDouble(doubleValue);
}
@Override
public final Type getType() {
return Type.DOUBLE;
}
@Override
public abstract double getDoubleValue();
}
}