Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparing QuantityType with inverted dimensions #4571

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
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);
});
}
}