diff options
author | Yang Song <songy23@users.noreply.github.com> | 2018-09-17 10:15:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 10:15:15 -0700 |
commit | 3d98a788798898ca653d3d2ac0aa984410e2a436 (patch) | |
tree | 942887102efbeb0740164b62394da578e247c37f /api/src | |
parent | 5faef17f74e5d8d857b50cc9e66e0f595080c488 (diff) | |
download | opencensus-java-3d98a788798898ca653d3d2ac0aa984410e2a436.tar.gz |
Tracing: Add AttributeValueDouble. (#1442)
Also update Trace exporters and ZPages, and fix a few typos.
Diffstat (limited to 'api/src')
-rw-r--r-- | api/src/main/java/io/opencensus/trace/AttributeValue.java | 107 | ||||
-rw-r--r-- | api/src/test/java/io/opencensus/trace/AttributeValueTest.java | 82 |
2 files changed, 185 insertions, 4 deletions
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> T match( Function<? super String, T> stringFunction, Function<? super Boolean, T> booleanFunction, Function<? super Long, T> longFunction, Function<Object, T> 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> T 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); + @Immutable @AutoValue abstract static class AttributeValueString extends AttributeValue { @@ -105,6 +141,16 @@ public abstract class AttributeValue { return stringFunction.apply(getStringValue()); } + @Override + public final <T> T 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) { + 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> T 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) { + 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> T 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) { + 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> T match( + Function<? super String, T> stringFunction, + Function<? super Boolean, T> booleanFunction, + Function<? super Long, T> longFunction, + Function<Object, T> defaultFunction) { + return defaultFunction.apply(getDoubleValue()); + } + + @Override + public final <T> T 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) { + return doubleFunction.apply(getDoubleValue()); + } + + abstract Double getDoubleValue(); + } } diff --git a/api/src/test/java/io/opencensus/trace/AttributeValueTest.java b/api/src/test/java/io/opencensus/trace/AttributeValueTest.java index e505c59b..05ef43c0 100644 --- a/api/src/test/java/io/opencensus/trace/AttributeValueTest.java +++ b/api/src/test/java/io/opencensus/trace/AttributeValueTest.java @@ -124,6 +124,83 @@ public class AttributeValueTest { } @Test + public void doubleAttributeValue() { + AttributeValue attribute = AttributeValue.doubleAttributeValue(1.23456); + attribute.match( + new Function<String, Object>() { + @Override + @Nullable + public Object apply(String stringValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Boolean, Object>() { + @Override + @Nullable + public Object apply(Boolean booleanValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Long, Object>() { + @Override + @Nullable + public Object apply(Long longValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Double, Object>() { + @Override + @Nullable + public Object apply(Double doubleValue) { + assertThat(doubleValue).isEqualTo(1.23456); + return null; + } + }, + Functions.throwIllegalArgumentException()); + } + + @Test + public void doubleAttributeValue_DeprecatedMatchFunction() { + AttributeValue attribute = AttributeValue.doubleAttributeValue(1.23456); + attribute.match( + new Function<String, Object>() { + @Override + @Nullable + public Object apply(String stringValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Boolean, Object>() { + @Override + @Nullable + public Object apply(Boolean booleanValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Long, Object>() { + @Override + @Nullable + public Object apply(Long longValue) { + fail("Expected a Double"); + return null; + } + }, + new Function<Object, Object>() { + @Override + @Nullable + public Object apply(Object value) { + assertThat(value).isEqualTo(1.23456); + return null; + } + }); + } + + @Test public void attributeValue_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); tester.addEqualityGroup( @@ -136,6 +213,9 @@ public class AttributeValueTest { tester.addEqualityGroup( AttributeValue.longAttributeValue(123456L), AttributeValue.longAttributeValue(123456L)); tester.addEqualityGroup(AttributeValue.longAttributeValue(1234567L)); + tester.addEqualityGroup( + AttributeValue.doubleAttributeValue(1.23456), AttributeValue.doubleAttributeValue(1.23456)); + tester.addEqualityGroup(AttributeValue.doubleAttributeValue(1.234567)); tester.testEquals(); } @@ -147,5 +227,7 @@ public class AttributeValueTest { assertThat(attribute.toString()).contains("true"); attribute = AttributeValue.longAttributeValue(123456L); assertThat(attribute.toString()).contains("123456"); + attribute = AttributeValue.doubleAttributeValue(1.23456); + assertThat(attribute.toString()).contains("1.23456"); } } |