Skip to content

Commit

Permalink
Fix comparing QuantityType with inverted dimensions (#4571)
Browse files Browse the repository at this point in the history
* fix comparing inverted dimensions
* remove tolerance
* use BigDecimal in comparison
* remove comparing inverted dimensions
* equals, add tests

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
  • Loading branch information
mherwege authored Feb 15, 2025
1 parent b5f862c commit 38a1380
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,45 @@ public void testRelativeConversion() {
QuantityType<Temperature> f = c.toUnitRelative(ImperialUnits.FAHRENHEIT);
assertEquals(1.8, f.doubleValue());
}

@Test
public void testEquals() {
QuantityType<Temperature> temp1 = new QuantityType<>("293.15 K");
QuantityType<Temperature> 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<Temperature> temp1 = new QuantityType<>("293.15 K");
QuantityType<Temperature> 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<Temperature> temp3 = new QuantityType<>("100000 K");
QuantityType<Temperature> temp4 = new QuantityType<>("10 mirek");
assertThrows(IllegalArgumentException.class, () -> {
temp3.compareTo(temp4);
});
}
}

0 comments on commit 38a1380

Please sign in to comment.