Java Code Examples for javax.money.Monetary#getCurrency()

The following examples show how to use javax.money.Monetary#getCurrency() . 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: ECBRateReadingHandler.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
@Override
public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException {
    if ("Cube".equals(qName)) {
        if (Objects.nonNull(attributes.getValue("time"))) {

            this.localDate = LocalDate.parse(attributes.getValue("time")).atStartOfDay().toLocalDate();
        } else if (Objects.nonNull(attributes.getValue("currency"))) {
            // read data <Cube currency="USD" rate="1.3349"/>
            CurrencyUnit tgtCurrency = Monetary
                    .getCurrency(attributes.getValue("currency"));
            addRate(tgtCurrency, this.localDate, BigDecimal.valueOf(Double
                    .parseDouble(attributes.getValue("rate"))));
        }
    }
    super.startElement(uri, localName, qName, attributes);
}
 
Example 2
Source File: ConfigurableCurrencyUnitProviderTest.java    From jsr354-ri with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that searching by numeric code is supported by {@link ConfigurableCurrencyUnitProvider}.
 */
@Test
public void testSearchByNumericCurrencyCode() {
    CurrencyUnit usd = CurrencyUnitBuilder.of("USD", "search-test")
                    .setNumericCode(840)
                    .setDefaultFractionDigits(2)
                    .build(false);
    CurrencyUnit eur = CurrencyUnitBuilder.of("EUR", "search-test")
                    .setNumericCode(978)
                    .setDefaultFractionDigits(2)
                    .build(false);
    
    ConfigurableCurrencyUnitProvider.registerCurrencyUnit(usd);
    ConfigurableCurrencyUnitProvider.registerCurrencyUnit(eur);
    try {
        CurrencyQuery query = CurrencyQueryBuilder.of()
            .setProviderName(ConfigurableCurrencyUnitProvider.class.getSimpleName())
            .setNumericCodes(840)
            .build();
        CurrencyUnit currency = Monetary.getCurrency(query);
        assertEquals(usd, currency);
    } finally {
        ConfigurableCurrencyUnitProvider.removeCurrencyUnit(usd.getCurrencyCode());
        ConfigurableCurrencyUnitProvider.removeCurrencyUnit(eur.getCurrencyCode());
    }
}
 
Example 3
Source File: RoundingMonetaryAmountOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPositiveValueUsingRoundingType() {
	operator = new RoundingMonetaryAmountOperator(RoundingMode.HALF_EVEN);
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 2.3523");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), 2.35);

}
 
Example 4
Source File: RoundingMonetaryAmountOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPositiveValue() {
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 2.3523");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), 2.35);

}
 
Example 5
Source File: ReciprocalOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPositiveValue() {
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 2.0");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), 0.5);
}
 
Example 6
Source File: MonetaryReducerOperations.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
private static List<MonetaryAmount> aFistfulOfDollars() {
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
	List<MonetaryAmount> moneys = new ArrayList<>();
	moneys.add(Money.of(120, dollar));
	moneys.add(Money.of(50, dollar));
	moneys.add(Money.of(80, dollar));
	moneys.add(Money.of(90, dollar));
	moneys.add(Money.of(120, dollar));
	return moneys;
}
 
Example 7
Source File: ExtractorMinorPartOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNegativeValue() {
	CurrencyUnit currency = Monetary.getCurrency("BHD");
	MonetaryAmount money = Money.parse("BHD -1.345");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), -0.345);

}
 
Example 8
Source File: CreateMonetaryCurrency.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	CurrencyUnit real = Monetary.getCurrency("BRL");
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
       MonetaryAmount money = Money.of(120, real);
       MonetaryAmount fastMoney = FastMoney.of(80, dollar);
       System.out.println(money);
       System.out.println(fastMoney);
}
 
Example 9
Source File: ConversionOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExchangeCurrencyNegativeValue() {
	CurrencyUnit real = Monetary.getCurrency("BRL");
	MonetaryAmount money = Money.parse("BHD -1.345");
	MonetaryAmount result = ConversionOperators.exchange(real).apply(money);
	assertNotNull(result);
	assertEquals(result.getCurrency(), real);
	assertEquals(-1.345d, result.getNumber().doubleValue());
}
 
Example 10
Source File: MonetarySorterOperations.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
public static List<MonetaryAmount> getDollars() {
	CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
	List<MonetaryAmount> moneys = new ArrayList<>();
	moneys.add(Money.of(120, dollar));
	moneys.add(Money.of(50, dollar));
	moneys.add(Money.of(80, dollar));
	moneys.add(Money.of(90, dollar));
	moneys.add(Money.of(120, dollar));
	return moneys;
}
 
Example 11
Source File: RoundingMonetaryAmountOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNegativeValue() {
	CurrencyUnit currency = Monetary.getCurrency("BHD");
	MonetaryAmount money = Money.parse("BHD -1.34534432");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), -1.345);
}
 
Example 12
Source File: MonetaryOperatorsTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRoundingUsingScale() {
	CurrencyUnit euro = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR 2.355432");
	MonetaryAmount result = MonetaryOperators.rounding(4).apply(money);
	assertNotNull(result);
	assertEquals(result.getCurrency(), euro);
	assertEquals(2.3554d, result.getNumber().doubleValue());
}
 
Example 13
Source File: SmokeTest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateAmounts() {
    // Creating one
    CurrencyUnit currency = Monetary.getCurrency("CHF");
    Money amount1 = Money.of(1.0d, currency);
    Money amount2 = Money.of(1.0d, currency);
    Money amount3 = amount1.add(amount2);
    logger.debug(amount1 + " + " + amount2 + " = " + amount3);
    assertEquals(1.0d, amount1.getNumber().doubleValue(), 0);
    assertEquals(1.0d, amount2.getNumber().doubleValue(), 0);
    assertEquals(2.0d, amount3.getNumber().doubleValue(), 0);
}
 
Example 14
Source File: PermilOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNegativeValue() {
	CurrencyUnit currency = Monetary.getCurrency("EUR");
	MonetaryAmount money = Money.parse("EUR -2.35");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), -0.0235);

}
 
Example 15
Source File: PercentOperatorTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNegativeValue() {
	CurrencyUnit currency = Monetary.getCurrency("BHD");
	MonetaryAmount money = Money.parse("BHD -200.0");
	MonetaryAmount result = operator.apply(money);
	assertEquals(result.getCurrency(), currency);
	assertEquals(result.getNumber().doubleValue(), -20.0);
}
 
Example 16
Source File: ConvertMinorPartQueryTest.java    From jsr354-ri with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ArithmeticException.class)
public void shouldReturnWhenTheValueIsBiggerThanLong() {
	CurrencyUnit real = Monetary.getCurrency("BRL");
	MonetaryAmount monetaryAmount = Money.of(Long.MAX_VALUE, real);
	query.queryFrom(monetaryAmount.add(Money.of(10, real)));
	fail();
}
 
Example 17
Source File: MoneyProducerTest.java    From jsr354-ri with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void setup() {
	producer = new MoneyProducer();
	currency = Monetary.getCurrency(Locale.getDefault());
}
 
Example 18
Source File: CurrencyUnitPair.java    From consensusj with Apache License 2.0 4 votes vote down vote up
/**
 * @param pair A string of the form "base/target"
 */
public CurrencyUnitPair(String pair) {
    this(Monetary.getCurrency(pair.split("/")[0]), Monetary.getCurrency(pair.split("/")[1]));
}
 
Example 19
Source File: CurrenciesFactory.java    From javamoney-examples with Apache License 2.0 4 votes vote down vote up
@Produces
@Europe
public CurrencyUnit getEurope() {
	return Monetary.getCurrency("EUR");
}
 
Example 20
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public MonetaryAmount parse(String text, Locale locale) throws ParseException {
	CurrencyUnit currencyUnit = Monetary.getCurrency(locale);
	Number numberValue = this.numberFormatter.parse(text, locale);
	return Monetary.getDefaultAmountFactory().setNumber(numberValue).setCurrency(currencyUnit).create();
}