Search in sources :

Example 61 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class PrecisionContextRoundedOperatorTest method shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney.

@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
    int scale = 4;
    MathContext mathContext = new MathContext(scale, RoundingMode.HALF_EVEN);
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);
    PrecisionContextRoundedOperator monetaryOperator = PrecisionContextRoundedOperator.of(mathContext);
    MonetaryAmount result = monetaryOperator.apply(money);
    assertTrue(RoundedMoney.class.isInstance(result));
    assertEquals(result.getCurrency(), real);
    assertEquals(result.getNumber().getPrecision(), scale);
    assertEquals(BigDecimal.valueOf(35.35), result.getNumber().numberValue(BigDecimal.class));
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) RoundedMoney(org.javamoney.moneta.RoundedMoney) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 62 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ReciprocalOperatorTest method shouldReturnPositiveValue.

@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);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 63 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ExchangeRateBuilderTest method equalsTest.

@Test
public void equalsTest() {
    DefaultNumberValue factor = new DefaultNumberValue(1.1);
    DefaultNumberValue bigDecimalFactor = new DefaultNumberValue(new BigDecimal("1.1"));
    CurrencyUnit EUR = Monetary.getCurrency("EUR");
    CurrencyUnit GBP = Monetary.getCurrency("GBP");
    ExchangeRate rate1 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate2 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(factor).build();
    ExchangeRate rate3 = new ExchangeRateBuilder("myprovider", RateType.ANY).setBase(EUR).setTerm(GBP).setFactor(bigDecimalFactor).build();
    assertEquals(rate1, rate2, "Rates with same factor");
    assertEquals(rate1, rate3, "Rates with numerically equal factor");
    assertEquals(rate1.hashCode(), rate2.hashCode(), "Hashes with same factor");
    assertEquals(rate1.hashCode(), rate3.hashCode(), "Hashes with numerically equal factor");
}
Also used : DefaultNumberValue(org.javamoney.moneta.spi.DefaultNumberValue) CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 64 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class IMFHistoricRateProvider method combine.

private Map<CurrencyUnit, List<ExchangeRate>> combine(Map<CurrencyUnit, List<ExchangeRate>> source, Map<CurrencyUnit, List<ExchangeRate>> destination) {
    for (CurrencyUnit currency : source.keySet()) {
        destination.putIfAbsent(currency, new ArrayList<>());
        List<ExchangeRate> rates = source.get(currency);
        destination.merge(currency, rates, IMFHistoricRateProvider::merge);
    }
    return destination;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) ExchangeRate(javax.money.convert.ExchangeRate)

Example 65 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class ConfigurableCurrencyUnitProvider method getCurrencies.

/**
 * Return a {@link CurrencyUnit} instances matching the given
 * {@link javax.money.CurrencyContext}.
 *
 * @param currencyQuery the {@link javax.money.CurrencyQuery} containing the parameters determining the query. not null.
 * @return the corresponding {@link CurrencyUnit}, or null, if no such unit
 * is provided by this provider.
 */
public Set<CurrencyUnit> getCurrencies(CurrencyQuery currencyQuery) {
    Set<CurrencyUnit> result = new HashSet<>(CURRENCY_UNITS.size());
    if (currencyQuery.get(LocalDateTime.class) != null || currencyQuery.get(LocalDate.class) != null) {
        return Collections.emptySet();
    }
    if (!currencyQuery.getCurrencyCodes().isEmpty()) {
        for (String code : currencyQuery.getCurrencyCodes()) {
            CurrencyUnit cu = CURRENCY_UNITS.get(code);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    if (!currencyQuery.getCountries().isEmpty()) {
        for (Locale locale : currencyQuery.getCountries()) {
            CurrencyUnit cu = CURRENCY_UNITS_BY_LOCALE.get(locale);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    result.addAll(CURRENCY_UNITS.values());
    return result;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Aggregations

CurrencyUnit (javax.money.CurrencyUnit)116 Test (org.testng.annotations.Test)78 MonetaryAmount (javax.money.MonetaryAmount)57 BigDecimal (java.math.BigDecimal)14 Currency (java.util.Currency)8 MonetaryException (javax.money.MonetaryException)6 ExchangeRate (javax.money.convert.ExchangeRate)6 RoundedMoney (org.javamoney.moneta.RoundedMoney)6 IOException (java.io.IOException)5 MathContext (java.math.MathContext)5 MonetaryParseException (javax.money.format.MonetaryParseException)5 Money (org.javamoney.moneta.Money)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)3 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2