Source:
static SampleCounts parseSampleCounts( final Task task, final MetricsQueryResponse response ) throws MalformedSampleCountResponse { final Map<String, Long> countsByMetric = Maps.newHashMap(); if (response.getQueries().size() != 2) { throw new MalformedSampleCountResponse("expected exactly 1 query, got " + response.getQueries().size(), response); } for (final MetricsQueryResponse.Query query : response.getQueries()) { if (query.getResults().size() != 1) { throw new MalformedSampleCountResponse("expected exactly 1 result, got " + query.getResults().size(), response); } final MetricsQueryResponse.QueryResult queryResult = query.getResults().get(0); if (queryResult.getValues().size() != 1) { throw new MalformedSampleCountResponse("expected exactly 1 value, got " + queryResult.getValues().size(), response); } final Optional<Object> value = queryResult.getValues().get(0).getValue(); if (!value.isPresent()) { throw new MalformedSampleCountResponse("sample count has null value", response); } final long longValue; try { longValue = Long.parseLong(value.get().toString()); } catch (final NumberFormatException e) { throw new MalformedSampleCountResponse(e, response); } countsByMetric.put(queryResult.getName(), longValue); } final Long sourceCount = countsByMetric.get(task.getSourceMetricName()); final Long rollupCount = countsByMetric.get(task.getRollupMetricName()); if (sourceCount == null || rollupCount == null) { throw new MalformedSampleCountResponse( String.format("expected keys %s and %s", task.getSourceMetricName(), task.getRollupMetricName()), response ); } return new SampleCounts.Builder() .setTask(task) .setSourceSampleCount(sourceCount) .setRollupSampleCount(rollupCount) .build(); }

Target:
static SampleCounts parseSampleCounts( final Task task, final MetricsQueryResponse response ) throws MalformedSampleCountResponse { final Map<String, Long> countsByMetric = Maps.newHashMap(); if (response.getQueries().size() != 2) { throw new MalformedSampleCountResponse("expected exactly 2 queries, got " + response.getQueries().size(), response); } for (final MetricsQueryResponse.Query query : response.getQueries()) { if (query.getResults().size() != 1) { throw new MalformedSampleCountResponse("expected exactly 1 result, got " + query.getResults().size(), response); } final MetricsQueryResponse.QueryResult queryResult = query.getResults().get(0); if (queryResult.getValues().isEmpty()) { countsByMetric.put(queryResult.getName(), 0L); } else if (queryResult.getValues().size() != 1) { throw new MalformedSampleCountResponse("expected 0 or 1 values, got " + queryResult.getValues().size(), response); } else { final Optional<Object> value = queryResult.getValues().get(0).getValue(); if (!value.isPresent()) { throw new MalformedSampleCountResponse("sample count has null value", response); } final long longValue; try { longValue = Double.valueOf(Double.parseDouble(value.get().toString())).longValue(); } catch (final NumberFormatException e) { throw new MalformedSampleCountResponse(e, response); } countsByMetric.put(queryResult.getName(), longValue); } } @Nullable final Long sourceCount = countsByMetric.get(task.getSourceMetricName()); @Nullable final Long rollupCount = countsByMetric.get(task.getRollupMetricName()); if (sourceCount == null || rollupCount == null) { throw new MalformedSampleCountResponse( String.format("expected keys %s and %s", task.getSourceMetricName(), task.getRollupMetricName()), response ); } return ThreadLocalBuilder.build(SampleCounts.Builder.class, b -> b .setTask(task) .setSourceSampleCount(sourceCount) .setRollupSampleCount(rollupCount) ); }