• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-17, 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.trace;
18 
19 import com.google.auto.value.AutoValue;
20 import io.opencensus.common.Function;
21 import io.opencensus.internal.Utils;
22 import javax.annotation.concurrent.Immutable;
23 
24 /**
25  * A class that represents all the possible values for an attribute. An attribute can have 3 types
26  * of values: {@code String}, {@code Boolean} or {@code Long}.
27  *
28  * @since 0.5
29  */
30 @Immutable
31 public abstract class AttributeValue {
32   /**
33    * Returns an {@code AttributeValue} with a string value.
34    *
35    * @param stringValue The new value.
36    * @return an {@code AttributeValue} with a string value.
37    * @throws NullPointerException if {@code stringValue} is {@code null}.
38    * @since 0.5
39    */
stringAttributeValue(String stringValue)40   public static AttributeValue stringAttributeValue(String stringValue) {
41     return AttributeValueString.create(stringValue);
42   }
43 
44   /**
45    * Returns an {@code AttributeValue} with a boolean value.
46    *
47    * @param booleanValue The new value.
48    * @return an {@code AttributeValue} with a boolean value.
49    * @since 0.5
50    */
booleanAttributeValue(boolean booleanValue)51   public static AttributeValue booleanAttributeValue(boolean booleanValue) {
52     return AttributeValueBoolean.create(booleanValue);
53   }
54 
55   /**
56    * Returns an {@code AttributeValue} with a long value.
57    *
58    * @param longValue The new value.
59    * @return an {@code AttributeValue} with a long value.
60    * @since 0.5
61    */
longAttributeValue(long longValue)62   public static AttributeValue longAttributeValue(long longValue) {
63     return AttributeValueLong.create(longValue);
64   }
65 
66   /**
67    * Returns an {@code AttributeValue} with a double value.
68    *
69    * @param doubleValue The new value.
70    * @return an {@code AttributeValue} with a double value.
71    * @since 0.17
72    */
doubleAttributeValue(double doubleValue)73   public static AttributeValue doubleAttributeValue(double doubleValue) {
74     return AttributeValueDouble.create(doubleValue);
75   }
76 
AttributeValue()77   AttributeValue() {}
78 
79   /**
80    * Applies a function to the underlying value. The function that is called depends on the value's
81    * type, which can be {@code String}, {@code Long}, or {@code Boolean}.
82    *
83    * @param stringFunction the function that should be applied if the value has type {@code String}.
84    * @param longFunction the function that should be applied if the value has type {@code Long}.
85    * @param booleanFunction the function that should be applied if the value has type {@code
86    *     Boolean}.
87    * @param defaultFunction the function that should be applied if the value has a type that was
88    *     added after this {@code match} method was added to the API. See {@link
89    *     io.opencensus.common.Functions} for some common functions for handling unknown types.
90    * @return the result of the function applied to the underlying value.
91    * @since 0.5
92    * @deprecated in favor of {@link #match(Function, Function, Function, Function, Function)}.
93    */
94   @Deprecated
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> defaultFunction)95   public abstract <T> T match(
96       Function<? super String, T> stringFunction,
97       Function<? super Boolean, T> booleanFunction,
98       Function<? super Long, T> longFunction,
99       Function<Object, T> defaultFunction);
100 
101   /**
102    * Applies a function to the underlying value. The function that is called depends on the value's
103    * type, which can be {@code String}, {@code Long}, or {@code Boolean}.
104    *
105    * @param stringFunction the function that should be applied if the value has type {@code String}.
106    * @param longFunction the function that should be applied if the value has type {@code Long}.
107    * @param booleanFunction the function that should be applied if the value has type {@code
108    *     Boolean}.
109    * @param doubleFunction the function that should be applied if the value has type {@code Double}.
110    * @param defaultFunction the function that should be applied if the value has a type that was
111    *     added after this {@code match} method was added to the API. See {@link
112    *     io.opencensus.common.Functions} for some common functions for handling unknown types.
113    * @return the result of the function applied to the underlying value.
114    * @since 0.17
115    */
116   @SuppressWarnings("InconsistentOverloads")
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<? super Double, T> doubleFunction, Function<Object, T> defaultFunction)117   public abstract <T> T match(
118       Function<? super String, T> stringFunction,
119       Function<? super Boolean, T> booleanFunction,
120       Function<? super Long, T> longFunction,
121       Function<? super Double, T> doubleFunction,
122       Function<Object, T> defaultFunction);
123 
124   @Immutable
125   @AutoValue
126   abstract static class AttributeValueString extends AttributeValue {
127 
AttributeValueString()128     AttributeValueString() {}
129 
create(String stringValue)130     static AttributeValue create(String stringValue) {
131       return new AutoValue_AttributeValue_AttributeValueString(
132           Utils.checkNotNull(stringValue, "stringValue"));
133     }
134 
135     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> defaultFunction)136     public final <T> T match(
137         Function<? super String, T> stringFunction,
138         Function<? super Boolean, T> booleanFunction,
139         Function<? super Long, T> longFunction,
140         Function<Object, T> defaultFunction) {
141       return stringFunction.apply(getStringValue());
142     }
143 
144     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<? super Double, T> doubleFunction, Function<Object, T> defaultFunction)145     public final <T> T match(
146         Function<? super String, T> stringFunction,
147         Function<? super Boolean, T> booleanFunction,
148         Function<? super Long, T> longFunction,
149         Function<? super Double, T> doubleFunction,
150         Function<Object, T> defaultFunction) {
151       return stringFunction.apply(getStringValue());
152     }
153 
getStringValue()154     abstract String getStringValue();
155   }
156 
157   @Immutable
158   @AutoValue
159   abstract static class AttributeValueBoolean extends AttributeValue {
160 
AttributeValueBoolean()161     AttributeValueBoolean() {}
162 
create(Boolean booleanValue)163     static AttributeValue create(Boolean booleanValue) {
164       return new AutoValue_AttributeValue_AttributeValueBoolean(
165           Utils.checkNotNull(booleanValue, "booleanValue"));
166     }
167 
168     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> defaultFunction)169     public final <T> T match(
170         Function<? super String, T> stringFunction,
171         Function<? super Boolean, T> booleanFunction,
172         Function<? super Long, T> longFunction,
173         Function<Object, T> defaultFunction) {
174       return booleanFunction.apply(getBooleanValue());
175     }
176 
177     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<? super Double, T> doubleFunction, Function<Object, T> defaultFunction)178     public final <T> T match(
179         Function<? super String, T> stringFunction,
180         Function<? super Boolean, T> booleanFunction,
181         Function<? super Long, T> longFunction,
182         Function<? super Double, T> doubleFunction,
183         Function<Object, T> defaultFunction) {
184       return booleanFunction.apply(getBooleanValue());
185     }
186 
getBooleanValue()187     abstract Boolean getBooleanValue();
188   }
189 
190   @Immutable
191   @AutoValue
192   abstract static class AttributeValueLong extends AttributeValue {
193 
AttributeValueLong()194     AttributeValueLong() {}
195 
create(Long longValue)196     static AttributeValue create(Long longValue) {
197       return new AutoValue_AttributeValue_AttributeValueLong(
198           Utils.checkNotNull(longValue, "longValue"));
199     }
200 
201     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> defaultFunction)202     public final <T> T match(
203         Function<? super String, T> stringFunction,
204         Function<? super Boolean, T> booleanFunction,
205         Function<? super Long, T> longFunction,
206         Function<Object, T> defaultFunction) {
207       return longFunction.apply(getLongValue());
208     }
209 
210     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<? super Double, T> doubleFunction, Function<Object, T> defaultFunction)211     public final <T> T match(
212         Function<? super String, T> stringFunction,
213         Function<? super Boolean, T> booleanFunction,
214         Function<? super Long, T> longFunction,
215         Function<? super Double, T> doubleFunction,
216         Function<Object, T> defaultFunction) {
217       return longFunction.apply(getLongValue());
218     }
219 
getLongValue()220     abstract Long getLongValue();
221   }
222 
223   @Immutable
224   @AutoValue
225   abstract static class AttributeValueDouble extends AttributeValue {
226 
AttributeValueDouble()227     AttributeValueDouble() {}
228 
create(Double doubleValue)229     static AttributeValue create(Double doubleValue) {
230       return new AutoValue_AttributeValue_AttributeValueDouble(
231           Utils.checkNotNull(doubleValue, "doubleValue"));
232     }
233 
234     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> defaultFunction)235     public final <T> T match(
236         Function<? super String, T> stringFunction,
237         Function<? super Boolean, T> booleanFunction,
238         Function<? super Long, T> longFunction,
239         Function<Object, T> defaultFunction) {
240       return defaultFunction.apply(getDoubleValue());
241     }
242 
243     @Override
match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<? super Double, T> doubleFunction, Function<Object, T> defaultFunction)244     public final <T> T match(
245         Function<? super String, T> stringFunction,
246         Function<? super Boolean, T> booleanFunction,
247         Function<? super Long, T> longFunction,
248         Function<? super Double, T> doubleFunction,
249         Function<Object, T> defaultFunction) {
250       return doubleFunction.apply(getDoubleValue());
251     }
252 
getDoubleValue()253     abstract Double getDoubleValue();
254   }
255 }
256