From b29673b307bf08a6a65ad69f28bb4d6b0d1d26e4 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 19 Dec 2024 17:12:13 -0700 Subject: [PATCH 1/4] special case UnitUtils.getDimensionName for mirek Signed-off-by: Cody Cutrer --- .../src/main/java/org/openhab/core/types/util/UnitUtils.java | 5 +++++ .../test/java/org/openhab/core/types/util/UnitUtilsTest.java | 1 + 2 files changed, 6 insertions(+) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java index 2cd3c7d6fa4..2bef28aebe1 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java @@ -27,6 +27,7 @@ import javax.measure.Unit; import javax.measure.UnitConverter; import javax.measure.format.MeasurementParseException; +import javax.measure.quantity.Temperature; import javax.measure.spi.SystemOfUnits; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -112,6 +113,10 @@ public class UnitUtils { * @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits. */ public static @Nullable String getDimensionName(Unit unit) { + // Special cased, because the actual dimension is 1/Temperature + if (unit.equals(Units.MIRED)) { + return Temperature.class.getSimpleName(); + } String compatibleDimension = null; for (Class system : ALL_SYSTEM_OF_UNITS) { for (Field field : system.getDeclaredFields()) { diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java index 3d954b7efc0..b718502dfc4 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java @@ -120,6 +120,7 @@ public void testGetDimensionNameWithDimension() { unit = Objects.requireNonNull(UnitUtils.parseUnit("m")); assertThat(UnitUtils.getDimensionName(unit), is(Length.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(Units.MIRED), is(Temperature.class.getSimpleName())); } @Test From 0a70b779af9ce2c4787697d2a9539274bf501aa5 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 20 Dec 2024 06:36:01 -0700 Subject: [PATCH 2/4] check if the inverse unit is compatible, instead of special case Signed-off-by: Cody Cutrer --- .../main/java/org/openhab/core/types/util/UnitUtils.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java index 2bef28aebe1..f2e89f1cc0e 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java @@ -27,7 +27,6 @@ import javax.measure.Unit; import javax.measure.UnitConverter; import javax.measure.format.MeasurementParseException; -import javax.measure.quantity.Temperature; import javax.measure.spi.SystemOfUnits; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -113,10 +112,6 @@ public class UnitUtils { * @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits. */ public static @Nullable String getDimensionName(Unit unit) { - // Special cased, because the actual dimension is 1/Temperature - if (unit.equals(Units.MIRED)) { - return Temperature.class.getSimpleName(); - } String compatibleDimension = null; for (Class system : ALL_SYSTEM_OF_UNITS) { for (Field field : system.getDeclaredFields()) { @@ -134,7 +129,8 @@ public class UnitUtils { LOGGER.warn("Unit field points to a null value: {}", field); } else if (systemUnit.equals(unit)) { return dimension; - } else if (compatibleDimension == null && systemUnit.isCompatible(unit)) { + } else if (compatibleDimension == null + && (systemUnit.isCompatible(unit) || systemUnit.inverse().isCompatible(unit))) { compatibleDimension = dimension; } } catch (IllegalArgumentException | IllegalAccessException e) { From fcc4b3fc5a0ce24da5d924997c56c00921658a78 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 20 Dec 2024 07:24:11 -0700 Subject: [PATCH 3/4] address review comments Signed-off-by: Cody Cutrer --- .../src/main/java/org/openhab/core/types/util/UnitUtils.java | 2 +- .../test/java/org/openhab/core/types/util/UnitUtilsTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java index f2e89f1cc0e..593b54cc1a9 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java @@ -130,7 +130,7 @@ public class UnitUtils { } else if (systemUnit.equals(unit)) { return dimension; } else if (compatibleDimension == null - && (systemUnit.isCompatible(unit) || systemUnit.inverse().isCompatible(unit))) { + && (systemUnit.isCompatible(unit) || systemUnit.isCompatible(unit.inverse()))) { compatibleDimension = dimension; } } catch (IllegalArgumentException | IllegalAccessException e) { diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java index b718502dfc4..13ee825b906 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java @@ -121,6 +121,9 @@ public void testGetDimensionNameWithDimension() { unit = Objects.requireNonNull(UnitUtils.parseUnit("m")); assertThat(UnitUtils.getDimensionName(unit), is(Length.class.getSimpleName())); assertThat(UnitUtils.getDimensionName(Units.MIRED), is(Temperature.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(Units.KELVIN), is(Temperature.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(SIUnits.CELSIUS), is(Temperature.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(ImperialUnits.FAHRENHEIT), is(Temperature.class.getSimpleName())); } @Test From ea48fdf8b0a66aaa5669aea928883dda68d7eedd Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Sat, 28 Dec 2024 10:30:26 -0700 Subject: [PATCH 4/4] switch to two loops, ensuring a directly compatible dimension is found first Signed-off-by: Cody Cutrer --- .../org/openhab/core/types/util/UnitUtils.java | 16 +++++++++++++--- .../openhab/core/types/util/UnitUtilsTest.java | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java index 593b54cc1a9..cf00a8a44e1 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/types/util/UnitUtils.java @@ -112,6 +112,17 @@ public class UnitUtils { * @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits. */ public static @Nullable String getDimensionName(Unit unit) { + String compatibleDimension = getDirectDimensionName(unit); + if (compatibleDimension == null) { + compatibleDimension = getDimensionName(unit.inverse()); + } + return compatibleDimension; + } + + /** + * Returns the name of a compatible dimension for the given unit, without checking for invertedness + */ + private static @Nullable String getDirectDimensionName(Unit unit) { String compatibleDimension = null; for (Class system : ALL_SYSTEM_OF_UNITS) { for (Field field : system.getDeclaredFields()) { @@ -129,8 +140,7 @@ public class UnitUtils { LOGGER.warn("Unit field points to a null value: {}", field); } else if (systemUnit.equals(unit)) { return dimension; - } else if (compatibleDimension == null - && (systemUnit.isCompatible(unit) || systemUnit.isCompatible(unit.inverse()))) { + } else if (compatibleDimension == null && (systemUnit.isCompatible(unit))) { compatibleDimension = dimension; } } catch (IllegalArgumentException | IllegalAccessException e) { @@ -143,7 +153,7 @@ public class UnitUtils { } } } - return compatibleDimension == null ? null : compatibleDimension; + return compatibleDimension; } /** diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java index 13ee825b906..07b5fc23843 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/types/util/UnitUtilsTest.java @@ -25,6 +25,8 @@ import javax.measure.Unit; import javax.measure.quantity.Angle; import javax.measure.quantity.Dimensionless; +import javax.measure.quantity.ElectricConductance; +import javax.measure.quantity.ElectricResistance; import javax.measure.quantity.Energy; import javax.measure.quantity.Length; import javax.measure.quantity.Power; @@ -124,6 +126,8 @@ public void testGetDimensionNameWithDimension() { assertThat(UnitUtils.getDimensionName(Units.KELVIN), is(Temperature.class.getSimpleName())); assertThat(UnitUtils.getDimensionName(SIUnits.CELSIUS), is(Temperature.class.getSimpleName())); assertThat(UnitUtils.getDimensionName(ImperialUnits.FAHRENHEIT), is(Temperature.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(Units.SIEMENS), is(ElectricConductance.class.getSimpleName())); + assertThat(UnitUtils.getDimensionName(Units.OHM), is(ElectricResistance.class.getSimpleName())); } @Test