-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jan N. Klug <github@klug.nrw>
- Loading branch information
Showing
12 changed files
with
799 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
....openhab.core/src/main/java/org/openhab/core/internal/library/unit/CurrencyConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* 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.internal.library.unit; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.MathContext; | ||
import java.util.Objects; | ||
|
||
import javax.measure.UnitConverter; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import tech.units.indriya.function.AbstractConverter; | ||
|
||
/** | ||
* The {@link CurrencyConverter} is a | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class CurrencyConverter extends AbstractConverter { | ||
|
||
private final BigDecimal factor; | ||
|
||
public CurrencyConverter(BigDecimal factor) { | ||
this.factor = factor; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object cvtr) { | ||
return cvtr instanceof CurrencyConverter currencyConverter && factor.equals(currencyConverter.factor); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(factor); | ||
} | ||
|
||
@Override | ||
protected @Nullable String transformationLiteral() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected AbstractConverter inverseWhenNotIdentity() { | ||
return new CurrencyConverter(BigDecimal.ONE.divide(factor, MathContext.DECIMAL128)); | ||
} | ||
|
||
@Override | ||
protected boolean canReduceWith(@Nullable AbstractConverter that) { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected Number convertWhenNotIdentity(@NonNullByDefault({}) Number value) { | ||
return new BigDecimal(value.toString()).multiply(factor, MathContext.DECIMAL128); | ||
} | ||
|
||
@Override | ||
public int compareTo(@Nullable UnitConverter o) { | ||
return o instanceof CurrencyConverter currencyConverter ? factor.compareTo(currencyConverter.factor) : -1; | ||
} | ||
|
||
@Override | ||
public boolean isIdentity() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isLinear() { | ||
return true; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...rg.openhab.core/src/main/java/org/openhab/core/internal/library/unit/CurrencyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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.internal.library.unit; | ||
|
||
import static org.openhab.core.library.unit.CurrencyUnits.SYSTEM_CURRENCY; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Set; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
import java.util.function.Function; | ||
|
||
import javax.measure.Unit; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.library.dimension.Currency; | ||
import org.openhab.core.library.unit.CurrencyProvider; | ||
import org.openhab.core.library.unit.CurrencyUnit; | ||
import org.openhab.core.library.unit.CurrencyUnits; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
|
||
import tech.units.indriya.format.SimpleUnitFormat; | ||
|
||
/** | ||
* The {@link CurrencyService} is a | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@Component | ||
@NonNullByDefault | ||
public class CurrencyService { | ||
|
||
public static Function<Unit<Currency>, @Nullable BigDecimal> FACTOR_FCN = unit -> null; | ||
|
||
private final Set<CurrencyProvider> currencyProviders = new CopyOnWriteArraySet<>(); | ||
|
||
@Activate | ||
public CurrencyService( | ||
@Reference(target = "(" + Constants.SERVICE_PID + "=org.openhab.i18n)") CurrencyProvider currencyProvider) { | ||
currencyProviders.add(currencyProvider); | ||
enableProvider(currencyProvider); | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void addCurrencyProvider(CurrencyProvider currencyProvider) { | ||
currencyProviders.add(currencyProvider); | ||
} | ||
|
||
public void removeCurrencyProvider(CurrencyProvider currencyProvider) { | ||
currencyProviders.remove(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()); | ||
currencyProvider.getCurrencies().forEach(CurrencyUnits::addUnit); | ||
} | ||
|
||
/** | ||
* Get the exchange rate for a given currency to the system's base unit | ||
* | ||
* @param currency the currency | ||
* @return the exchange rate | ||
*/ | ||
public static @Nullable BigDecimal getConversionFactor(Unit<Currency> currency) { | ||
return FACTOR_FCN.apply(currency); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
bundles/org.openhab.core/src/main/java/org/openhab/core/library/dimension/Currency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Currency} is a | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public interface Currency extends Quantity<Currency> { | ||
} |
66 changes: 66 additions & 0 deletions
66
bundles/org.openhab.core/src/main/java/org/openhab/core/library/unit/CurrencyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* 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.unit; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
import javax.measure.Unit; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.library.dimension.Currency; | ||
|
||
/** | ||
* The {@link CurrencyProvider} can be implemented by services that supply currencies and their | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public interface CurrencyProvider { | ||
|
||
/** | ||
* Get the name of this {@link CurrencyProvider} | ||
* | ||
* @return the name, defaults to the class name | ||
*/ | ||
default String getName() { | ||
return getClass().getName(); | ||
} | ||
|
||
/** | ||
* Get the base currency from this provider | ||
* <p /> | ||
* This currency is used as base for calculating exchange rates. | ||
* | ||
* @return the base currency of this provider | ||
*/ | ||
Unit<Currency> getBaseCurrency(); | ||
|
||
/** | ||
* Get all units that are supported by this provider | ||
* | ||
* @return a {@link Collection} of {@link Unit<Currency>}s | ||
*/ | ||
Collection<Unit<Currency>> getCurrencies(); | ||
|
||
/** | ||
* Get a {@link Function} that supplies exchanges rates for currencies supported by this provider | ||
* <p /> | ||
* This needs to be dynamic because in most cases exchange rates are not constant over time. | ||
* | ||
* @return the function | ||
*/ | ||
Function<Unit<Currency>, @Nullable BigDecimal> getExchangeRateFunction(); | ||
} |
Oops, something went wrong.