From e8b3f87184db6caceebce90595f610b273c0be35 Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Sun, 12 Nov 2023 05:43:47 -0500 Subject: [PATCH] Added equals and hashcode. Changed code to catch an empty String (#897) Signed-off-by: dhoard --- .../metrics/model/snapshots/Unit.java | 19 ++++++- .../metrics/model/snapshots/UnitTest.java | 51 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java index d73980a97..94827c64c 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java @@ -1,5 +1,7 @@ package io.prometheus.metrics.model.snapshots; +import java.util.Objects; + /** * Some pre-defined units for convenience. You can create your own units with *
@@ -23,13 +25,13 @@ public class Unit {
     public static final Unit AMPERES = new Unit("amperes");
 
     public Unit(String name) {
-        this.name = name;
         if (name == null) {
             throw new NullPointerException("Unit name cannot be null.");
         }
-        if (name.isEmpty()) {
+        if (name.trim().isEmpty()) {
             throw new IllegalArgumentException("Unit name cannot be empty.");
         }
+        this.name = name.trim();
     }
 
     @Override
@@ -52,4 +54,17 @@ public static double secondsToMillis(double seconds) {
     public static double kiloBytesToBytes(double kilobytes) {
         return kilobytes * 1024;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        Unit unit = (Unit) o;
+        return Objects.equals(name, unit.name);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name);
+    }
 }
diff --git a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java
new file mode 100644
index 000000000..f88458cc3
--- /dev/null
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java
@@ -0,0 +1,51 @@
+package io.prometheus.metrics.model.snapshots;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.junit.Assert.fail;
+
+public class UnitTest {
+
+    @Test
+    public void testEmpty() {
+        try {
+            new Unit(" ");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+    }
+
+    @Test
+    public void testEquals1() {
+        Unit unit1 = Unit.BYTES;
+        Unit unit2 = new Unit("bytes");
+
+        Assert.assertEquals(unit2, unit1);
+    }
+
+    @Test
+    public void testEquals2() {
+        Unit unit1 = new Unit("bytes ");
+        Unit unit2 = new Unit("bytes");
+
+        Assert.assertEquals(unit2, unit1);
+    }
+
+    @Test
+    public void testEquals3() {
+        Unit unit1 = new Unit(" bytes");
+        Unit unit2 = new Unit("bytes");
+
+        Assert.assertEquals(unit2, unit1);
+    }
+
+    @Test
+    public void testEquals4() {
+        Unit unit1 = new Unit(" bytes ");
+        Unit unit2 = new Unit("bytes");
+
+        Assert.assertEquals(unit2, unit1);
+    }
+}