Source:
<code>public Map<MetricQuery, List<MetricScanner>> getMetricScanners(List<MetricQuery> queries) { requireNotDisposed(); requireArgument(queries != null, "Metric Queries cannot be null."); _logger.trace("Active Threads in the pool = " + ((ThreadPoolExecutor) _executorService).getActiveCount()); double percentage = 0.05; long start = System.currentTimeMillis(); Map<MetricQuery, List<MetricScanner>> scannerMap = new HashMap<>(); for (MetricQuery query : queries) { Long chunkTime = (Long) Math.round((query.getEndTimestamp() - query.getStartTimestamp()) * percentage); int additionalChunks = -1; scannerMap.put(query, new ArrayList<>()); Map<MetricQuery, List<Metric>> metricMap; MetricQuery miniQuery; Long startTime; Long stopTime; do { additionalChunks++; startTime = query.getStartTimestamp() + chunkTime * additionalChunks; <START> stopTime = Math.max(query.getEndTimestamp(), startTime + chunkTime); <END> miniQuery = new MetricQuery(query.getScope(), query.getMetric(), query.getTags(), startTime, stopTime); List<MetricQuery> miniQueries = new ArrayList<>(); miniQueries.add(miniQuery); metricMap = getMetrics(miniQueries); } while (!metricMap.containsKey(miniQuery) && (startTime + chunkTime) < query.getEndTimestamp()); if (!metricMap.containsKey(miniQuery)) {scannerMap.put(miniQuery, new ArrayList<>()); continue;} List<Metric> miniMetrics = metricMap.get(miniQuery); for (Metric miniMetric : miniMetrics) { MetricScanner scanner = new MetricScanner(miniMetric, query, this, stopTime); scannerMap.get(query).add(scanner); } } _logger.debug("Time to get Scanners = " + (System.currentTimeMillis() - start)); return scannerMap; }</code><technical_language>is logical reasoning for Math.max here? endTimestamp greater start+chunk time, for iteration. If this is chunk of 1 hr in a 24 hr period intending start - 0, stop -1, start -1, stop-2</technical_language>

Target:
public Map<MetricQuery, List<MetricScanner>> getMetricScanners(List<MetricQuery> queries) { requireNotDisposed(); requireArgument(queries != null, "Metric Queries cannot be null."); _logger.trace("Active Threads in the pool = " + ((ThreadPoolExecutor) _executorService).getActiveCount()); double percentage = 0.05; long start = System.currentTimeMillis(); Map<MetricQuery, List<MetricScanner>> scannerMap = new HashMap<>(); for (MetricQuery query : queries) { Long chunkTime = (Long) Math.round((query.getEndTimestamp() - query.getStartTimestamp()) * percentage); int additionalChunks = -1; scannerMap.put(query, new ArrayList<>()); Map<MetricQuery, List<Metric>> metricMap; MetricQuery miniQuery; Long startTime; Long stopTime; do { additionalChunks++; startTime = query.getStartTimestamp() + chunkTime * additionalChunks; stopTime = Math.min(query.getEndTimestamp(), startTime + chunkTime); miniQuery = new MetricQuery(query.getScope(), query.getMetric(), query.getTags(), startTime, stopTime); List<MetricQuery> miniQueries = new ArrayList<>(); miniQueries.add(miniQuery); metricMap = getMetrics(miniQueries); } while (!metricMap.containsKey(miniQuery) && (startTime + chunkTime) < query.getEndTimestamp()); if (!metricMap.containsKey(miniQuery)) {scannerMap.put(miniQuery, new ArrayList<>()); continue;} List<Metric> miniMetrics = metricMap.get(miniQuery); for (Metric miniMetric : miniMetrics) { MetricScanner scanner = new MetricScanner(miniMetric, query, this, stopTime); scannerMap.get(query).add(scanner); } } _logger.debug("Time to get Scanners = " + (System.currentTimeMillis() - start)); return scannerMap; }