Search in sources :

Example 11 with ExchangeRateProvider

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

the class ExchangeRateTypeTest method shouldReturnsIMFRateProvider.

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

Example 12 with ExchangeRateProvider

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

the class ExchangeRateTypeTest method shouldReturnsECBHistoric90RateProvider.

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

Example 13 with ExchangeRateProvider

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

the class ECBEarlyAccessProviderTest method testAccess_HIST90.

@Test
public void testAccess_HIST90() throws InterruptedException {
    final MonetaryAmount inEuro = Money.of(10, "EUR");
    for (int i = 0; i < 100; i++) {
        try {
            final ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider("ECB-HIST90");
            final CurrencyConversion dollarConversion = rateProvider.getCurrencyConversion("USD");
            final MonetaryAmount inDollar = inEuro.with(dollarConversion);
            System.out.println(String.format("RUN: %n - %s: %s ≙ %s", i, rateProvider, inEuro, inDollar));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 100; i++) {
        final ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider("ECB-HIST90");
        final CurrencyConversion dollarConversion = rateProvider.getCurrencyConversion("USD");
        Thread.sleep(100L);
        final MonetaryAmount inDollar = inEuro.with(dollarConversion);
        System.out.println(String.format("RUN: %n - %s: %s ≙ %s", i, rateProvider, inEuro, inDollar));
    }
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) MonetaryAmount(javax.money.MonetaryAmount) CurrencyConversion(javax.money.convert.CurrencyConversion) Test(org.testng.annotations.Test)

Example 14 with ExchangeRateProvider

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

the class CompoundRateProvider method createContext.

private static ProviderContext createContext(Iterable<ExchangeRateProvider> providers) {
    Set<RateType> rateTypeSet = new HashSet<>();
    StringBuilder providerName = new StringBuilder("Compound: ");
    List<ProviderContext> childContextList = new ArrayList<>();
    for (ExchangeRateProvider exchangeRateProvider : providers) {
        childContextList.add(exchangeRateProvider.getContext());
        providerName.append(exchangeRateProvider.getContext().getProviderName());
        providerName.append(',');
        rateTypeSet.addAll(exchangeRateProvider.getContext().getRateTypes());
    }
    providerName.setLength(providerName.length() - 1);
    ProviderContextBuilder builder = ProviderContextBuilder.of(providerName.toString(), rateTypeSet);
    builder.set(CHILD_PROVIDER_CONTEXTS_KEY, childContextList);
    return builder.build();
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ProviderContext(javax.money.convert.ProviderContext) RateType(javax.money.convert.RateType) ProviderContextBuilder(javax.money.convert.ProviderContextBuilder)

Example 15 with ExchangeRateProvider

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

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(ConversionQuery query) {
    Collection<String> providers = getProvidersToUse(query);
    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.isEmpty()) {
        throw new MonetaryException("No such providers: " + query);
    }
    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)

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