From cd68b481129dce90e401d4a1d3299a672acc9c5b Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Tue, 24 Dec 2024 16:21:13 +0100 Subject: [PATCH] fix: proper factor for deci prefix --- .../sustainability/power/SensorUnit.java | 18 ++++++++++-------- .../sustainability/power/SensorUnitTest.java | 10 +++++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/metadata/src/main/java/net/laprun/sustainability/power/SensorUnit.java b/metadata/src/main/java/net/laprun/sustainability/power/SensorUnit.java index 49555a8..79d3682 100644 --- a/metadata/src/main/java/net/laprun/sustainability/power/SensorUnit.java +++ b/metadata/src/main/java/net/laprun/sustainability/power/SensorUnit.java @@ -10,19 +10,27 @@ public class SensorUnit { + /** + * Note that for simplicity's sake, we're not supporting deca prefix since it would make the prefix identification logic + * more complex and it's not a very used prefix. Might revisit as needed. + */ private final static Map prefixesToFactors = Map.of( "n", 1e-9, "µ", 1e-6, "m", 1e-3, "c", 1e-2, - "d", 10.0, + "d", 1e-1, "h", 100.0, "k", 1e3, "M", 1e6, "G", 1e9); private final static Map baseUnits = new HashMap<>(); + public static final SensorUnit W = baseUnitFor("W"); + public static final SensorUnit J = baseUnitFor("J"); + public static final SensorUnit decimalPercentage = baseUnitFor("decimal percentage"); private final static Map knownUnits = new HashMap<>(); - + public static final SensorUnit mW = SensorUnit.of("mW"); + public static final SensorUnit µJ = SensorUnit.of("µJ"); private final String symbol; private final SensorUnit base; private final double factor; @@ -121,10 +129,4 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hashCode(symbol); } - - public static final SensorUnit W = baseUnitFor("W"); - public static final SensorUnit mW = SensorUnit.of("mW"); - public static final SensorUnit J = baseUnitFor("J"); - public static final SensorUnit µJ = SensorUnit.of("µJ"); - public static final SensorUnit decimalPercentage = baseUnitFor("decimal percentage"); } diff --git a/metadata/src/test/java/net/laprun/sustainability/power/SensorUnitTest.java b/metadata/src/test/java/net/laprun/sustainability/power/SensorUnitTest.java index 691d0c5..3bd71b5 100644 --- a/metadata/src/test/java/net/laprun/sustainability/power/SensorUnitTest.java +++ b/metadata/src/test/java/net/laprun/sustainability/power/SensorUnitTest.java @@ -18,6 +18,10 @@ void ofShouldWork() { assertThat(su).isEqualTo(SensorUnit.decimalPercentage); assertThat(su.factor()).isEqualTo(1); assertThat(su.base()).isEqualTo(SensorUnit.decimalPercentage); + + su = SensorUnit.of("dW"); + assertThat(su.base()).isEqualTo(SensorUnit.W); + assertThat(su.factor()).isEqualTo(0.1); } @Test @@ -47,9 +51,9 @@ void conversionFactorShouldWork() { assertEquals(1, SensorUnit.J.conversionFactorTo(SensorUnit.J)); assertEquals(1, SensorUnit.decimalPercentage.conversionFactorTo(SensorUnit.decimalPercentage)); assertEquals(1, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("mW"))); - assertEquals(1e-3, SensorUnit.of("mW").conversionFactorTo(SensorUnit.W), 0.0001); - assertEquals(1e3, SensorUnit.of("W").conversionFactorTo(SensorUnit.of("mW")), 0.0001); - assertEquals(1e3, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("µW")), 0.0001); + assertEquals(0.001, SensorUnit.of("mW").conversionFactorTo(SensorUnit.W), 0.0001); + assertEquals(1000, SensorUnit.of("W").conversionFactorTo(SensorUnit.of("mW")), 0.0001); + assertEquals(1000, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("µW")), 0.0001); assertEquals(1e-3, SensorUnit.of("µW").conversionFactorTo(SensorUnit.of("mW")), 0.0001); assertEquals(0.1, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("cW")), 0.0001); assertEquals(10, SensorUnit.of("cW").conversionFactorTo(SensorUnit.of("mW")), 0.0001);