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

[core] Added units kvarh and kvar #695

Merged
merged 1 commit into from
Apr 7, 2019
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 @@ -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ₙ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we loose the symbol "g" when applying this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Rest looks good IMHO.

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> KILOVAR = addUnit(MetricPrefix.KILO(new BaseUnit<Power>("var")));
public static final Unit<Energy> KILOVAR_HOUR = addUnit(new ProductUnit<>(KILOVAR.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 @@ -208,6 +213,8 @@ public final class SmartHomeUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(KIBIOCTET, "Kio");
SimpleUnitFormat.getInstance().label(KILOBIT, "kbit");
SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s");
SimpleUnitFormat.getInstance().label(KILOVAR, "kvar");
SimpleUnitFormat.getInstance().label(KILOVAR_HOUR, "kvarh");
SimpleUnitFormat.getInstance().label(KILOWATT_HOUR, "kWh");
SimpleUnitFormat.getInstance().label(KNOT, KNOT.getSymbol());
SimpleUnitFormat.getInstance().label(LITRE_PER_MINUTE, "l/min");
Expand Down Expand Up @@ -250,4 +257,18 @@ 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.KILOVAR_HOUR));
}

@Test
Expand Down