remove comparing inverted dimensions

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
pull/4571/head
Mark Herwege 2025-01-20 18:29:22 +01:00
parent 61ef3f245e
commit 7967ec5916
3 changed files with 3 additions and 34 deletions

View File

@ -250,14 +250,6 @@ public class QuantityType<T extends Quantity<T>> extends Number
return true;
}
/**
* This method overrides {@link Comparable.compareTo}
*
* Warning: This method will also compare {@link QuantityType<?>} where the arguments are of the inverted dimension.
* Contrary to what is stated as a requirements for overriding {@link Comparable.compareTo}, this implementation is
* not transitive when the arguments are of an inverted dimension. When the dimensions are inverted, a > b and b > a
* may also be true at the same time.
*/
@Override
public int compareTo(QuantityType<T> o) {
return internalCompareTo(o);
@ -272,8 +264,6 @@ public class QuantityType<T extends Quantity<T>> extends Number
} else {
throw new IllegalArgumentException("Unable to convert to system unit during compare.");
}
} else if (quantity.getUnit().inverse().isCompatible(o.quantity.getUnit())) {
return internalCompareTo(o.inverse());
} else {
throw new IllegalArgumentException("Can not compare incompatible units.");
}

View File

@ -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;
@ -166,7 +166,8 @@ public class QuantityTypeArithmeticGroupFunctionTest {
GroupFunction function = new QuantityTypeArithmeticGroupFunction.Avg(Temperature.class);
State state = function.calculate(items);
assertEquals(new QuantityType<>("55.33333333333333333333333333333334 °C"), state);
QuantityType<?> qt = state.as(QuantityType.class).toUnit("°C");
assertEquals(55.33333334, qt.doubleValue(), 0.00001);
}
@ParameterizedTest

View File

@ -674,27 +674,5 @@ public class QuantityTypeTest {
assertEquals(1, temp1.compareTo(temp2));
temp2 = new QuantityType<>("50 °C");
assertEquals(-1, temp1.compareTo(temp2));
temp1 = new QuantityType<>("100000 K");
temp2 = new QuantityType<>("10 mirek");
assertEquals(0, temp1.compareTo(temp2));
assertEquals(0, temp2.compareTo(temp1));
temp2 = new QuantityType<>("20 mirek");
assertEquals(1, temp1.compareTo(temp2)); // temp1 (100000 K) > temp2 (20 mirek = 50000 K) in Kelvin
assertEquals(1, temp2.compareTo(temp1)); // temp2 (20 mirek) > temp1 (100000 K = 10 mirek) in mirek
temp2 = new QuantityType<>("1 mirek");
assertEquals(-1, temp1.compareTo(temp2));
assertEquals(-1, temp2.compareTo(temp1));
temp1 = new QuantityType<>("0.1 MK");
temp2 = new QuantityType<>("10 mirek");
assertEquals(0, temp1.compareTo(temp2));
assertEquals(0, temp2.compareTo(temp1));
temp2 = new QuantityType<>("20 mirek");
assertEquals(1, temp1.compareTo(temp2));
assertEquals(1, temp2.compareTo(temp1));
temp2 = new QuantityType<>("1 mirek");
assertEquals(-1, temp1.compareTo(temp2));
assertEquals(-1, temp2.compareTo(temp1));
}
}