21
21
import io .opencensus .common .Function ;
22
22
import io .opencensus .common .Functions ;
23
23
import io .opencensus .common .Timestamp ;
24
- import io .opencensus .internal .Utils ;
25
24
import io .opencensus .stats .Aggregation .Count ;
26
25
import io .opencensus .stats .Aggregation .Distribution ;
27
26
import io .opencensus .stats .Aggregation .LastValue ;
@@ -210,31 +209,36 @@ private static void checkWindow(
210
209
new Function <View .AggregationWindow .Cumulative , Void >() {
211
210
@ Override
212
211
public Void apply (View .AggregationWindow .Cumulative arg ) {
213
- Utils .checkArgument (
214
- windowData instanceof AggregationWindowData .CumulativeData ,
215
- createErrorMessageForWindow (arg , windowData ));
212
+ throwIfWindowMismatch (
213
+ windowData instanceof AggregationWindowData .CumulativeData , arg , windowData );
216
214
return null ;
217
215
}
218
216
},
219
217
new Function <View .AggregationWindow .Interval , Void >() {
220
218
@ Override
221
219
public Void apply (View .AggregationWindow .Interval arg ) {
222
- Utils .checkArgument (
223
- windowData instanceof AggregationWindowData .IntervalData ,
224
- createErrorMessageForWindow (arg , windowData ));
220
+ throwIfWindowMismatch (
221
+ windowData instanceof AggregationWindowData .IntervalData , arg , windowData );
225
222
return null ;
226
223
}
227
224
},
228
225
Functions .</*@Nullable*/ Void >throwAssertionError ());
229
226
}
230
227
228
+ private static void throwIfWindowMismatch (
229
+ boolean isValid , View .AggregationWindow window , AggregationWindowData windowData ) {
230
+ if (!isValid ) {
231
+ throw new IllegalArgumentException (createErrorMessageForWindow (window , windowData ));
232
+ }
233
+ }
234
+
231
235
private static String createErrorMessageForWindow (
232
236
View .AggregationWindow window , AggregationWindowData windowData ) {
233
237
return "AggregationWindow and AggregationWindowData types mismatch. "
234
238
+ "AggregationWindow: "
235
- + window
239
+ + window . getClass (). getSimpleName ()
236
240
+ " AggregationWindowData: "
237
- + windowData ;
241
+ + windowData . getClass (). getSimpleName () ;
238
242
}
239
243
240
244
private static void checkAggregation (
@@ -247,18 +251,16 @@ public Void apply(Sum arg) {
247
251
new Function <MeasureDouble , Void >() {
248
252
@ Override
249
253
public Void apply (MeasureDouble arg ) {
250
- Utils .checkArgument (
251
- aggregationData instanceof SumDataDouble ,
252
- createErrorMessageForAggregation (aggregation , aggregationData ));
254
+ throwIfAggregationMismatch (
255
+ aggregationData instanceof SumDataDouble , aggregation , aggregationData );
253
256
return null ;
254
257
}
255
258
},
256
259
new Function <MeasureLong , Void >() {
257
260
@ Override
258
261
public Void apply (MeasureLong arg ) {
259
- Utils .checkArgument (
260
- aggregationData instanceof SumDataLong ,
261
- createErrorMessageForAggregation (aggregation , aggregationData ));
262
+ throwIfAggregationMismatch (
263
+ aggregationData instanceof SumDataLong , aggregation , aggregationData );
262
264
return null ;
263
265
}
264
266
},
@@ -269,18 +271,16 @@ public Void apply(MeasureLong arg) {
269
271
new Function <Count , Void >() {
270
272
@ Override
271
273
public Void apply (Count arg ) {
272
- Utils .checkArgument (
273
- aggregationData instanceof CountData ,
274
- createErrorMessageForAggregation (aggregation , aggregationData ));
274
+ throwIfAggregationMismatch (
275
+ aggregationData instanceof CountData , aggregation , aggregationData );
275
276
return null ;
276
277
}
277
278
},
278
279
new Function <Distribution , Void >() {
279
280
@ Override
280
281
public Void apply (Distribution arg ) {
281
- Utils .checkArgument (
282
- aggregationData instanceof DistributionData ,
283
- createErrorMessageForAggregation (aggregation , aggregationData ));
282
+ throwIfAggregationMismatch (
283
+ aggregationData instanceof DistributionData , aggregation , aggregationData );
284
284
return null ;
285
285
}
286
286
},
@@ -291,18 +291,18 @@ public Void apply(LastValue arg) {
291
291
new Function <MeasureDouble , Void >() {
292
292
@ Override
293
293
public Void apply (MeasureDouble arg ) {
294
- Utils . checkArgument (
294
+ throwIfAggregationMismatch (
295
295
aggregationData instanceof LastValueDataDouble ,
296
- createErrorMessageForAggregation (aggregation , aggregationData ));
296
+ aggregation ,
297
+ aggregationData );
297
298
return null ;
298
299
}
299
300
},
300
301
new Function <MeasureLong , Void >() {
301
302
@ Override
302
303
public Void apply (MeasureLong arg ) {
303
- Utils .checkArgument (
304
- aggregationData instanceof LastValueDataLong ,
305
- createErrorMessageForAggregation (aggregation , aggregationData ));
304
+ throwIfAggregationMismatch (
305
+ aggregationData instanceof LastValueDataLong , aggregation , aggregationData );
306
306
return null ;
307
307
}
308
308
},
@@ -317,23 +317,32 @@ public Void apply(Aggregation arg) {
317
317
// we need to continue supporting Mean, since it could still be used by users and some
318
318
// deprecated RPC views.
319
319
if (arg instanceof Aggregation .Mean ) {
320
- Utils . checkArgument (
320
+ throwIfAggregationMismatch (
321
321
aggregationData instanceof AggregationData .MeanData ,
322
- createErrorMessageForAggregation (aggregation , aggregationData ));
322
+ aggregation ,
323
+ aggregationData );
323
324
return null ;
324
325
}
325
326
throw new AssertionError ();
326
327
}
327
328
});
328
329
}
329
330
331
+ private static void throwIfAggregationMismatch (
332
+ boolean isValid , Aggregation aggregation , AggregationData aggregationData ) {
333
+ if (!isValid ) {
334
+ throw new IllegalArgumentException (
335
+ createErrorMessageForAggregation (aggregation , aggregationData ));
336
+ }
337
+ }
338
+
330
339
private static String createErrorMessageForAggregation (
331
340
Aggregation aggregation , AggregationData aggregationData ) {
332
341
return "Aggregation and AggregationData types mismatch. "
333
342
+ "Aggregation: "
334
- + aggregation
343
+ + aggregation . getClass (). getSimpleName ()
335
344
+ " AggregationData: "
336
- + aggregationData ;
345
+ + aggregationData . getClass (). getSimpleName () ;
337
346
}
338
347
339
348
/**
0 commit comments