Search in sources :

Example 31 with ExchangeRateProvider

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

the class DefaultMonetaryConversionsSingletonSpi method getProvidersToUse.

private Collection<String> getProvidersToUse(ConversionQuery query) {
    List<String> providersToUse = new ArrayList<>();
    List<String> providers = query.getProviderNames();
    if (providers.isEmpty()) {
        providers = getDefaultProviderChain();
        if (providers.isEmpty()) {
            throw new IllegalStateException("No default provider chain available.");
        }
    }
    for (String provider : providers) {
        ExchangeRateProvider prov = this.conversionProviders.get(provider);
        if (prov == null) {
            throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider);
        }
        providersToUse.add(provider);
    }
    return providersToUse;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) MonetaryException(javax.money.MonetaryException)

Example 32 with ExchangeRateProvider

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

the class BaseMonetaryConversionsSingletonSpi method getExchangeRateProviders.

/**
 * Access the current registered {@link javax.money.convert.ExchangeRateProvider} instances. If no provider
 * names are passed ALL current registered providers are returned in undefined order.
 *
 * @param providers the provider names of hte providers to be accessed
 * @return the list of providers, in the same order as requested.
 * @throws javax.money.MonetaryException if a provider could not be resolved.
 */
public List<ExchangeRateProvider> getExchangeRateProviders(String... providers) {
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    Collection<String> providerNames = Arrays.asList(providers);
    if (providerNames.isEmpty()) {
        providerNames = getProviderNames();
    }
    for (String provName : providerNames) {
        ExchangeRateProvider provider = getExchangeRateProvider(provName);
        if (provider == null) {
            throw new MonetaryException("Unsupported conversion/rate provider: " + provName);
        }
        provInstances.add(provider);
    }
    return provInstances;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) MonetaryException(javax.money.MonetaryException)

Example 33 with ExchangeRateProvider

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

the class ECBEarlyAccessProviderTest method testAccess_HIST.

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

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

the class ECBEarlyAccessProviderTest method testAccess_ECB.

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

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

the class IMFEarlyAccessProviderTest 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)

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