javax.money.convert.MonetaryConversions Java Examples

The following examples show how to use javax.money.convert.MonetaryConversions. 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: JavaMoneyUnitManualTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
@Ignore("Currency providers are not always available")
public void givenAmount_whenConversion_thenNotNull() {
    MonetaryAmount oneDollar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();

    CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR");

    MonetaryAmount convertedAmountUSDtoEUR = oneDollar.with(conversionEUR);

    assertEquals("USD 1", oneDollar.toString());
    assertNotNull(convertedAmountUSDtoEUR);
}
 
Example #2
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 #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(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 #4
Source File: ConversionExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
	String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
	if (args.length > 0) {
		termCurrencyCode = args[0];
	}
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
	System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));
	conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
	System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));

	System.out.println(MessageFormat.format(
			"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
			termCurrencyCode, amt.with(MonetaryConversions
					.getConversion(ConversionQueryBuilder.of()
							.setTermCurrency(termCurrencyCode)
							//.set(LocalDate.of(2015, 01, 05))
							.build()))));
}
 
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(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 #6
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 #7
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 #8
Source File: ConversionExample.java    From javamoney-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
	String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
	if (args.length > 0) {
		termCurrencyCode = args[0];
	}
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
	System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));
	conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
	System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
			termCurrencyCode, amt.with(conv)));

	System.out.println(MessageFormat.format(
			"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
			termCurrencyCode, amt.with(MonetaryConversions
					.getConversion(ConversionQueryBuilder.of()
							.setTermCurrency(termCurrencyCode)
							.set(LocalDate.of(2015, 01, 05)).build()))));
}
 
Example #9
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 #10
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 #11
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);
}
 
Example #12
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(Objects.nonNull(prov));
	assertEquals(ECBCurrentRateProvider.class, prov.getClass());
}
 
Example #13
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsIMF() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "IMF");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #14
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #15
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSupportedProviderContexts() {
	Collection<String> types = MonetaryConversions
			.getConversionProviderNames();
	assertNotNull(types);
	assertTrue(types.size() >= 1);
	assertTrue(types.contains("IMF"));
	assertTrue(types.contains("ECB"));
}
 
Example #16
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSupportedProviderContexts() {
	Collection<String> types = MonetaryConversions
			.getConversionProviderNames();
	assertNotNull(types);
	assertTrue(types.size() >= 1);
	assertTrue(types.contains("IMF"));
	assertTrue(types.contains("ECB"));
}
 
Example #17
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 #18
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 #19
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsECB() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "ECB");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #20
Source File: MonetaryConversionTest.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsIMF() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "IMF");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #21
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 #22
Source File: SmokeTests.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateMoney() {
	// Creating one
	Money amount1 = Money.of(1.0d,"CHF");
	Money amount2 = Money.of(1.0d, "EUR");
	Money amount3 = amount1.add(
			MonetaryConversions.getConversion(CHF).apply(amount2));
	LOGGER.debug(amount1 + " + " + amount2 + " = " + amount3);
	assertEquals(BigDecimal.ONE, amount1.getNumber().numberValue(BigDecimal.class));
	assertEquals(BigDecimal.ONE, amount2.getNumber().numberValue(BigDecimal.class));
	assertTrue(amount3.getNumber().doubleValue() > 1d); 
	assertEquals(CHF, amount1.getCurrency());
	assertEquals(EUR, amount2.getCurrency());
	assertEquals(CHF, amount3.getCurrency());
}
 
Example #23
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 #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: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonetaryConversionsIMF() {
	final MonetaryAmount amt = Money.of(2000, "EUR");
	CurrencyConversion conv= MonetaryConversions.getConversion("CHF", "IMF");
	MonetaryAmount converted = amt.with(conv);
	assertNotNull(converted);
}
 
Example #26
Source File: MonetaryConversionTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSupportedProviderContexts() {
	Collection<String> types = MonetaryConversions
			.getConversionProviderNames();
	assertNotNull(types);
	assertTrue(types.size() >= 1);
	assertTrue(types.contains("IMF"));
}
 
Example #27
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 #28
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 #29
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 #30
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());
}