-
-
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
13 changed files
with
352 additions
and
73 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
....core/src/main/java/org/openhab/core/config/core/CurrencyServiceConfigOptionProvider.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,65 @@ | ||
/** | ||
* 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.config.core; | ||
|
||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.library.unit.CurrencyProvider; | ||
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; | ||
|
||
/** | ||
* The {@link CurrencyServiceConfigOptionProvider} is an implementation of {@link ConfigOptionProvider} for the | ||
* available currency providers. | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@Component(service = ConfigOptionProvider.class) | ||
@NonNullByDefault | ||
public class CurrencyServiceConfigOptionProvider implements ConfigOptionProvider { | ||
|
||
private final List<CurrencyProvider> currencyProviders = new CopyOnWriteArrayList<>(); | ||
|
||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void addCurrencyProvider(CurrencyProvider currencyProvider) { | ||
currencyProviders.add(currencyProvider); | ||
} | ||
|
||
public void removeCurrencyProvider(CurrencyProvider currencyProvider) { | ||
currencyProviders.remove(currencyProvider); | ||
} | ||
|
||
@Override | ||
public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context, | ||
@Nullable Locale locale) { | ||
if ("system:units".equals(uri.toString()) && param.equals("currencyProvider")) { | ||
return currencyProviders.stream().map(this::mapProvider).toList(); | ||
} | ||
return null; | ||
} | ||
|
||
private ParameterOption mapProvider(CurrencyProvider currencyProvider) { | ||
String providerName = currencyProvider.getName(); | ||
int lastDot = providerName.lastIndexOf("."); | ||
String providerDescription = lastDot > -1 ? providerName.substring(lastDot + 1) : providerName; | ||
return new ParameterOption(providerName, providerDescription); | ||
} | ||
} |
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
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
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
76 changes: 76 additions & 0 deletions
76
...nhab.core/src/main/java/org/openhab/core/internal/library/unit/FixedCurrencyProvider.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,76 @@ | ||
/** | ||
* 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.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
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.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Modified; | ||
|
||
/** | ||
* The {@link FixedCurrencyProvider} is an implementation of {@link CurrencyProvider} that provides only a single | ||
* (configurable) currency. | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@Component(service = CurrencyProvider.class, configurationPid = CurrencyService.CONFIGURATION_PID) | ||
@NonNullByDefault | ||
public class FixedCurrencyProvider implements CurrencyProvider { | ||
public static final String CONFIG_OPTION_BASE_CURRENCY = "fixedBaseCurrency"; | ||
private String currencyCode = "DEF"; | ||
|
||
@Activate | ||
public FixedCurrencyProvider(Map<String, Object> config) { | ||
modified(config); | ||
} | ||
|
||
@Modified | ||
public void modified(Map<String, Object> config) { | ||
String code = (String) config.get(CONFIG_OPTION_BASE_CURRENCY); | ||
currencyCode = Objects.requireNonNullElse(code, "DEF"); | ||
} | ||
|
||
@Override | ||
public Unit<Currency> getBaseCurrency() { | ||
String symbol = null; | ||
try { | ||
symbol = java.util.Currency.getInstance(currencyCode).getSymbol(); | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
|
||
return new CurrencyUnit(currencyCode, symbol); | ||
} | ||
|
||
@Override | ||
public Collection<Unit<Currency>> getAdditionalCurrencies() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Function<Unit<Currency>, @Nullable BigDecimal> getExchangeRateFunction() { | ||
return unit -> null; | ||
} | ||
} |
Oops, something went wrong.