Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit e8e4d4b

Browse files
authored
Add missing default function for match() (#587)
1 parent 36a87b8 commit e8e4d4b

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

core/src/main/java/io/opencensus/stats/Measure.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public abstract class Measure {
3737
*/
3838
public abstract <T> T match(
3939
Function<? super MeasureDouble, T> p0,
40-
Function<? super MeasureLong, T> p1);
40+
Function<? super MeasureLong, T> p1,
41+
Function<? super Measure, T> defaultFunction);
4142

4243
/**
4344
* Name of measure, as a {@code String}. Should be a ASCII string with a length no greater than
@@ -91,7 +92,10 @@ public static MeasureDouble create(String name, String description, String unit)
9192
}
9293

9394
@Override
94-
public <T> T match(Function<? super MeasureDouble, T> p0, Function<? super MeasureLong, T> p1) {
95+
public <T> T match(
96+
Function<? super MeasureDouble, T> p0,
97+
Function<? super MeasureLong, T> p1,
98+
Function<? super Measure, T> defaultFunction) {
9599
return p0.apply(this);
96100
}
97101

@@ -127,7 +131,10 @@ public static MeasureLong create(String name, String description, String unit) {
127131
}
128132

129133
@Override
130-
public <T> T match(Function<? super MeasureDouble, T> p0, Function<? super MeasureLong, T> p1) {
134+
public <T> T match(
135+
Function<? super MeasureDouble, T> p0,
136+
Function<? super MeasureLong, T> p1,
137+
Function<? super Measure, T> defaultFunction) {
131138
return p1.apply(this);
132139
}
133140

core/src/main/java/io/opencensus/stats/Measurement.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public abstract class Measurement {
3030
* Applies the given match function to the underlying data type.
3131
*/
3232
public abstract <T> T match(
33-
Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1);
33+
Function<? super MeasurementDouble, T> p0,
34+
Function<? super MeasurementLong, T> p1,
35+
Function<? super Measurement, T> defaultFunction);
3436

3537
/**
3638
* Extracts the measured {@link Measure}.
@@ -60,7 +62,9 @@ public static MeasurementDouble create(MeasureDouble measure, double value) {
6062

6163
@Override
6264
public <T> T match(
63-
Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1) {
65+
Function<? super MeasurementDouble, T> p0,
66+
Function<? super MeasurementLong, T> p1,
67+
Function<? super Measurement, T> defaultFunction) {
6468
return p0.apply(this);
6569
}
6670
}
@@ -84,7 +88,9 @@ public static MeasurementLong create(MeasureLong measure, long value) {
8488

8589
@Override
8690
public <T> T match(
87-
Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1) {
91+
Function<? super MeasurementDouble, T> p0,
92+
Function<? super MeasurementLong, T> p1,
93+
Function<? super Measurement, T> defaultFunction) {
8894
return p1.apply(this);
8995
}
9096
}

core/src/test/java/io/opencensus/stats/MeasureTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.common.collect.Lists;
2122
import com.google.common.testing.EqualsTester;
23+
import io.opencensus.common.Function;
24+
import io.opencensus.common.Functions;
25+
import io.opencensus.stats.Measure.MeasureDouble;
26+
import io.opencensus.stats.Measure.MeasureLong;
2227
import java.util.Arrays;
28+
import java.util.List;
2329
import org.junit.Rule;
2430
import org.junit.Test;
2531
import org.junit.rules.ExpectedException;
@@ -115,6 +121,32 @@ public void testMeasureLongEquals() {
115121
.testEquals();
116122
}
117123

124+
@Test
125+
public void testMatch() {
126+
List<Measure> measures = Arrays.asList(
127+
MeasureDouble.create("measure1", "description", "1"),
128+
MeasureLong.create("measure2", "description", "1"));
129+
List<String> outputs = Lists.newArrayList();
130+
for (Measure measure : measures) {
131+
outputs.add(
132+
measure.match(
133+
new Function<MeasureDouble, String>() {
134+
@Override
135+
public String apply(MeasureDouble arg) {
136+
return "double";
137+
}
138+
},
139+
new Function<MeasureLong, String>() {
140+
@Override
141+
public String apply(MeasureLong arg) {
142+
return "long";
143+
}
144+
},
145+
Functions.<String>throwAssertionError()));
146+
}
147+
assertThat(outputs).containsExactly("double", "long").inOrder();
148+
}
149+
118150
@Test
119151
public void testMeasureDoubleIsNotEqualToMeasureLong() {
120152
assertThat(Measure.MeasureDouble.create("name", "description", "bit/s"))

core_impl/src/main/java/io/opencensus/implcore/stats/MeasureToViewMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.Multimap;
2121
import io.opencensus.common.Clock;
2222
import io.opencensus.common.Function;
23+
import io.opencensus.common.Functions;
2324
import io.opencensus.common.Timestamp;
2425
import io.opencensus.stats.MeasureMap;
2526
import io.opencensus.stats.Measurement;
@@ -101,7 +102,8 @@ synchronized void record(TagContext tags, MeasureMap stats, Timestamp timestamp)
101102
for (MutableViewData view : views) {
102103
measurement.match(
103104
new RecordDoubleValueFunc(tags, view, timestamp),
104-
new RecordLongValueFunc(tags, view, timestamp));
105+
new RecordLongValueFunc(tags, view, timestamp),
106+
Functions.<Void>throwAssertionError());
105107
}
106108
}
107109
}

0 commit comments

Comments
 (0)