aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristen Kozak <sebright@google.com>2017-05-10 13:52:07 -0700
committerKristen Kozak <sebright@google.com>2017-05-10 13:52:07 -0700
commitaffea37044e8ca1f52e44127ecc4db7c8f8e7576 (patch)
tree0975175432f76e0230614f5179e9eeaf71853007
parent54a084014185d80cd27b81cc4318bb5d62333288 (diff)
downloadplatform_external_opencensus-java-affea37044e8ca1f52e44127ecc4db7c8f8e7576.tar.gz
platform_external_opencensus-java-affea37044e8ca1f52e44127ecc4db7c8f8e7576.tar.bz2
platform_external_opencensus-java-affea37044e8ca1f52e44127ecc4db7c8f8e7576.zip
Use AutoValue to implement DistributionAggregation.
-rw-r--r--core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java95
-rw-r--r--core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java14
2 files changed, 46 insertions, 63 deletions
diff --git a/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java
index 290cf5b5..9900bf0a 100644
--- a/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java
+++ b/core/src/main/java/com/google/instrumentation/stats/DistributionAggregation.java
@@ -13,10 +13,12 @@
package com.google.instrumentation.stats;
+import com.google.auto.value.AutoValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
// TODO(aveitch) The below class should be changed to use a Distribution as a private member.
/**
@@ -29,64 +31,61 @@ import javax.annotation.Nullable;
* <p>Although not forbidden, it is generally a bad idea to include non-finite values (infinities
* or NaNs) in the population of values, as this will render the {@code mean} meaningless.
*/
-public final class DistributionAggregation {
+@Immutable
+@AutoValue
+public abstract class DistributionAggregation {
/**
* Constructs a new {@link DistributionAggregation}.
*/
- public static final DistributionAggregation create(
+ public static DistributionAggregation create(
long count, double mean, double sum, Range range, List<Tag> tags) {
- return new DistributionAggregation(count, mean, sum, range, tags, null);
+ return createInternal(count, mean, sum, range, tags, null);
}
/**
* Constructs a new {@link DistributionAggregation} with the optional {@code bucketCount}s.
*/
- public static final DistributionAggregation create(
+ public static DistributionAggregation create(
long count, double mean, double sum, Range range, List<Tag> tags, List<Long> bucketCounts) {
- return new DistributionAggregation(count, mean, sum, range, tags,
+ return createInternal(count, mean, sum, range, tags,
Collections.unmodifiableList(new ArrayList<Long>(bucketCounts)));
}
- /**
- * {@link Tag}s associated with this {@link DistributionAggregation}.
- *
- * <p>Note: The returned list is unmodifiable, attempts to update it will throw an
- * UnsupportedOperationException.
- */
- public final List<Tag> getTags() {
- return tags;
+ private static DistributionAggregation createInternal(
+ long count, double mean, double sum, Range range, List<Tag> tags, List<Long> bucketCounts) {
+ return new AutoValue_DistributionAggregation(count, mean, sum, range, tags, bucketCounts);
}
/**
* The number of values in the population. Must be non-negative.
*/
- public long getCount() {
- return count;
- }
+ public abstract long getCount();
/**
* The arithmetic mean of the values in the population. If {@link #getCount()} is zero then this
* value must also be zero.
*/
- public double getMean() {
- return mean;
- }
+ public abstract double getMean();
/**
* The sum of the values in the population. If {@link #getCount()} is zero then this values must
* also be zero.
*/
- public double getSum() {
- return sum;
- }
+ public abstract double getSum();
/**
* The range of the population values. If {@link #getCount()} is zero then this returned range is
* implementation-dependent.
*/
- public Range getRange() {
- return range;
- }
+ public abstract Range getRange();
+
+ /**
+ * {@link Tag}s associated with this {@link DistributionAggregation}.
+ *
+ * <p>Note: The returned list is unmodifiable, attempts to update it will throw an
+ * UnsupportedOperationException.
+ */
+ public abstract List<Tag> getTags();
/**
* A Distribution may optionally contain a histogram of the values in the population. The
@@ -109,59 +108,29 @@ public final class DistributionAggregation {
* {@link DistributionAggregationDescriptor#getBucketBoundaries()} returns null.
*/
@Nullable
- public List<Long> getBucketCounts() {
- return bucketCounts;
- }
-
- private final long count;
- private final double mean;
- private final double sum;
- private final Range range;
- private final List<Tag> tags;
- private final List<Long> bucketCounts;
-
- private DistributionAggregation(
- long count, double mean, double sum, Range range, List<Tag> tags,
- @Nullable List<Long> bucketCounts) {
- this.count = count;
- this.mean = mean;
- this.sum = sum;
- this.range = range;
- this.tags = tags;
- this.bucketCounts = bucketCounts;
- }
+ public abstract List<Long> getBucketCounts();
/**
* Describes a range of population values.
*/
- public static final class Range {
+ @Immutable
+ @AutoValue
+ public abstract static class Range {
/**
* Constructs a new {@link Range}.
*/
- public static final Range create(double min, double max) {
- return new Range(min, max);
+ public static Range create(double min, double max) {
+ return new AutoValue_DistributionAggregation_Range(min, max);
}
/**
* The minimum of the population values.
*/
- public double getMin() {
- return min;
- }
+ public abstract double getMin();
/**
* The maximum of the population values.
*/
- public double getMax() {
- return max;
- }
-
- private double min;
- private double max;
-
- private Range(double min, double max) {
- this.min = min;
- this.max = max;
- }
+ public abstract double getMax();
}
}
diff --git a/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java
index 55e0e4e0..633de2bb 100644
--- a/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java
+++ b/core/src/test/java/com/google/instrumentation/stats/DistributionAggregationTest.java
@@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.instrumentation.stats.DistributionAggregation.Range;
+import com.google.common.testing.EqualsTester;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
@@ -67,6 +68,19 @@ public final class DistributionAggregationTest {
}
}
+ @Test
+ public void testDistributionAggregationEquals() {
+ List<Long> buckets = Arrays.asList(1L, 2L, 3L);
+ new EqualsTester()
+ .addEqualityGroup(
+ DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), TAGS),
+ DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), TAGS))
+ .addEqualityGroup(
+ DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), TAGS, buckets),
+ DistributionAggregation.create(10, 5.0, 30.0, Range.create(1.0, 5.0), TAGS, buckets))
+ .testEquals();
+ }
+
private static final TagKey K1 = TagKey.create("k1");
private static final TagKey K2 = TagKey.create("k2");