From 3d98a788798898ca653d3d2ac0aa984410e2a436 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Mon, 17 Sep 2018 10:15:15 -0700 Subject: Tracing: Add AttributeValueDouble. (#1442) Also update Trace exporters and ZPages, and fix a few typos. --- .../java/io/opencensus/trace/AttributeValue.java | 107 ++++++++++++++++++++- 1 file changed, 103 insertions(+), 4 deletions(-) (limited to 'api/src/main/java') diff --git a/api/src/main/java/io/opencensus/trace/AttributeValue.java b/api/src/main/java/io/opencensus/trace/AttributeValue.java index a8d4e1e3..efa9d1df 100644 --- a/api/src/main/java/io/opencensus/trace/AttributeValue.java +++ b/api/src/main/java/io/opencensus/trace/AttributeValue.java @@ -63,6 +63,17 @@ public abstract class AttributeValue { 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.17 + */ + public static AttributeValue doubleAttributeValue(double doubleValue) { + return AttributeValueDouble.create(doubleValue); + } + AttributeValue() {} /** @@ -78,13 +89,38 @@ public abstract class AttributeValue { * io.opencensus.common.Functions} for some common functions for handling unknown types. * @return the result of the function applied to the underlying value. * @since 0.5 + * @deprecated in favor of {@link #match(Function, Function, Function, Function, Function)}. */ + @Deprecated public abstract T match( Function stringFunction, Function booleanFunction, Function longFunction, Function defaultFunction); + /** + * Applies a function to the underlying value. The function that is called depends on the value's + * type, which can be {@code String}, {@code Long}, or {@code Boolean}. + * + * @param stringFunction the function that should be applied if the value has type {@code String}. + * @param longFunction the function that should be applied if the value has type {@code Long}. + * @param booleanFunction the function that should be applied if the value has type {@code + * Boolean}. + * @param doubleFunction the function that should be applied if the value has type {@code Double}. + * @param defaultFunction the function that should be applied if the value has a type that was + * added after this {@code match} method was added to the API. See {@link + * io.opencensus.common.Functions} for some common functions for handling unknown types. + * @return the result of the function applied to the underlying value. + * @since 0.17 + */ + @SuppressWarnings("InconsistentOverloads") + public abstract T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function doubleFunction, + Function defaultFunction); + @Immutable @AutoValue abstract static class AttributeValueString extends AttributeValue { @@ -105,6 +141,16 @@ public abstract class AttributeValue { return stringFunction.apply(getStringValue()); } + @Override + public final T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function doubleFunction, + Function defaultFunction) { + return stringFunction.apply(getStringValue()); + } + abstract String getStringValue(); } @@ -114,9 +160,9 @@ public abstract class AttributeValue { AttributeValueBoolean() {} - static AttributeValue create(Boolean stringValue) { + static AttributeValue create(Boolean booleanValue) { return new AutoValue_AttributeValue_AttributeValueBoolean( - Utils.checkNotNull(stringValue, "stringValue")); + Utils.checkNotNull(booleanValue, "booleanValue")); } @Override @@ -128,6 +174,16 @@ public abstract class AttributeValue { return booleanFunction.apply(getBooleanValue()); } + @Override + public final T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function doubleFunction, + Function defaultFunction) { + return booleanFunction.apply(getBooleanValue()); + } + abstract Boolean getBooleanValue(); } @@ -137,9 +193,9 @@ public abstract class AttributeValue { AttributeValueLong() {} - static AttributeValue create(Long stringValue) { + static AttributeValue create(Long longValue) { return new AutoValue_AttributeValue_AttributeValueLong( - Utils.checkNotNull(stringValue, "stringValue")); + Utils.checkNotNull(longValue, "longValue")); } @Override @@ -151,6 +207,49 @@ public abstract class AttributeValue { return longFunction.apply(getLongValue()); } + @Override + public final T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function doubleFunction, + Function defaultFunction) { + return longFunction.apply(getLongValue()); + } + abstract Long getLongValue(); } + + @Immutable + @AutoValue + abstract static class AttributeValueDouble extends AttributeValue { + + AttributeValueDouble() {} + + static AttributeValue create(Double doubleValue) { + return new AutoValue_AttributeValue_AttributeValueDouble( + Utils.checkNotNull(doubleValue, "doubleValue")); + } + + @Override + public final T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function defaultFunction) { + return defaultFunction.apply(getDoubleValue()); + } + + @Override + public final T match( + Function stringFunction, + Function booleanFunction, + Function longFunction, + Function doubleFunction, + Function defaultFunction) { + return doubleFunction.apply(getDoubleValue()); + } + + abstract Double getDoubleValue(); + } } -- cgit v1.2.3