Java Code Examples for javax.money.convert.ExchangeRateProvider#getExchangeRate()

The following examples show how to use javax.money.convert.ExchangeRateProvider#getExchangeRate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompoundRateProvider.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Override
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
    for (ExchangeRateProvider prov : this.providers) {
        try {
            if (prov.isAvailable(conversionQuery)) {
                ExchangeRate rate = prov.getExchangeRate(conversionQuery);
                if (Objects.nonNull(rate)) {
                    return rate;
                }
            }
        } catch (Exception e) {
            throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
                "Rate Provider did not return data though at check before data was flagged as available," +
                    " provider=" + prov.getContext().getProviderName() + ", query=" + conversionQuery, e);
        }
    }
    throw new CurrencyConversionException(conversionQuery.getBaseCurrency(), conversionQuery.getCurrency(), null,
            "All delegate providers failed to deliver rate, providers=" + this.providers +
                    ", query=" + conversionQuery);
}
 
Example 2
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB");
	assertTrue(Objects.nonNull(prov));
	assertEquals(ECBCurrentRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate r = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(Objects.nonNull(r));
	assertTrue(r.isDerived());
	// Test rate provided by ECB
	r = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(Objects.nonNull(r));
	assertFalse(r.isDerived());
}
 
Example 3
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@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)
	Thread.sleep(5000L); // wait for provider to load...
	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());
}
 
Example 4
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB", "IMF");
	assertTrue(prov != null);
	assertEquals(CompoundRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate r = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(r != null);
	assertTrue(r.isDerived());
	// Test rate provided by ECB
	r = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(r != null);
	assertFalse(r.isDerived());
}
 
Example 5
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetExchangeRateProvider_Chained()
		throws InterruptedException {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB", "IMF");
	assertTrue(Objects.nonNull(prov));
	assertEquals(CompoundRateProvider.class, prov.getClass());
	// Test rate provided by IMF (derived)
	Thread.sleep(5000L); // wait for provider to load...
	ExchangeRate r = prov.getExchangeRate(Monetary.getCurrency("USD"),
			Monetary.getCurrency("INR"));
	assertTrue(Objects.nonNull(r));
	assertTrue(r.isDerived());
	// Test rate provided by ECB
	r = prov.getExchangeRate(Monetary.getCurrency("EUR"),
			Monetary.getCurrency("CHF"));
	assertTrue(Objects.nonNull(r));
	assertFalse(r.isDerived());
}
 
Example 6
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExchangeRateDefault() {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider();
	assertTrue(Objects.nonNull(prov));
	ExchangeRate rate = prov.getExchangeRate("CHF", "EUR");
	assertNotNull(rate);
}
 
Example 7
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExchangeRateDefault() {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider();
	assertTrue(prov != null);
	ExchangeRate rate = prov.getExchangeRate("CHF", "EUR");
	assertNotNull(rate);
}
 
Example 8
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExchangeRateDefault() {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider();
	assertTrue(Objects.nonNull(prov));
	ExchangeRate rate = prov.getExchangeRate("CHF", "EUR");
	assertNotNull(rate);
}