Skip to content

Commit

Permalink
further work
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed May 13, 2023
1 parent 0036306 commit fd03c58
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,7 +399,7 @@ public static Map<Class<? extends Quantity<?>>, Map<SystemOfUnits, Unit<? extend
addDefaultUnit(dimensionMap, Area.class, SIUnits.SQUARE_METRE, ImperialUnits.SQUARE_FOOT);
addDefaultUnit(dimensionMap, ArealDensity.class, Units.DOBSON_UNIT);
addDefaultUnit(dimensionMap, CatalyticActivity.class, Units.KATAL);
addDefaultUnit(dimensionMap, Currency.class, CurrencyUnits.SYSTEM_CURRENCY);
addDefaultUnit(dimensionMap, Currency.class, CurrencyUnits.BASE_CURRENCY);
addDefaultUnit(dimensionMap, DataAmount.class, Units.BYTE);
addDefaultUnit(dimensionMap, DataTransferRate.class, Units.MEGABIT_PER_SECOND);
addDefaultUnit(dimensionMap, Density.class, Units.KILOGRAM_PER_CUBICMETRE);
Expand All @@ -424,6 +425,7 @@ public static Map<Class<? extends Quantity<?>>, Map<SystemOfUnits, Unit<? extend
addDefaultUnit(dimensionMap, Mass.class, SIUnits.KILOGRAM, ImperialUnits.POUND);
addDefaultUnit(dimensionMap, Power.class, Units.WATT);
addDefaultUnit(dimensionMap, Pressure.class, HECTO(SIUnits.PASCAL), ImperialUnits.INCH_OF_MERCURY);
addDefaultUnit(dimensionMap, Price.class, CurrencyUnits.BASE_PRICE);
addDefaultUnit(dimensionMap, RadiationDoseAbsorbed.class, Units.GRAY);
addDefaultUnit(dimensionMap, RadiationDoseEffective.class, Units.SIEVERT);
addDefaultUnit(dimensionMap, Radioactivity.class, Units.BECQUEREL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.core.internal.library.unit;

import static org.openhab.core.library.unit.CurrencyUnits.SYSTEM_CURRENCY;
import static org.openhab.core.library.unit.CurrencyUnits.BASE_CURRENCY;

import java.math.BigDecimal;
import java.util.Set;
Expand Down Expand Up @@ -67,9 +67,9 @@ public void removeCurrencyProvider(CurrencyProvider currencyProvider) {

private synchronized void enableProvider(CurrencyProvider currencyProvider) {
FACTOR_FCN = currencyProvider.getExchangeRateFunction();
((CurrencyUnit) SYSTEM_CURRENCY).setSymbol(currencyProvider.getBaseCurrency().getSymbol());
((CurrencyUnit) SYSTEM_CURRENCY).setName(currencyProvider.getBaseCurrency().getName());
SimpleUnitFormat.getInstance().label(SYSTEM_CURRENCY, currencyProvider.getBaseCurrency().getSymbol());
((CurrencyUnit) BASE_CURRENCY).setSymbol(currencyProvider.getBaseCurrency().getSymbol());
((CurrencyUnit) BASE_CURRENCY).setName(currencyProvider.getBaseCurrency().getName());
SimpleUnitFormat.getInstance().label(BASE_CURRENCY, currencyProvider.getBaseCurrency().getSymbol());
currencyProvider.getCurrencies().forEach(CurrencyUnits::addUnit);
}

Expand All @@ -79,7 +79,7 @@ private synchronized void enableProvider(CurrencyProvider currencyProvider) {
* @param currency the currency
* @return the exchange rate
*/
public static @Nullable BigDecimal getConversionFactor(Unit<Currency> currency) {
public static @Nullable BigDecimal getExchangeRate(Unit<Currency> currency) {
return FACTOR_FCN.apply(currency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Price> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -191,12 +191,12 @@ private UnitConverter internalGetConverterTo(Unit<Currency> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,10 +36,12 @@ public final class CurrencyUnits extends AbstractSystemOfUnits {

private static final CurrencyUnits INSTANCE = new CurrencyUnits();

public static final Unit<Currency> SYSTEM_CURRENCY = new CurrencyUnit("DEF", null);
public static final Unit<Currency> BASE_CURRENCY = new CurrencyUnit("DEF", null);
public static Unit<Price> BASE_PRICE = new ProductUnit<>(AbstractUnit.ONE.divide(BASE_CURRENCY));

static {
addUnit(SYSTEM_CURRENCY);
addUnit(BASE_CURRENCY);
INSTANCE.units.add(BASE_PRICE);
}

@Override
Expand Down

0 comments on commit fd03c58

Please sign in to comment.