Search in sources :

Example 16 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri-bp by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method getDefaultProviderChain.

@Override
public List<String> getDefaultProviderChain() {
    List<String> provList = new ArrayList<>();
    String defaultChain = MonetaryConfig.getConfig().get("conversion.default-chain");
    if (defaultChain != null) {
        String[] items = defaultChain.split(",");
        for (String item : items) {
            if (getProviderNames().contains(item.trim())) {
                provList.add(item);
            } else {
                LOG.warning("Ignoring non existing default provider: " + item);
            }
        }
    } else {
        for (ExchangeRateProvider p : Bootstrap.getServices(ExchangeRateProvider.class)) {
            provList.add(p.getContext().getProviderName());
        }
    }
    return provList;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList)

Example 17 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri-bp by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(String... providers) {
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    for (String provName : providers) {
        ExchangeRateProvider prov = this.conversionProviders.get(provName);
        if (prov == null) {
            throw new MonetaryException("Unsupported conversion/rate provider: " + provName);
        }
        provInstances.add(prov);
    }
    if (provInstances.size() == 1) {
        return provInstances.get(0);
    }
    return new CompoundRateProvider(provInstances);
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) CompoundRateProvider(org.javamoney.moneta.spi.CompoundRateProvider) MonetaryException(javax.money.MonetaryException)

Example 18 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri-bp by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method reload.

/**
 * Reloads/reinitializes the providers found.
 */
public void reload() {
    Map<String, ExchangeRateProvider> newProviders = new ConcurrentHashMap<>();
    for (ExchangeRateProvider prov : Bootstrap.getServices(ExchangeRateProvider.class)) {
        newProviders.put(prov.getContext().getProviderName(), prov);
    }
    this.conversionProviders = newProviders;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 19 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri by JavaMoney.

the class ExchangeRateTypeTest method shouldReturnsECBHistoricRateProvider.

@Test
public void shouldReturnsECBHistoricRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.ECB_HIST);
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBHistoricRateProvider.class, prov.getClass());
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Example 20 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri by JavaMoney.

the class ExchangeRateTypeTest method shouldReturnsECBHistoric90RateProvider.

@Test
public void shouldReturnsECBHistoric90RateProvider() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.ECB_HIST90);
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBHistoric90RateProvider.class, prov.getClass());
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Aggregations

ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)35 Test (org.testng.annotations.Test)23 CurrencyConversion (javax.money.convert.CurrencyConversion)10 ArrayList (java.util.ArrayList)9 MonetaryAmount (javax.money.MonetaryAmount)9 MonetaryException (javax.money.MonetaryException)7 CompoundRateProvider (org.javamoney.moneta.spi.CompoundRateProvider)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExchangeRate (javax.money.convert.ExchangeRate)2 ProviderContext (javax.money.convert.ProviderContext)2 ProviderContextBuilder (javax.money.convert.ProviderContextBuilder)2 RateType (javax.money.convert.RateType)2 HashSet (java.util.HashSet)1 Money (org.javamoney.moneta.Money)1