Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
* <pre>
Expand All @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}