From 1fb40a008b52939404eaaaf60f17217721e48f8e Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Wed, 3 Apr 2019 19:40:12 +0200 Subject: [PATCH] Added units kvarh and kvar. Also simplified gravity unit. Signed-off-by: Hilbrand Bouwkamp --- .../core/library/unit/SmartHomeUnits.java | 27 +++++++++++++++++-- .../core/types/util/UnitUtilsTest.java | 10 +++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java index d6130b6127b..0435883f66f 100644 --- a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java +++ b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java @@ -14,6 +14,7 @@ import java.math.BigInteger; +import javax.measure.Quantity; import javax.measure.Unit; import javax.measure.quantity.Acceleration; import javax.measure.quantity.AmountOfSubstance; @@ -63,6 +64,7 @@ import tec.uom.se.function.PiMultiplierConverter; import tec.uom.se.function.RationalConverter; import tec.uom.se.unit.AlternateUnit; +import tec.uom.se.unit.BaseUnit; import tec.uom.se.unit.ProductUnit; import tec.uom.se.unit.TransformedUnit; import tec.uom.se.unit.Units; @@ -82,8 +84,8 @@ public final class SmartHomeUnits extends CustomUnits { // Alphabetical ordered by Unit. public static final Unit METRE_PER_SQUARE_SECOND = addUnit(Units.METRE_PER_SQUARE_SECOND); - public static final Unit STANDARD_GRAVITY = new TransformedUnit<>("gₙ", - SmartHomeUnits.METRE_PER_SQUARE_SECOND, new MultiplyConverter(9.80665)); + public static final Unit STANDARD_GRAVITY = addUnit( + SmartHomeUnits.METRE_PER_SQUARE_SECOND.multiply((9.80665))); public static final Unit MOLE = addUnit(Units.MOLE); public static final Unit DEUTSCHE_HAERTE = addUnit(new TransformedUnit("°dH", (Unit) MetricPrefix.MILLI(Units.MOLE).divide(Units.LITRE), @@ -116,6 +118,9 @@ public final class SmartHomeUnits extends CustomUnits { public static final Unit WATT_HOUR = addUnit(new ProductUnit(Units.WATT.multiply(Units.HOUR))); public static final Unit KILOWATT_HOUR = addUnit(MetricPrefix.KILO(WATT_HOUR)); public static final Unit MEGAWATT_HOUR = addUnit(MetricPrefix.MEGA(WATT_HOUR)); + public static final Unit KILO_VAR = addUnit(MetricPrefix.KILO(new BaseUnit("var"))); + public static final Unit KILO_VAR_HOUR = addUnit(new ProductUnit<>(KILO_VAR.divide(Units.HOUR)), + Energy.class); public static final Unit NEWTON = addUnit(Units.NEWTON); public static final Unit HERTZ = addUnit(Units.HERTZ); public static final Unit IRRADIANCE = addUnit( @@ -209,6 +214,8 @@ public final class SmartHomeUnits extends CustomUnits { SimpleUnitFormat.getInstance().label(KILOBIT, "kbit"); SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s"); SimpleUnitFormat.getInstance().label(KILOWATT_HOUR, "kWh"); + SimpleUnitFormat.getInstance().label(KILO_VAR, "kvar"); + SimpleUnitFormat.getInstance().label(KILO_VAR_HOUR, "kvarh"); SimpleUnitFormat.getInstance().label(KNOT, KNOT.getSymbol()); SimpleUnitFormat.getInstance().label(LITRE_PER_MINUTE, "l/min"); SimpleUnitFormat.getInstance().label(MEBIOCTET, "Mio"); @@ -250,4 +257,20 @@ private static > U addUnit(U unit) { INSTANCE.units.add(unit); return unit; } + + /** + * Adds a new unit and maps it to the specified quantity type. + * + * @param unit + * the unit being added. + * @param type + * the quantity type. + * @return unit. + */ + private static > U addUnit(U unit, Class> type) { + INSTANCE.units.add(unit); + INSTANCE.quantityToUnit.put(type, unit); + return unit; + } + } diff --git a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java b/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java index 8ee2f59934a..217f9b221b5 100644 --- a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java +++ b/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java @@ -68,8 +68,13 @@ public void whenValidDimensionIsGiven_shouldCreateQuantityClass() { @Test public void testConversionOfUnit() { - assertThat(SmartHomeUnits.DECIBEL_MILLIWATTS.getConverterTo(SmartHomeUnits.WATT).convert(50), closeTo(100, 3)); - assertThat(SmartHomeUnits.WATT.getConverterTo(SmartHomeUnits.DECIBEL_MILLIWATTS).convert(0.1), closeTo(20, 3)); + assertThat(SmartHomeUnits.DECIBEL_MILLIWATTS.getConverterTo(SmartHomeUnits.WATT).convert(50), + closeTo(100, 0.001)); + assertThat(SmartHomeUnits.WATT.getConverterTo(SmartHomeUnits.DECIBEL_MILLIWATTS).convert(0.1), + closeTo(20, 0.0001)); + assertThat( + SmartHomeUnits.METRE_PER_SQUARE_SECOND.getConverterTo(SmartHomeUnits.STANDARD_GRAVITY).convert(9.8065), + closeTo(1.0, 0.0001)); } @Test @@ -79,6 +84,7 @@ public void shouldParseUnitFromPattern() { assertThat(UnitUtils.parseUnit("myLabel km"), is(KILO(SIUnits.METRE))); assertThat(UnitUtils.parseUnit("%.2f %%"), is(SmartHomeUnits.PERCENT)); assertThat(UnitUtils.parseUnit("myLabel %unit%"), is(nullValue())); + assertThat(UnitUtils.parseUnit("%.2f kvarh"), is(SmartHomeUnits.KILO_VAR_HOUR)); } @Test