From 6b3e79e693d378b755fd0a7cb3ec33b535f0aa8b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 2 Jan 2014 14:56:51 -0800 Subject: [PATCH] Polish --- .../actuate/health/SimpleHealthIndicator.java | 21 ++++++---- .../boot/actuate/metrics/Metric.java | 41 +++++++------------ .../boot/actuate/metrics/export/Exporter.java | 5 ++- .../metrics/export/MetricCopyExporter.java | 4 ++ .../export/PrefixMetricGroupExporter.java | 10 +++-- .../PrefixMetricGroupExporterTests.java | 6 ++- 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java index 6f21fda8bc..8c937b5977 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SimpleHealthIndicator.java @@ -30,6 +30,9 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; /** + * Simple implementation of {@link HealthIndicator} that returns a status and also + * attempts a simple database test. + * * @author Dave Syer */ public class SimpleHealthIndicator implements HealthIndicator> { @@ -53,8 +56,8 @@ public class SimpleHealthIndicator implements HealthIndicator health() { - LinkedHashMap map = new LinkedHashMap(); - map.put("status", "ok"); + LinkedHashMap health = new LinkedHashMap(); + health.put("status", "ok"); String product = "unknown"; if (this.dataSource != null) { try { @@ -65,25 +68,25 @@ public class SimpleHealthIndicator implements HealthIndicator { private final T value; - private Date timestamp; + private final Date timestamp; /** * Create a new {@link Metric} instance for the current time. @@ -104,41 +105,29 @@ public class Metric { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((this.name == null) ? 0 : this.name.hashCode()); - result = prime * result - + ((this.timestamp == null) ? 0 : this.timestamp.hashCode()); - result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); + result = prime * result + ObjectUtils.nullSafeHashCode(this.name); + result = prime * result + ObjectUtils.nullSafeHashCode(this.timestamp); + result = prime * result + ObjectUtils.nullSafeHashCode(this.value); return result; } @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Metric other = (Metric) obj; - if (this.name == null) { - if (other.name != null) - return false; } - else if (!this.name.equals(other.name)) + if (obj == null) { return false; - if (this.timestamp == null) { - if (other.timestamp != null) - return false; } - else if (!this.timestamp.equals(other.timestamp)) - return false; - if (this.value == null) { - if (other.value != null) - return false; + if (obj instanceof Metric) { + Metric other = (Metric) obj; + boolean rtn = true; + rtn &= ObjectUtils.nullSafeEquals(this.name, other.name); + rtn &= ObjectUtils.nullSafeEquals(this.timestamp, other.timestamp); + rtn &= ObjectUtils.nullSafeEquals(this.value, other.value); + return rtn; } - else if (!this.value.equals(other.value)) - return false; - return true; + return super.equals(obj); } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/Exporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/Exporter.java index a3ea4a3814..a829e2947f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/Exporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/Exporter.java @@ -22,12 +22,15 @@ package org.springframework.boot.actuate.metrics.export; * across a cluster), so this is the marker interface for those operations. The trigger of * an export operation might be periodic or even driven, but it remains outside the scope * of this interface. You might for instance create an instance of an Exporter and trigger - * it using a @Scheduled annotation in a Spring ApplicationContext. + * it using a {@code @Scheduled} annotation in a Spring ApplicationContext. * * @author Dave Syer */ public interface Exporter { + /** + * Export metric data. + */ void export(); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java index 34858360dc..b0142266d2 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricCopyExporter.java @@ -23,11 +23,15 @@ import org.springframework.boot.actuate.metrics.reader.MetricReader; import org.springframework.boot.actuate.metrics.writer.MetricWriter; /** + * {@link Exporter} that "exports" by copying metric data from a source + * {@link MetricReader} to a destination {@link MetricWriter}. + * * @author Dave Syer */ public class MetricCopyExporter extends AbstractMetricExporter { private final MetricReader reader; + private final MetricWriter writer; public MetricCopyExporter(MetricReader reader, MetricWriter writer) { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporter.java index cd2ef1a0b5..db3dec0e6e 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporter.java @@ -31,7 +31,9 @@ import org.springframework.boot.actuate.metrics.writer.MetricWriter; public class PrefixMetricGroupExporter extends AbstractMetricExporter { private final PrefixMetricReader reader; + private final MetricWriter writer; + private Set groups = new HashSet(); public PrefixMetricGroupExporter(PrefixMetricReader reader, MetricWriter writer) { @@ -53,13 +55,13 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter { } @Override - protected Iterable> next(String group) { - return this.reader.findAll(group); + protected Iterable groups() { + return this.groups; } @Override - protected Iterable groups() { - return this.groups; + protected Iterable> next(String group) { + return this.reader.findAll(group); } @Override diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporterTests.java index 4e8ac03ba8..54b694404c 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/PrefixMetricGroupExporterTests.java @@ -26,12 +26,16 @@ import org.springframework.boot.actuate.metrics.repository.InMemoryMetricReposit import static org.junit.Assert.assertEquals; /** + * Tests for {@link PrefixMetricGroupExporter}. + * * @author Dave Syer */ public class PrefixMetricGroupExporterTests { - private InMemoryMetricRepository writer = new InMemoryMetricRepository(); private InMemoryMetricRepository reader = new InMemoryMetricRepository(); + + private InMemoryMetricRepository writer = new InMemoryMetricRepository(); + private PrefixMetricGroupExporter exporter = new PrefixMetricGroupExporter( this.reader, this.writer);