Search in sources :

Example 26 with ExchangeRateProvider

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

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(String... providers) {
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    for (String provName : providers) {
        ExchangeRateProvider prov = Optional.ofNullable(this.conversionProviders.get(provName)).orElseThrow(() -> 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 27 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri 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 28 with ExchangeRateProvider

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

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(ConversionQuery conversionQuery) {
    Collection<String> providers = getProvidersToUse(conversionQuery);
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    for (String provName : providers) {
        ExchangeRateProvider prov = Optional.ofNullable(this.conversionProviders.get(provName)).orElseThrow(() -> new MonetaryException("Unsupported conversion/rate provider: " + provName));
        provInstances.add(prov);
    }
    if (provInstances.isEmpty()) {
        throw new MonetaryException("No such providers: " + conversionQuery);
    }
    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 29 with ExchangeRateProvider

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

the class ExchangeRateTypeTest method shouldReturnsECBHistoricRateProvider.

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

Example 30 with ExchangeRateProvider

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

the class ExchangeRateTypeTest method shouldReturnsIdentityRateProvider.

@Test
public void shouldReturnsIdentityRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IDENTITY);
    assertNotNull(prov);
    assertEquals(IdentityRateProvider.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