Upgrade Units of Measurement dependencies (#10583)

* Fix code/tests for upgrade
* Resolve runbundles
* Update Checkstyle ruleset for changed packages

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2021-05-11 08:31:03 +02:00
committed by GitHub
parent 0fd9c4c1f8
commit c3a6aa5814
24 changed files with 178 additions and 91 deletions

View File

@@ -25,10 +25,10 @@ import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.ThingTypeUID;
import tec.uom.se.format.SimpleUnitFormat;
import tec.uom.se.function.RationalConverter;
import tec.uom.se.unit.ProductUnit;
import tec.uom.se.unit.TransformedUnit;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.units.indriya.function.MultiplyConverter;
import tech.units.indriya.unit.ProductUnit;
import tech.units.indriya.unit.TransformedUnit;
/**
* The {@link AirthingsBindingConstants} class defines common constants, which are
@@ -59,7 +59,7 @@ public class AirthingsBindingConstants {
public static final String CHANNEL_ID_RADON_LT_AVG = "radon_lt_avg";
public static final Unit<Dimensionless> PARTS_PER_BILLION = new TransformedUnit<>(Units.ONE,
new RationalConverter(BigInteger.ONE, BigInteger.valueOf(1000000000)));
MultiplyConverter.ofRational(BigInteger.ONE, BigInteger.valueOf(1000000000)));
public static final Unit<Density> BECQUEREL_PER_CUBIC_METRE = new ProductUnit<>(
Units.BECQUEREL.divide(SIUnits.CUBIC_METRE));

View File

@@ -36,12 +36,10 @@ import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
import tec.uom.se.format.SimpleUnitFormat;
import tec.uom.se.function.MultiplyConverter;
import tec.uom.se.function.PiMultiplierConverter;
import tec.uom.se.function.RationalConverter;
import tec.uom.se.unit.ProductUnit;
import tec.uom.se.unit.TransformedUnit;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.units.indriya.function.MultiplyConverter;
import tech.units.indriya.unit.ProductUnit;
import tech.units.indriya.unit.TransformedUnit;
/**
* The {@link BluetoothUnit} maps bluetooth units to openHAB units.
@@ -239,13 +237,13 @@ public enum BluetoothUnit {
new ProductUnit<RadiationDoseAbsorptionRate>(Units.GRAY.divide(Units.SECOND)));
public static final Unit<Mass> POUND = addUnit(
new TransformedUnit<Mass>(SIUnits.KILOGRAM, new MultiplyConverter(0.45359237)));
new TransformedUnit<Mass>(SIUnits.KILOGRAM, MultiplyConverter.of(0.45359237)));
public static final Unit<Angle> MINUTE_ANGLE = addUnit(new TransformedUnit<Angle>(Units.RADIAN,
new PiMultiplierConverter().concatenate(new RationalConverter(1, 180 * 60))));
MultiplyConverter.ofPiExponent(1).concatenate(MultiplyConverter.ofRational(1, 180 * 60))));
public static final Unit<Angle> SECOND_ANGLE = addUnit(new TransformedUnit<Angle>(Units.RADIAN,
new PiMultiplierConverter().concatenate(new RationalConverter(1, 180 * 60 * 60))));
MultiplyConverter.ofPiExponent(1).concatenate(MultiplyConverter.ofRational(1, 180 * 60 * 60))));
public static final Unit<Area> HECTARE = addUnit(SIUnits.SQUARE_METRE.multiply(10000.0));
public static final Unit<Area> BARN = addUnit(SIUnits.SQUARE_METRE.multiply(10E-28));
@@ -259,10 +257,10 @@ public enum BluetoothUnit {
new ProductUnit<Radiance>(WATT_PER_STERADIAN.divide(SIUnits.SQUARE_METRE)));
public static final Unit<Frequency> CYCLES_PER_MINUTE = addUnit(new TransformedUnit<Frequency>(Units.HERTZ,
new RationalConverter(BigInteger.valueOf(60), BigInteger.ONE)));
MultiplyConverter.ofRational(BigInteger.valueOf(60), BigInteger.ONE)));
public static final Unit<Angle> REVOLUTION = addUnit(new TransformedUnit<Angle>(Units.RADIAN,
new PiMultiplierConverter().concatenate(new RationalConverter(2, 1))));
MultiplyConverter.ofPiExponent(1).concatenate(MultiplyConverter.ofRational(2, 1))));
public static final Unit<AngularVelocity> REVOLUTION_PER_MINUTE = addUnit(
new ProductUnit<AngularVelocity>(REVOLUTION.divide(Units.MINUTE)));

View File

@@ -124,7 +124,15 @@ class CosemQuantity<Q extends @Nullable Quantity<Q>> extends CosemValueDescripto
*/
private String prepare(String cosemValue) {
Matcher matcher = COSEM_VALUE_WITH_UNIT_PATTERN.matcher(cosemValue.replace("m3", ""));
if (!matcher.find()) {
return cosemValue;
}
return matcher.find() ? matcher.group(1) + ' ' + matcher.group(2) : cosemValue;
try {
Integer.parseInt(matcher.group(2));
return cosemValue;
} catch (NumberFormatException e) {
return matcher.group(1) + ' ' + matcher.group(2);
}
}
}

View File

@@ -24,7 +24,7 @@ import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.Units;
import org.openhab.core.types.State;
import tec.uom.se.quantity.QuantityDimension;
import tech.units.indriya.unit.UnitDimension;
/**
* Tests for {@link AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
@@ -75,12 +75,12 @@ public class ConvertFromBindingTest extends BaseConverterTest {
floatQuantityDp.setUnit("°C");
convertedState = temperatureConverter.convertFromBinding(floatQuantityDp);
assertThat(convertedState, instanceOf(QuantityType.class));
assertThat(((QuantityType<?>) convertedState).getDimension(), is(QuantityDimension.TEMPERATURE));
assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
assertThat(((QuantityType<?>) convertedState).toUnit(ImperialUnits.FAHRENHEIT).doubleValue(), is(50.9));
floatQuantityDp.setUnit("°C");
assertThat(((QuantityType<?>) convertedState).getDimension(), is(QuantityDimension.TEMPERATURE));
assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
integerQuantityDp.setValue(50000);
@@ -88,7 +88,7 @@ public class ConvertFromBindingTest extends BaseConverterTest {
convertedState = frequencyConverter.convertFromBinding(integerQuantityDp);
assertThat(convertedState, instanceOf(QuantityType.class));
assertThat(((QuantityType<?>) convertedState).getDimension(),
is(QuantityDimension.NONE.divide(QuantityDimension.TIME)));
is(UnitDimension.NONE.divide(UnitDimension.TIME)));
assertThat(((QuantityType<?>) convertedState).intValue(), is(50000));
assertThat(((QuantityType<?>) convertedState).toUnit(Units.HERTZ).intValue(), is(50));
@@ -96,7 +96,7 @@ public class ConvertFromBindingTest extends BaseConverterTest {
floatQuantityDp.setUnit("100%");
convertedState = timeConverter.convertFromBinding(floatQuantityDp);
assertThat(convertedState, instanceOf(QuantityType.class));
assertThat(((QuantityType<?>) convertedState).getDimension(), is(QuantityDimension.NONE));
assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.NONE));
assertThat(((QuantityType<?>) convertedState).doubleValue(), is(70.0));
assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));

View File

@@ -14,6 +14,8 @@ package org.openhab.binding.http.internal.converter;
import java.util.function.Consumer;
import javax.measure.format.MeasurementParseException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.http.internal.config.HttpChannelConfig;
@@ -60,7 +62,7 @@ public class NumberItemConverter extends AbstractTransformingItemConverter {
return new QuantityType<>(trimmedValue);
}
}
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | MeasurementParseException e) {
// finally failed
}
}

View File

@@ -66,7 +66,7 @@ public class ConverterTest {
Assertions.assertEquals(new QuantityType<>(500, Units.WATT), converter.toState("500"));
// no valid value
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("100°C"));
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("100foo"));
Assertions.assertEquals(UnDefType.UNDEF, converter.toState("foo"));
Assertions.assertEquals(UnDefType.UNDEF, converter.toState(""));
}

View File

@@ -26,7 +26,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.measure.Unit;
import javax.measure.format.ParserException;
import javax.measure.format.MeasurementParseException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -161,7 +161,7 @@ public class MiIoBasicHandler extends MiIoAbstractHandler {
qtc = ((QuantityType<?>) command).toUnit(unit);
}
}
} catch (ParserException e) {
} catch (MeasurementParseException e) {
// swallow
}
if (qtc != null) {

View File

@@ -62,10 +62,10 @@ public class DataBlockTest {
Optional<Data> dataOpt = mc.parse(DataType.POWER);
assertTrue(dataOpt.isPresent());
PowerBlock b = (PowerBlock) dataOpt.get();
assertEquals("242.0 W", b.pvPowerSupply.toString(), "PV Supply");
assertEquals("14.0 W", b.gridPowerSupply.toString(), "Grid Supply");
assertEquals("0.0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
assertEquals("303.0 W", b.batteryPowerSupply.toString(), "Battery Supply");
assertEquals("242 W", b.pvPowerSupply.toString(), "PV Supply");
assertEquals("14 W", b.gridPowerSupply.toString(), "Grid Supply");
assertEquals("0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
assertEquals("303 W", b.batteryPowerSupply.toString(), "Battery Supply");
}
@Test
@@ -73,10 +73,10 @@ public class DataBlockTest {
Optional<Data> dataOpt = mcNegativePVSupply.parse(DataType.POWER);
assertTrue(dataOpt.isPresent());
PowerBlock b = (PowerBlock) dataOpt.get();
assertEquals("-330.0 W", b.pvPowerSupply.toString(), "PV Supply");
assertEquals("14.0 W", b.gridPowerSupply.toString(), "Grid Supply");
assertEquals("0.0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
assertEquals("303.0 W", b.batteryPowerSupply.toString(), "Battery Supply");
assertEquals("-330 W", b.pvPowerSupply.toString(), "PV Supply");
assertEquals("14 W", b.gridPowerSupply.toString(), "Grid Supply");
assertEquals("0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
assertEquals("303 W", b.batteryPowerSupply.toString(), "Battery Supply");
}
@Test

View File

@@ -231,10 +231,9 @@ public class ModbusGainOffsetProfile<Q extends Quantity<Q>> implements StateProf
* When the conversion is towards the handler (towardsItem=false), unit will be ONE
*
*/
@SuppressWarnings("unchecked") // Safe cast since QU = Dimensionless * QU
private <QU extends Quantity<QU>> QuantityType<QU> applyGainTowardsItem(QuantityType<Dimensionless> qtState,
QuantityType<QU> gainDelta) {
return (QuantityType<QU>) qtState.multiply(gainDelta);
return new QuantityType<>(qtState.toBigDecimal().multiply(gainDelta.toBigDecimal()), gainDelta.getUnit());
}
private QuantityType<Dimensionless> applyGainTowardsHandler(QuantityType<?> qtState, QuantityType<?> gainDelta) {

View File

@@ -172,10 +172,7 @@ public class ModbusGainOffsetProfileTest {
// Workaround for errors like "java.lang.UnsupportedOperationException: °C is non-linear, cannot convert"
if (expectedStateUpdateTowardsItem instanceof QuantityType<?>) {
assertTrue(actualStateUpdateTowardsItem instanceof QuantityType<?>);
assertEquals(((QuantityType<?>) expectedStateUpdateTowardsItem).getUnit(),
((QuantityType<?>) actualStateUpdateTowardsItem).getUnit());
assertEquals(((QuantityType<?>) expectedStateUpdateTowardsItem).toBigDecimal(),
((QuantityType<?>) actualStateUpdateTowardsItem).toBigDecimal());
assertEquals(expectedStateUpdateTowardsItem, actualStateUpdateTowardsItem);
} else {
assertEquals(expectedStateUpdateTowardsItem, actualStateUpdateTowardsItem);
}