QuantityType.as(PercentType.class) fixes with dimensionless units (#3297)

* QuantityType.as(PercentType.class) fixes with dimensionless units

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
Sami Salonen 2023-01-05 19:51:58 +02:00 committed by GitHub
parent 084f9779a7
commit ef34e5b0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -290,7 +290,7 @@ public class QuantityType<T extends Quantity<T>> extends Number
/**
* Convert this QuantityType to a new {@link QuantityType} using the given target unit.
*
*
* Implicit conversions using inverse units are allowed (i.e. mired <=> Kelvin). This may
* change the dimension.
*
@ -306,7 +306,7 @@ public class QuantityType<T extends Quantity<T>> extends Number
@SuppressWarnings("unchecked")
public @Nullable QuantityType<?> toInvertibleUnit(String targetUnit) {
Unit<?> unit = (Unit<?>) AbstractUnit.parse(targetUnit);
Unit<?> unit = AbstractUnit.parse(targetUnit);
if (unit != null) {
return toInvertibleUnit(unit);
}
@ -336,7 +336,7 @@ public class QuantityType<T extends Quantity<T>> extends Number
}
Quantity<?> result = quantity.to(targetUnit);
return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
return new QuantityType<T>(result.getValue(), targetUnit);
}
public BigDecimal toBigDecimal() {
@ -455,10 +455,13 @@ public class QuantityType<T extends Quantity<T>> extends Number
return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO,
new PercentType(toBigDecimal().multiply(BIG_DECIMAL_HUNDRED))));
} else if (target == PercentType.class) {
if (Units.PERCENT.equals(getUnit())) {
return target.cast(new PercentType(toBigDecimal()));
QuantityType<T> inPercent = toUnit(Units.PERCENT);
if (inPercent == null) {
// incompatible unit
return null;
} else {
return target.cast(new PercentType(inPercent.toBigDecimal()));
}
return target.cast(new PercentType(toBigDecimal().multiply(BIG_DECIMAL_HUNDRED)));
} else if (target == DecimalType.class) {
return target.cast(new DecimalType(toBigDecimal()));
} else {

View File

@ -307,6 +307,19 @@ public class QuantityTypeTest {
assertEquals(PercentType.HUNDRED, new QuantityType<>("100 %").as(PercentType.class));
assertEquals(PercentType.ZERO, new QuantityType<>("0 %").as(PercentType.class));
// Test QuantityType (different ways to refer to 10%) conversion to PercentType
assertEquals(new PercentType(BigDecimal.valueOf(10)), new QuantityType<>("10 %").as(PercentType.class));
assertEquals(new PercentType(BigDecimal.valueOf(10)), new QuantityType<>("0.1").as(PercentType.class));
assertEquals(new PercentType(BigDecimal.valueOf(10)), new QuantityType<>("100 %/10").as(PercentType.class));
assertEquals(new PercentType(BigDecimal.valueOf(10)), new QuantityType<>("100000 ppm").as(PercentType.class));
// Known caveat: bare unit, different dimension. Still gets converted to %
assertEquals(new PercentType(BigDecimal.valueOf(10)),
new QuantityType<>(0.1, Units.RADIAN).as(PercentType.class));
// incompatible units
assertEquals(null, new QuantityType<>("0.5 m").as(PercentType.class));
}
@ParameterizedTest