javax.money.convert.ExchangeRateProvider Java Examples

The following examples show how to use javax.money.convert.ExchangeRateProvider. 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: DefaultMonetaryConversionsSingletonSpi.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
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 #3
Source File: ExchangeRateAccess.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args){
    System.out.println("Default conversion chain -> " + MonetaryConversions.getDefaultConversionProviderChain());

    ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider("IDENT");
    System.out.println("IDENT provider -> " + provider);
    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB");
    System.out.println("IDENT, ECB provider -> " + provider);

    System.out.println(provider.getExchangeRate("CHF", "EUR"));
    System.out.println(provider.getExchangeRate("CHF", "CHF"));

    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "IMF");
    System.out.println(provider.getExchangeRate("TND", "BRL"));

    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB", "ECB-HIST");
    System.out.println("CHF -> EUR (today) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
            .setBaseCurrency("EUR").setTermCurrency("CHF")
            .set(LocalDate.of(2008, 1, 8)).build()));
    System.out.println("CHF -> EUR (1.8.2008) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
            .setBaseCurrency("EUR").setTermCurrency("CHF")
            .set(LocalDate.of(2008, 1, 8)).build()));
}
 
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: ExchangeRateAccess.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args){
    System.out.println("Default conversion chain -> " + MonetaryConversions.getDefaultConversionProviderChain());

    ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider("IDENT");
    System.out.println("IDENT provider -> " + provider);
    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB");
    System.out.println("IDENT, ECB provider -> " + provider);

    System.out.println(provider.getExchangeRate("CHF", "EUR"));
    System.out.println(provider.getExchangeRate("CHF", "CHF"));

    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "IMF");
    System.out.println(provider.getExchangeRate("TND", "BRL"));

    provider = MonetaryConversions.getExchangeRateProvider("IDENT", "ECB", "ECB-HIST");
    System.out.println("CHF -> EUR (today) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
            .setBaseCurrency("EUR").setTermCurrency("CHF")
            //.set(new Date() LocalDate.of(2008, 1, 8))
            .build()));
    System.out.println("CHF -> EUR (1.8.2008) -> " + provider.getExchangeRate(ConversionQueryBuilder.of()
            .setBaseCurrency("EUR").setTermCurrency("CHF")
            //.set(LocalDate.of(2008, 1, 8))
            .build()));
}
 
Example #6
Source File: ExchangeExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	ExchangeRateProvider imfRateProvider = MonetaryConversions
			.getExchangeRateProvider("IMF");
	ExchangeRateProvider ecbRateProvider = MonetaryConversions
			.getExchangeRateProvider("ECB");

	CurrencyUnit euro = Monetary.getCurrency("EUR");
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);

	CurrencyConversion ecbDollarConvertion = ecbRateProvider
			.getCurrencyConversion(dollar);

	CurrencyConversion imfDollarConvertion = imfRateProvider
			.getCurrencyConversion(dollar);

	MonetaryAmount money = Money.of(10, euro);
	System.out.println(money.with(ecbDollarConvertion));
	System.out.println(money.with(imfDollarConvertion));
}
 
Example #7
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 #8
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 #9
Source File: DefaultMonetaryConversionsSingletonSpi.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getDefaultProviderChain() {
    List<String> provList = new ArrayList<>();
    String defaultChain = MonetaryConfig.getString("conversion.default-chain").orElse(null);
    if(defaultChain!=null) {
        String[] items = defaultChain.split(",");
        for (String item : items) {
            if (getProviderNames().contains(item.trim())) {
                provList.add(item);
            } else {
                LOG.warning("Ignoring non existing default provider: " + item);
            }
        }
    }
    else{
        Bootstrap.getServices(ExchangeRateProvider.class).forEach(
                p -> provList.add(p.getContext().getProviderName())
        );
    }
    return provList;
}
 
Example #10
Source File: DefaultMonetaryConversionsSingletonSpi.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
private Collection<String> getProvidersToUse(ConversionQuery query) {
    List<String> providersToUse = new ArrayList<>();
    List<String> providerNames = query.getProviderNames();
    if (providerNames.isEmpty()) {
        providerNames = getDefaultProviderChain();
        if (providerNames.isEmpty()) {
            throw new IllegalStateException("No default provider chain available.");
        }
    }
    for (String provider : providerNames) {
        ExchangeRateProvider prov = this.conversionProviders.get(provider);
        if (prov == null) {
            throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider);
        }
        providersToUse.add(provider);
    }
    return providersToUse;
}
 
Example #11
Source File: DefaultMonetaryConversionsSingletonSpi.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #12
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 #13
Source File: CompoundRateProvider.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #14
Source File: ExchangeRateTypeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsYahooRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider("YAHOO");
    assertTrue(Objects.nonNull(prov));
    assertEquals(YahooRateProvider.class, prov.getClass());
}
 
Example #15
Source File: OSGIActivator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) {
    LOG.info("Registering JavaMoney services...");
    OSGIServiceHelper.registerService(context.getBundle(), ExchangeRateProvider.class, ECBCurrentRateProvider.class);
    OSGIServiceHelper.registerService(context.getBundle(), ExchangeRateProvider.class, ECBHistoricRateProvider.class);
    OSGIServiceHelper.registerService(context.getBundle(), ExchangeRateProvider.class, ECBHistoric90RateProvider.class);
    LOG.info("Registered JavaMoney services...");
}
 
Example #16
Source File: OSGIActivator.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Override
public void stop(BundleContext context) {
    LOG.info("Unregistering JavaMoney services...");
    OSGIServiceHelper.unregisterService(context.getBundle(), ExchangeRateProvider.class, ECBCurrentRateProvider.class);
    OSGIServiceHelper.unregisterService(context.getBundle(), ExchangeRateProvider.class, ECBHistoricRateProvider.class);
    OSGIServiceHelper.unregisterService(context.getBundle(), ExchangeRateProvider.class, ECBHistoric90RateProvider.class);
}
 
Example #17
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExchangeRateProvider() {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB");
	assertTrue(Objects.nonNull(prov));
	assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
 
Example #18
Source File: ExchangeRateTypeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsYahooRateProviderFromType() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(YahooExchangeRateType.YAHOO);
    assertTrue(Objects.nonNull(prov));
    assertEquals(YahooRateProvider.class, prov.getClass());
}
 
Example #19
Source File: ExchangeRateTypeTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsECBCurrentRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(ExchangeRateType.ECB);
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
 
Example #20
Source File: ExchangeRateTypeTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsECBHistoricRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(ExchangeRateType.ECB_HIST);
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBHistoricRateProvider.class, prov.getClass());
}
 
Example #21
Source File: ExchangeRateTypeTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsECBHistoric90RateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(ExchangeRateType.ECB_HIST90);
    assertTrue(Objects.nonNull(prov));
    assertEquals(ECBHistoric90RateProvider.class, prov.getClass());
}
 
Example #22
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 #23
Source File: MonetaryFunctions.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
/**
 * return the minimum value, if the monetary amounts have different
 * currencies, will converter first using the given ExchangeRateProvider
 * @param provider
 *            the ExchangeRateProvider to convert the currencies
 * @return the minimum value
 */
public static BinaryOperator<MonetaryAmount> min(
		ExchangeRateProvider provider) {

	return (m1, m2) -> {
		CurrencyConversion conversion = provider.getCurrencyConversion(m1
				.getCurrency());

		if (m1.isGreaterThan(conversion.apply(m2))) {
			return m2;
		}
		return m1;
	};
}
 
Example #24
Source File: ExchangeRateTypeTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsIMFRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(ExchangeRateType.IMF);
    assertTrue(Objects.nonNull(prov));
    assertEquals(IMFRateProvider.class, prov.getClass());
}
 
Example #25
Source File: ExchangeRateTypeTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsIMFHistoricRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(ExchangeRateType.IMF_HIST);
    assertTrue(Objects.nonNull(prov));
    assertEquals(IMFHistoricRateProvider.class, prov.getClass());
}
 
Example #26
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 #27
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExchangeRateProvider() {
	ExchangeRateProvider prov = MonetaryConversions
			.getExchangeRateProvider("ECB");
	assertTrue(prov != null);
	assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
 
Example #28
Source File: USFederalReserveExchangeRateTypeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsFRBRateProviderFromType() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider(USFederalReserveExchangeRateType.FRB);
    assertTrue(Objects.nonNull(prov));
    assertEquals(USFederalReserveRateProvider.class, prov.getClass());
}
 
Example #29
Source File: USFederalReserveExchangeRateTypeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnsFRBRateProvider() {
    ExchangeRateProvider prov = MonetaryConversions
            .getExchangeRateProvider("FRB");
    assertTrue(Objects.nonNull(prov));
    assertEquals(USFederalReserveRateProvider.class, prov.getClass());
}
 
Example #30
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);
}