Skip to content

Commit

Permalink
Added units kvarh and kvar.
Browse files Browse the repository at this point in the history
Also simplified gravity unit.

Signed-off-by: Hilbrand Bouwkamp <[email protected]>
  • Loading branch information
Hilbrand committed Apr 3, 2019
1 parent 6900fcb commit 1fb40a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -82,8 +84,8 @@ public final class SmartHomeUnits extends CustomUnits {

// Alphabetical ordered by Unit.
public static final Unit<Acceleration> METRE_PER_SQUARE_SECOND = addUnit(Units.METRE_PER_SQUARE_SECOND);
public static final Unit<Acceleration> STANDARD_GRAVITY = new TransformedUnit<>("gₙ",
SmartHomeUnits.METRE_PER_SQUARE_SECOND, new MultiplyConverter(9.80665));
public static final Unit<Acceleration> STANDARD_GRAVITY = addUnit(
SmartHomeUnits.METRE_PER_SQUARE_SECOND.multiply((9.80665)));
public static final Unit<AmountOfSubstance> MOLE = addUnit(Units.MOLE);
public static final Unit<AmountOfSubstance> DEUTSCHE_HAERTE = addUnit(new TransformedUnit<AmountOfSubstance>("°dH",
(Unit<AmountOfSubstance>) MetricPrefix.MILLI(Units.MOLE).divide(Units.LITRE),
Expand Down Expand Up @@ -116,6 +118,9 @@ public final class SmartHomeUnits extends CustomUnits {
public static final Unit<Energy> WATT_HOUR = addUnit(new ProductUnit<Energy>(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> KILO_VAR = addUnit(MetricPrefix.KILO(new BaseUnit<Power>("var")));
public static final Unit<Energy> KILO_VAR_HOUR = addUnit(new ProductUnit<>(KILO_VAR.divide(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(
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -250,4 +257,20 @@ private static <U extends Unit<?>> 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 <code>unit</code>.
*/
private static <U extends AbstractUnit<?>> U addUnit(U unit, Class<? extends Quantity<?>> type) {
INSTANCE.units.add(unit);
INSTANCE.quantityToUnit.put(type, unit);
return unit;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1fb40a0

Please sign in to comment.