diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java index 84aa7c800ca..572f677c78d 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java @@ -240,10 +240,15 @@ public boolean equals(@Nullable Object obj) { if (!(obj instanceof QuantityType other)) { return false; } - if (!quantity.getUnit().isCompatible(other.quantity.getUnit()) - && !quantity.getUnit().inverse().isCompatible(other.quantity.getUnit())) { - return false; - } else if (internalCompareTo(other) != 0) { + if (quantity.getUnit().isCompatible(other.quantity.getUnit())) { + if (internalCompareTo(other) != 0) { + return false; + } + } else if (quantity.getUnit().isCompatible(other.quantity.getUnit().inverse())) { + if (internalCompareTo(other.inverse()) != 0) { + return false; + } + } else { return false; } @@ -264,8 +269,6 @@ private int internalCompareTo(QuantityType o) { } else { throw new IllegalArgumentException("Unable to convert to system unit during compare."); } - } else if (quantity.getUnit().inverse().isCompatible(o.quantity.getUnit())) { - return inverse().internalCompareTo(o); } else { throw new IllegalArgumentException("Can not compare incompatible units."); } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunctionTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunctionTest.java index 03031323520..71e2598ada7 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunctionTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunctionTest.java @@ -12,7 +12,7 @@ */ package org.openhab.core.library.types; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.number.IsCloseTo.closeTo; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java index c8da24784db..a3fec4b0a1a 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java @@ -664,4 +664,45 @@ public void testRelativeConversion() { QuantityType f = c.toUnitRelative(ImperialUnits.FAHRENHEIT); assertEquals(1.8, f.doubleValue()); } + + @Test + public void testEquals() { + QuantityType temp1 = new QuantityType<>("293.15 K"); + QuantityType temp2 = new QuantityType<>("20 °C"); + assertTrue(temp1.equals(temp2)); + assertTrue(temp2.equals(temp1)); + temp2 = new QuantityType<>("-5 °C"); + assertFalse(temp1.equals(temp2)); + + temp1 = new QuantityType<>("100000 K"); + temp2 = new QuantityType<>("10 mirek"); + assertTrue(temp1.equals(temp2)); + assertTrue(temp2.equals(temp1)); + temp2 = new QuantityType<>("20 mirek"); + assertFalse(temp1.equals(temp2)); + + temp1 = new QuantityType<>("0.1 MK"); + temp2 = new QuantityType<>("10 mirek"); + assertTrue(temp1.equals(temp2)); + assertTrue(temp2.equals(temp1)); + temp2 = new QuantityType<>("20 mirek"); + assertFalse(temp1.equals(temp2)); + } + + @Test + public void testCompareTo() { + QuantityType temp1 = new QuantityType<>("293.15 K"); + QuantityType temp2 = new QuantityType<>("20 °C"); + assertEquals(0, temp1.compareTo(temp2)); + temp2 = new QuantityType<>("-5 °C"); + assertEquals(1, temp1.compareTo(temp2)); + temp2 = new QuantityType<>("50 °C"); + assertEquals(-1, temp1.compareTo(temp2)); + + QuantityType temp3 = new QuantityType<>("100000 K"); + QuantityType temp4 = new QuantityType<>("10 mirek"); + assertThrows(IllegalArgumentException.class, () -> { + temp3.compareTo(temp4); + }); + } }