aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorMayur Kale <mayurkale@google.com>2018-10-10 12:31:51 -0700
committerGitHub <noreply@github.com>2018-10-10 12:31:51 -0700
commit9c4df3dbe085a70850cc466849d33d90ffa846f4 (patch)
treef36c626bae1ec48ac2209be310d4e30d929db7b6 /api
parente3b2074a090836985307123517fd2d361c4b503c (diff)
downloadopencensus-java-9c4df3dbe085a70850cc466849d33d90ffa846f4.tar.gz
Metrics/TimeSeries: Add createWithOnePoint method to accept only one … (#1486)
* Metrics/TimeSeries: Add createWithOnePoint method to accept only one point * Fix reviews * Minor Fix
Diffstat (limited to 'api')
-rw-r--r--api/src/main/java/io/opencensus/metrics/export/TimeSeries.java37
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java34
2 files changed, 68 insertions, 3 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java
index 959f55ba..bfaeae98 100644
--- a/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java
+++ b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java
@@ -52,14 +52,45 @@ public abstract class TimeSeries {
*/
public static TimeSeries create(
List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) {
+ Utils.checkNotNull(points, "points");
+ Utils.checkListElementNotNull(points, "point");
+ return createInternal(
+ labelValues, Collections.unmodifiableList(new ArrayList<Point>(points)), startTimestamp);
+ }
+
+ /**
+ * Creates a {@link TimeSeries}.
+ *
+ * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
+ * @param point the single data {@code Point} of this {@code TimeSeries}.
+ * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null
+ * for cumulative {@code Point}s.
+ * @return a {@code TimeSeries}.
+ * @since 0.17
+ */
+ public static TimeSeries createWithOnePoint(
+ List<LabelValue> labelValues, Point point, @Nullable Timestamp startTimestamp) {
+ Utils.checkNotNull(point, "point");
+ return createInternal(labelValues, Collections.singletonList(point), startTimestamp);
+ }
+
+ /**
+ * Creates a {@link TimeSeries}.
+ *
+ * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
+ * @param points the data {@code Point}s of this {@code TimeSeries}.
+ * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null
+ * for cumulative {@code Point}s.
+ * @return a {@code TimeSeries}.
+ */
+ private static TimeSeries createInternal(
+ List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) {
// Fail fast on null lists to prevent NullPointerException when copying the lists.
Utils.checkNotNull(labelValues, "labelValues");
- Utils.checkNotNull(points, "points");
Utils.checkListElementNotNull(labelValues, "labelValue");
- Utils.checkListElementNotNull(points, "point");
return new AutoValue_TimeSeries(
Collections.unmodifiableList(new ArrayList<LabelValue>(labelValues)),
- Collections.unmodifiableList(new ArrayList<Point>(points)),
+ points,
startTimestamp);
}
diff --git a/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java
index 31812549..92a2c8cf 100644
--- a/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java
+++ b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java
@@ -90,6 +90,40 @@ public class TimeSeriesTest {
}
@Test
+ public void testGet_WithOnePointTimeSeries() {
+ TimeSeries cumulativeTimeSeries =
+ TimeSeries.createWithOnePoint(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), POINT_1, TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getStartTimestamp()).isEqualTo(TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getLabelValues())
+ .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2)
+ .inOrder();
+ assertThat(cumulativeTimeSeries.getPoints()).containsExactly(POINT_1).inOrder();
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullLabelValueList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValues"));
+ TimeSeries.createWithOnePoint(null, POINT_1, TIMESTAMP_1);
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullLabelValue() {
+ List<LabelValue> labelValues = Arrays.asList(LABEL_VALUE_1, null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValue"));
+ TimeSeries.createWithOnePoint(labelValues, POINT_1, TIMESTAMP_1);
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullPointList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("point"));
+ TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), null, TIMESTAMP_1);
+ }
+
+ @Test
public void testEquals() {
new EqualsTester()
.addEqualityGroup(