From eb75ce81969ab391e34bfca992778c4fab23c213 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Mon, 3 Jul 2023 20:59:46 +0200 Subject: [PATCH] Add unit "calorie" Signed-off-by: Jan N. Klug --- .../java/org/openhab/core/library/unit/Units.java | 5 +++++ .../org/openhab/core/library/unit/UnitsTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java index ce910fe11a7..ae175a6d9e7 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/Units.java @@ -12,6 +12,7 @@ */ package org.openhab.core.library.unit; +import java.math.BigDecimal; import java.math.BigInteger; import javax.measure.Quantity; @@ -122,6 +123,8 @@ public final class Units extends CustomUnits { public static final Unit VOLT = addUnit(tech.units.indriya.unit.Units.VOLT); public static final Unit OHM = addUnit(tech.units.indriya.unit.Units.OHM); public static final Unit JOULE = addUnit(tech.units.indriya.unit.Units.JOULE); + public static final Unit CALORIE = addUnit(JOULE.multiply(new BigDecimal("4.184"))); + public static final Unit KILO_CALORIE = addUnit(MetricPrefix.KILO(CALORIE)); public static final Unit WATT_SECOND = addUnit( new ProductUnit<>(tech.units.indriya.unit.Units.WATT.multiply(tech.units.indriya.unit.Units.SECOND))); public static final Unit WATT_HOUR = addUnit( @@ -241,6 +244,8 @@ public final class Units extends CustomUnits { SimpleUnitFormat.getInstance().label(BIT_PER_SECOND, "bit/s"); SimpleUnitFormat.getInstance().label(BYTE, "B"); SimpleUnitFormat.getInstance().alias(BYTE, "o"); + SimpleUnitFormat.getInstance().label(CALORIE, "cal"); + SimpleUnitFormat.getInstance().label(KILO_CALORIE, "kcal"); SimpleUnitFormat.getInstance().label(CURIE, "Ci"); SimpleUnitFormat.getInstance().label(MILLI_CURIE, "mCi"); SimpleUnitFormat.getInstance().label(MICRO_CURIE, "µCi"); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/unit/UnitsTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/unit/UnitsTest.java index 68bdd23b414..95264a56ac0 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/unit/UnitsTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/unit/UnitsTest.java @@ -401,6 +401,20 @@ public void testRpm() { assertThat(converted.doubleValue(), is(closeTo(1.00, DEFAULT_ERROR))); } + @Test + public void testCalorie() { + QuantityType oneCalorie = QuantityType.valueOf("1 cal"); + QuantityType converted = oneCalorie.toUnit("J"); + assertThat(converted.doubleValue(), is(closeTo(4.184, DEFAULT_ERROR))); + } + + @Test + public void testKiloCalorie() { + QuantityType oneKiloCalorie = QuantityType.valueOf("1 kcal"); + QuantityType converted = oneKiloCalorie.toUnit("J"); + assertThat(converted.doubleValue(), is(closeTo(4184.0, DEFAULT_ERROR))); + } + private static class QuantityEquals extends IsEqual> { private Quantity quantity;