Small improvements for SmartHomeUnits (#1377)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2020-02-25 19:48:48 +01:00 committed by GitHub
parent 4b772cb6b3
commit 4258d87d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 25 deletions

View File

@ -65,7 +65,6 @@ import tec.uom.se.function.MultiplyConverter;
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;
@ -94,10 +93,10 @@ public final class SmartHomeUnits extends CustomUnits {
new PiMultiplierConverter().concatenate(new RationalConverter(BigInteger.ONE, BigInteger.valueOf(180)))));
public static final Unit<Angle> RADIAN = addUnit(Units.RADIAN);
public static final Unit<ArealDensity> DOBSON_UNIT = addUnit(
new ProductUnit<ArealDensity>(MetricPrefix.MILLI(Units.MOLE).multiply(0.4462).divide(Units.METRE.pow(2))));
new ProductUnit<ArealDensity>(MetricPrefix.MILLI(Units.MOLE).multiply(0.4462).divide(Units.SQUARE_METRE)));
public static final Unit<CatalyticActivity> KATAL = addUnit(Units.KATAL);
public static final Unit<Density> KILOGRAM_PER_CUBICMETRE = addUnit(
new ProductUnit<Density>(Units.KILOGRAM.divide(Units.METRE.pow(3))));
new ProductUnit<Density>(Units.KILOGRAM.divide(Units.CUBIC_METRE)));
public static final Unit<Density> MICROGRAM_PER_CUBICMETRE = addUnit(new TransformedUnit<>(KILOGRAM_PER_CUBICMETRE,
new RationalConverter(BigInteger.ONE, BigInteger.valueOf(1000000000))));
public static final Unit<Dimensionless> ONE = addUnit(AbstractUnit.ONE);
@ -124,18 +123,16 @@ public final class SmartHomeUnits extends CustomUnits {
public static final Unit<Energy> WATT_HOUR = addUnit(new ProductUnit<>(Units.WATT.multiply(Units.HOUR)));
public static final Unit<Energy> KILOWATT_HOUR = addUnit(MetricPrefix.KILO(WATT_HOUR));
public static final Unit<Energy> MEGAWATT_HOUR = addUnit(MetricPrefix.MEGA(WATT_HOUR));
public static final Unit<Power> VAR = addUnit(new AlternateUnit<Power>(Units.WATT, "var"));
public static final Unit<Power> VAR = addUnit(new AlternateUnit<>(Units.WATT, "var"));
public static final Unit<Power> KILOVAR = addUnit(MetricPrefix.KILO(VAR));
public static final Unit<Energy> VAR_HOUR = addUnit(new ProductUnit<>(VAR.multiply(Units.HOUR)),
Energy.class);
public static final Unit<Energy> VAR_HOUR = addUnit(new ProductUnit<>(VAR.multiply(Units.HOUR)), Energy.class);
public static final Unit<Energy> KILOVAR_HOUR = addUnit(MetricPrefix.KILO(VAR_HOUR));
public static final Unit<Power> VOLT_AMPERE = addUnit(new AlternateUnit<Power>(Units.WATT, "VA"));
public static final Unit<Power> VOLT_AMPERE = addUnit(new AlternateUnit<>(Units.WATT, "VA"));
public static final Unit<Energy> VOLT_AMPERE_HOUR = addUnit(new ProductUnit<>(VOLT_AMPERE.multiply(Units.HOUR)),
Energy.class);
public static final Unit<Force> NEWTON = addUnit(Units.NEWTON);
public static final Unit<Frequency> HERTZ = addUnit(Units.HERTZ);
public static final Unit<Intensity> IRRADIANCE = addUnit(
new AlternateUnit<Intensity>(Units.WATT.divide(Units.SQUARE_METRE), "W/m2"));
public static final Unit<Intensity> IRRADIANCE = addUnit(new ProductUnit<>(Units.WATT.divide(Units.SQUARE_METRE)));
public static final Unit<Intensity> MICROWATT_PER_SQUARE_CENTIMETRE = addUnit(
new TransformedUnit<>(IRRADIANCE, new RationalConverter(BigInteger.valueOf(100), BigInteger.ONE)));
public static final Unit<Illuminance> LUX = addUnit(Units.LUX);
@ -153,7 +150,7 @@ public final class SmartHomeUnits extends CustomUnits {
public static final Unit<Pressure> MILLIBAR = addUnit(MetricPrefix.MILLI(BAR));
public static final Unit<Radioactivity> BECQUEREL = addUnit(Units.BECQUEREL);
public static final Unit<Density> BECQUEREL_PER_CUBIC_METRE = addUnit(
new AlternateUnit<Density>(Units.BECQUEREL.divide(Units.CUBIC_METRE), "Bq/m³"));
new ProductUnit<>(Units.BECQUEREL.divide(Units.CUBIC_METRE)));
public static final Unit<RadiationDoseAbsorbed> GRAY = addUnit(Units.GRAY);
public static final Unit<RadiationDoseEffective> SIEVERT = addUnit(Units.SIEVERT);
public static final Unit<Speed> MILLIMETRE_PER_HOUR = addUnit(new TransformedUnit<>("mm/h",
@ -187,13 +184,11 @@ public final class SmartHomeUnits extends CustomUnits {
public static final Unit<DataAmount> KILOBIT = addUnit(MetricPrefix.KILO(BIT));
public static final Unit<DataAmount> GIGABIT = addUnit(MetricPrefix.GIGA(BIT));
public static final Unit<DataAmount> TERABIT = addUnit(MetricPrefix.TERA(BIT));
public static final Unit<DataAmount> BYTE = addUnit(BIT.multiply(8));
public static final Unit<DataAmount> OCTET = BYTE;
public static final Unit<DataAmount> KIBIOCTET = addUnit(BinaryPrefix.KIBI(OCTET));
public static final Unit<DataAmount> MEBIOCTET = addUnit(BinaryPrefix.MEBI(OCTET));
public static final Unit<DataAmount> GIBIOCTET = addUnit(BinaryPrefix.GIBI(OCTET));
public static final Unit<DataTransferRate> BIT_PER_SECOND = addUnit(
new ProductUnit<DataTransferRate>(BIT.divide(Units.SECOND)));
public static final Unit<DataTransferRate> KILOBIT_PER_SECOND = addUnit(MetricPrefix.KILO(BIT_PER_SECOND));

View File

@ -15,13 +15,14 @@ package org.openhab.core.library.unit;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.Assert.*;
import static org.openhab.core.library.unit.MetricPrefix.HECTO;
import java.math.BigDecimal;
import javax.measure.Quantity;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.Energy;
import javax.measure.quantity.Length;
import javax.measure.quantity.Power;
import javax.measure.quantity.Pressure;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Temperature;
@ -49,8 +50,8 @@ public class SmartHomeUnitsTest {
Quantity<Pressure> inHg = Quantities.getQuantity(BigDecimal.ONE, ImperialUnits.INCH_OF_MERCURY);
assertThat(inHg.to(SIUnits.PASCAL), is(Quantities.getQuantity(new BigDecimal("3386.388"), SIUnits.PASCAL)));
assertThat(inHg.to(HECTO(SIUnits.PASCAL)),
is(Quantities.getQuantity(new BigDecimal("33.86388"), HECTO(SIUnits.PASCAL))));
assertThat(inHg.to(MetricPrefix.HECTO(SIUnits.PASCAL)),
is(Quantities.getQuantity(new BigDecimal("33.86388"), MetricPrefix.HECTO(SIUnits.PASCAL))));
}
@Test
@ -64,7 +65,7 @@ public class SmartHomeUnitsTest {
Quantity<Pressure> pascal = Quantities.getQuantity(new BigDecimal("3386.388"), SIUnits.PASCAL);
assertThat(pascal.to(ImperialUnits.INCH_OF_MERCURY),
is(Quantities.getQuantity(new BigDecimal("1.000"), ImperialUnits.INCH_OF_MERCURY)));
is(Quantities.getQuantity(BigDecimal.ONE, ImperialUnits.INCH_OF_MERCURY)));
}
@Test
@ -72,8 +73,8 @@ public class SmartHomeUnitsTest {
Quantity<Pressure> mmHg = Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.MILLIMETRE_OF_MERCURY);
assertThat(mmHg.to(SIUnits.PASCAL), is(Quantities.getQuantity(new BigDecimal("133.322368"), SIUnits.PASCAL)));
assertThat(mmHg.to(HECTO(SIUnits.PASCAL)),
is(Quantities.getQuantity(new BigDecimal("1.33322368"), HECTO(SIUnits.PASCAL))));
assertThat(mmHg.to(MetricPrefix.HECTO(SIUnits.PASCAL)),
is(Quantities.getQuantity(new BigDecimal("1.33322368"), MetricPrefix.HECTO(SIUnits.PASCAL))));
}
@Test
@ -87,19 +88,20 @@ public class SmartHomeUnitsTest {
Quantity<Pressure> pascal = Quantities.getQuantity(new BigDecimal("133.322368"), SIUnits.PASCAL);
assertThat(pascal.to(SmartHomeUnits.MILLIMETRE_OF_MERCURY),
is(Quantities.getQuantity(new BigDecimal("1.000"), SmartHomeUnits.MILLIMETRE_OF_MERCURY)));
is(Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.MILLIMETRE_OF_MERCURY)));
}
@Test
public void testHectoPascal2Pascal() {
Quantity<Pressure> pascal = Quantities.getQuantity(BigDecimal.valueOf(100), SIUnits.PASCAL);
assertThat(pascal.to(HECTO(SIUnits.PASCAL)), is(Quantities.getQuantity(BigDecimal.ONE, HECTO(SIUnits.PASCAL))));
assertThat(pascal.to(MetricPrefix.HECTO(SIUnits.PASCAL)),
is(Quantities.getQuantity(BigDecimal.ONE, MetricPrefix.HECTO(SIUnits.PASCAL))));
}
@Test
public void testHpaUnitSymbol() {
assertThat(HECTO(SIUnits.PASCAL).toString(), is("hPa"));
assertThat(MetricPrefix.HECTO(SIUnits.PASCAL).toString(), is("hPa"));
}
@Test
@ -168,30 +170,43 @@ public class SmartHomeUnitsTest {
assertThat(SmartHomeUnits.KNOT.getSymbol(), is("kn"));
assertThat(SmartHomeUnits.KNOT.toString(), is("kn"));
}
@Test
public void testVarUnitSymbol() {
assertThat(SmartHomeUnits.VAR.getSymbol(), is("var"));
assertThat(SmartHomeUnits.VAR.toString(), is("var"));
assertThat(SmartHomeUnits.VAR_HOUR.toString(), is("varh"));
}
@Test
public void testKVarUnitSymbol() {
assertThat(SmartHomeUnits.KILOVAR.toString(), is("kvar"));
assertThat(SmartHomeUnits.KILOVAR_HOUR.toString(), is("kvarh"));
Quantity<Power> kvar = Quantities.getQuantity(BigDecimal.TEN, MetricPrefix.KILO(SmartHomeUnits.VAR));
assertThat(kvar.getUnit().toString(), is("kvar"));
Quantity<Energy> kvarh = Quantities.getQuantity(BigDecimal.TEN, MetricPrefix.KILO(SmartHomeUnits.VAR_HOUR));
assertThat(kvarh.getUnit().toString(), is("kvarh"));
}
@Test
public void testKVarHFromString() {
assertThat(QuantityType.valueOf("2.60 kvarh").getUnit().toString(), is("kvarh"));
}
@Test
public void testVoltAmpereUnitSymbol() {
assertThat(SmartHomeUnits.VOLT_AMPERE.toString(), is("VA"));
assertThat(SmartHomeUnits.VOLT_AMPERE.getSymbol(), is("VA"));
assertThat(SmartHomeUnits.VOLT_AMPERE_HOUR.toString(), is("VAh"));
Quantity<Power> kVA = Quantities.getQuantity(BigDecimal.TEN, MetricPrefix.KILO(SmartHomeUnits.VOLT_AMPERE));
assertThat(kVA.getUnit().toString(), is("kVA"));
Quantity<Energy> kVAh = Quantities.getQuantity(BigDecimal.TEN,
MetricPrefix.KILO(SmartHomeUnits.VOLT_AMPERE_HOUR));
assertThat(kVAh.getUnit().toString(), is("kVAh"));
}
@Test