From fd03c58c68aebd926ed021056838ed3884934fcf Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sat, 13 May 2023 20:20:31 +0200 Subject: [PATCH] further work Signed-off-by: Jan N. Klug --- .../core/internal/i18n/I18nProviderImpl.java | 4 ++- .../library/unit/CurrencyService.java | 10 +++---- .../core/library/dimension/Currency.java | 2 +- .../openhab/core/library/dimension/Price.java | 26 +++++++++++++++++++ .../core/library/unit/CurrencyUnit.java | 6 ++--- .../core/library/unit/CurrencyUnits.java | 9 +++++-- 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Price.java diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/I18nProviderImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/I18nProviderImpl.java index 84f2381f39e..4b052be6b72 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/I18nProviderImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/I18nProviderImpl.java @@ -78,6 +78,7 @@ import org.openhab.core.library.dimension.Density; import org.openhab.core.library.dimension.ElectricConductivity; import org.openhab.core.library.dimension.Intensity; +import org.openhab.core.library.dimension.Price; import org.openhab.core.library.dimension.VolumetricFlowRate; import org.openhab.core.library.types.PointType; import org.openhab.core.library.unit.CurrencyProvider; @@ -398,7 +399,7 @@ public static Map>, Map>, Map currency) { + public static @Nullable BigDecimal getExchangeRate(Unit currency) { return FACTOR_FCN.apply(currency); } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Currency.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Currency.java index b7f6d1efef8..3b46a1119e2 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Currency.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Currency.java @@ -17,7 +17,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; /** - * The {@link Currency} is a + * The {@link Currency} defines the dimension for currencies * * @author Jan N. Klug - Initial contribution */ diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Price.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Price.java new file mode 100644 index 00000000000..4fa93030553 --- /dev/null +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Price.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.library.dimension; + +import javax.measure.Quantity; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * The {@link Price} defines the dimension for prices + * + * @author Jan N. Klug - Initial contribution + */ +@NonNullByDefault +public interface Price extends Quantity { +} diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java index c05c4b114a6..eeef3b3cef2 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnit.java @@ -16,7 +16,7 @@ import static org.eclipse.jdt.annotation.DefaultLocation.PARAMETER; import static org.eclipse.jdt.annotation.DefaultLocation.RETURN_TYPE; import static org.eclipse.jdt.annotation.DefaultLocation.TYPE_BOUND; -import static org.openhab.core.library.unit.CurrencyUnits.SYSTEM_CURRENCY; +import static org.openhab.core.library.unit.CurrencyUnits.BASE_CURRENCY; import static tech.units.indriya.AbstractUnit.ONE; import java.io.Serializable; @@ -191,12 +191,12 @@ private UnitConverter internalGetConverterTo(Unit that) throws Unconve if (this.equals(that)) return AbstractConverter.IDENTITY; - if (SYSTEM_CURRENCY.equals(this)) { + if (BASE_CURRENCY.equals(this)) { BigDecimal factor = CurrencyService.FACTOR_FCN.apply(that); if (factor != null) { return new CurrencyConverter(factor); } - } else if (SYSTEM_CURRENCY.equals(that)) { + } else if (BASE_CURRENCY.equals(that)) { BigDecimal factor = CurrencyService.FACTOR_FCN.apply(this); if (factor != null) { return new CurrencyConverter(factor).inverse(); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnits.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnits.java index a397f27f13d..c70d759eb2f 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnits.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyUnits.java @@ -19,9 +19,12 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.library.dimension.Currency; +import org.openhab.core.library.dimension.Price; import tech.units.indriya.AbstractSystemOfUnits; +import tech.units.indriya.AbstractUnit; import tech.units.indriya.format.SimpleUnitFormat; +import tech.units.indriya.unit.ProductUnit; /** * The {@link CurrencyUnits} defines the UoM system for handling currencies @@ -33,10 +36,12 @@ public final class CurrencyUnits extends AbstractSystemOfUnits { private static final CurrencyUnits INSTANCE = new CurrencyUnits(); - public static final Unit SYSTEM_CURRENCY = new CurrencyUnit("DEF", null); + public static final Unit BASE_CURRENCY = new CurrencyUnit("DEF", null); + public static Unit BASE_PRICE = new ProductUnit<>(AbstractUnit.ONE.divide(BASE_CURRENCY)); static { - addUnit(SYSTEM_CURRENCY); + addUnit(BASE_CURRENCY); + INSTANCE.units.add(BASE_PRICE); } @Override