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

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

Merged
merged 2 commits into from
Jan 5, 2023
Merged
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 @@ -290,7 +290,7 @@ public Dimension getDimension() {

/**
* 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.
*
Expand All @@ -306,7 +306,7 @@ public Dimension getDimension() {

@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);
}
Expand Down Expand Up @@ -336,7 +336,7 @@ public Dimension getDimension() {
}
Quantity<?> result = quantity.to(targetUnit);

return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
return new QuantityType<T>(result.getValue(), targetUnit);
}

public BigDecimal toBigDecimal() {
Expand Down Expand Up @@ -455,10 +455,13 @@ public String toFullString() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ public void testConversionToPercentType(Locale locale) {

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
Expand Down