From 6230d905c61a702132104a33ffbcd2da6870014d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 5 Jun 2015 09:29:09 -0700 Subject: [PATCH] Polish --- .../aggregate/AggregateMetricReader.java | 35 ++++++++----------- .../aggregate/AggregateMetricReaderTests.java | 6 ++-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReader.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReader.java index 5d0a8eab524..af8915e67e7 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReader.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReader.java @@ -29,9 +29,8 @@ import org.springframework.util.StringUtils; * collecting data from many sources in the same form (like a scaled-out application). The * source has metrics with names in the form {@code *.*.counter.**} and * {@code *.*.[anything].**}, and the result has metric names in the form - * {@code aggregate.count.**} and {@code aggregate.[anything].**}. Counters are - * summed and anything else (i.e. gauges) are aggregated by choosing the most recent - * value. + * {@code aggregate.count.**} and {@code aggregate.[anything].**}. Counters are summed and + * anything else (i.e. gauges) are aggregated by choosing the most recent value. * * @author Dave Syer * @since 1.3.0 @@ -60,7 +59,6 @@ public class AggregateMetricReader implements MetricReader { * names) * * Default is "d.d" (we assume there is a global prefix of length 2). - * * @param keyPattern the keyPattern to set */ public void setKeyPattern(String keyPattern) { @@ -68,13 +66,15 @@ public class AggregateMetricReader implements MetricReader { } /** - * Prefix to apply to all output metrics. A period will be appended if not present in the - * provided value. - * + * Prefix to apply to all output metrics. A period will be appended if not present in + * the provided value. * @param prefix the prefix to use (default "aggregator.") */ public void setPrefix(String prefix) { - this.prefix = prefix.endsWith(".") ? prefix : (StringUtils.hasText(prefix) ? prefix + "." : ""); + if (StringUtils.hasText(prefix) && !prefix.endsWith(".")) { + prefix = prefix + "."; + } + this.prefix = prefix; } @Override @@ -139,20 +139,15 @@ public class AggregateMetricReader implements MetricReader { String[] keys = StringUtils.delimitedListToStringArray(name, "."); String[] patterns = StringUtils.delimitedListToStringArray(this.keyPattern, "."); StringBuilder builder = new StringBuilder(); - int index = 0; - for (index = 0; index < patterns.length; index++) { - if ("k".equals(patterns[index])) { - if (builder.length() > 0) { - builder.append("."); - } - builder.append(keys[index]); + for (int i = 0; i < patterns.length; i++) { + if ("k".equals(patterns[i])) { + builder.append(builder.length() > 0 ? "." : ""); + builder.append(keys[i]); } } - for (; index < keys.length; index++) { - if (builder.length() > 0) { - builder.append("."); - } - builder.append(keys[index]); + for (int i = patterns.length; i < keys.length; i++) { + builder.append(builder.length() > 0 ? "." : ""); + builder.append(keys[i]); } return builder.toString(); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReaderTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReaderTests.java index c4412a7ad17..ce203c824f7 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReaderTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/aggregate/AggregateMetricReaderTests.java @@ -16,9 +16,6 @@ package org.springframework.boot.actuate.metrics.aggregate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - import java.util.Date; import org.junit.Test; @@ -26,6 +23,9 @@ import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.writer.Delta; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + /** * Tests for {@link AggregateMetricReader}. *