2121import io .opencensus .common .Function ;
2222import io .opencensus .common .Functions ;
2323import io .opencensus .common .Timestamp ;
24- import io .opencensus .internal .Utils ;
2524import io .opencensus .stats .Aggregation .Count ;
2625import io .opencensus .stats .Aggregation .Distribution ;
2726import io .opencensus .stats .Aggregation .LastValue ;
@@ -210,31 +209,36 @@ private static void checkWindow(
210209 new Function <View .AggregationWindow .Cumulative , Void >() {
211210 @ Override
212211 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 );
216214 return null ;
217215 }
218216 },
219217 new Function <View .AggregationWindow .Interval , Void >() {
220218 @ Override
221219 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 );
225222 return null ;
226223 }
227224 },
228225 Functions .</*@Nullable*/ Void >throwAssertionError ());
229226 }
230227
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+
231235 private static String createErrorMessageForWindow (
232236 View .AggregationWindow window , AggregationWindowData windowData ) {
233237 return "AggregationWindow and AggregationWindowData types mismatch. "
234238 + "AggregationWindow: "
235- + window
239+ + window . getClass (). getSimpleName ()
236240 + " AggregationWindowData: "
237- + windowData ;
241+ + windowData . getClass (). getSimpleName () ;
238242 }
239243
240244 private static void checkAggregation (
@@ -247,18 +251,16 @@ public Void apply(Sum arg) {
247251 new Function <MeasureDouble , Void >() {
248252 @ Override
249253 public Void apply (MeasureDouble arg ) {
250- Utils .checkArgument (
251- aggregationData instanceof SumDataDouble ,
252- createErrorMessageForAggregation (aggregation , aggregationData ));
254+ throwIfAggregationMismatch (
255+ aggregationData instanceof SumDataDouble , aggregation , aggregationData );
253256 return null ;
254257 }
255258 },
256259 new Function <MeasureLong , Void >() {
257260 @ Override
258261 public Void apply (MeasureLong arg ) {
259- Utils .checkArgument (
260- aggregationData instanceof SumDataLong ,
261- createErrorMessageForAggregation (aggregation , aggregationData ));
262+ throwIfAggregationMismatch (
263+ aggregationData instanceof SumDataLong , aggregation , aggregationData );
262264 return null ;
263265 }
264266 },
@@ -269,18 +271,16 @@ public Void apply(MeasureLong arg) {
269271 new Function <Count , Void >() {
270272 @ Override
271273 public Void apply (Count arg ) {
272- Utils .checkArgument (
273- aggregationData instanceof CountData ,
274- createErrorMessageForAggregation (aggregation , aggregationData ));
274+ throwIfAggregationMismatch (
275+ aggregationData instanceof CountData , aggregation , aggregationData );
275276 return null ;
276277 }
277278 },
278279 new Function <Distribution , Void >() {
279280 @ Override
280281 public Void apply (Distribution arg ) {
281- Utils .checkArgument (
282- aggregationData instanceof DistributionData ,
283- createErrorMessageForAggregation (aggregation , aggregationData ));
282+ throwIfAggregationMismatch (
283+ aggregationData instanceof DistributionData , aggregation , aggregationData );
284284 return null ;
285285 }
286286 },
@@ -291,18 +291,18 @@ public Void apply(LastValue arg) {
291291 new Function <MeasureDouble , Void >() {
292292 @ Override
293293 public Void apply (MeasureDouble arg ) {
294- Utils . checkArgument (
294+ throwIfAggregationMismatch (
295295 aggregationData instanceof LastValueDataDouble ,
296- createErrorMessageForAggregation (aggregation , aggregationData ));
296+ aggregation ,
297+ aggregationData );
297298 return null ;
298299 }
299300 },
300301 new Function <MeasureLong , Void >() {
301302 @ Override
302303 public Void apply (MeasureLong arg ) {
303- Utils .checkArgument (
304- aggregationData instanceof LastValueDataLong ,
305- createErrorMessageForAggregation (aggregation , aggregationData ));
304+ throwIfAggregationMismatch (
305+ aggregationData instanceof LastValueDataLong , aggregation , aggregationData );
306306 return null ;
307307 }
308308 },
@@ -317,23 +317,32 @@ public Void apply(Aggregation arg) {
317317 // we need to continue supporting Mean, since it could still be used by users and some
318318 // deprecated RPC views.
319319 if (arg instanceof Aggregation .Mean ) {
320- Utils . checkArgument (
320+ throwIfAggregationMismatch (
321321 aggregationData instanceof AggregationData .MeanData ,
322- createErrorMessageForAggregation (aggregation , aggregationData ));
322+ aggregation ,
323+ aggregationData );
323324 return null ;
324325 }
325326 throw new AssertionError ();
326327 }
327328 });
328329 }
329330
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+
330339 private static String createErrorMessageForAggregation (
331340 Aggregation aggregation , AggregationData aggregationData ) {
332341 return "Aggregation and AggregationData types mismatch. "
333342 + "Aggregation: "
334- + aggregation
343+ + aggregation . getClass (). getSimpleName ()
335344 + " AggregationData: "
336- + aggregationData ;
345+ + aggregationData . getClass (). getSimpleName () ;
337346 }
338347
339348 /**
0 commit comments