Search in sources :

Example 21 with ExchangeRateProvider

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

the class ProviderTest 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 22 with ExchangeRateProvider

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

the class ProviderTest method testAccess_IMF.

@Test
public void testAccess_IMF() throws InterruptedException {
    final MonetaryAmount inEuro = Money.of(10, "EUR");
    for (int i = 0; i < 100; i++) {
        try {
            final ExchangeRateProvider rateProvider = MonetaryConversions.getExchangeRateProvider("IMF");
            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("IMF");
        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 23 with ExchangeRateProvider

use of javax.money.convert.ExchangeRateProvider in project jsr354-ri 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) ArrayList(java.util.ArrayList) RateType(javax.money.convert.RateType) ProviderContextBuilder(javax.money.convert.ProviderContextBuilder) HashSet(java.util.HashSet)

Example 24 with ExchangeRateProvider

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

the class MonetaryConversionTest method testGetExchangeRateProvider_Chained.

@Test
public void testGetExchangeRateProvider_Chained() throws InterruptedException {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider("IMF");
    assertTrue(Objects.nonNull(prov));
    assertEquals(IMFRateProvider.class, prov.getClass());
    // Test rate provided by IMF (derived)
    // wait for provider to load...
    Thread.sleep(5000L);
    ExchangeRate exchangeRate = prov.getExchangeRate(Monetary.getCurrency("USD"), Monetary.getCurrency("INR"));
    assertTrue(Objects.nonNull(exchangeRate));
    assertTrue(exchangeRate.isDerived());
    // Test rate provided by ECB
    exchangeRate = prov.getExchangeRate(Monetary.getCurrency("EUR"), Monetary.getCurrency("CHF"));
    assertTrue(Objects.nonNull(exchangeRate));
    assertTrue(exchangeRate.isDerived());
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) Test(org.testng.annotations.Test)

Example 25 with ExchangeRateProvider

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

the class MonetaryConversionTest method testGetExchangeRateDefault.

@Test
public void testGetExchangeRateDefault() {
    ExchangeRateProvider prov = MonetaryConversions.getExchangeRateProvider();
    assertTrue(Objects.nonNull(prov));
    ExchangeRate rate = prov.getExchangeRate("CHF", "EUR");
    assertNotNull(rate);
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) 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