Search in sources :

Example 86 with CurrencyUnit

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

the class RoudingMonetaryAmountOperator method apply.

@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    Objects.requireNonNull(amount, "Amount required.");
    CurrencyUnit currency = amount.getCurrency();
    int scale = scaleOptional == null ? currency.getDefaultFractionDigits() : scaleOptional;
    BigDecimal value = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode);
    return amount.getFactory().setNumber(value).create();
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) BigDecimal(java.math.BigDecimal)

Example 87 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri-bp 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<>(currencyUnits.size());
    if (!currencyQuery.getCurrencyCodes().isEmpty()) {
        for (String code : currencyQuery.getCurrencyCodes()) {
            CurrencyUnit cu = currencyUnits.get(code);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    if (!currencyQuery.getCountries().isEmpty()) {
        for (Locale locale : currencyQuery.getCountries()) {
            CurrencyUnit cu = currencyUnitsByLocale.get(locale);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    result.addAll(currencyUnits.values());
    return result;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 88 with CurrencyUnit

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

the class DefaultRoundingProvider method getRounding.

/**
 * Evaluate the rounding that match the given query.
 *
 * @return the (shared) default rounding instances matching, never null.
 */
public MonetaryRounding getRounding(RoundingQuery roundingQuery) {
    if (roundingQuery.get(GregorianCalendar.class) != null || roundingQuery.get(Calendar.class) != null) {
        return null;
    }
    CurrencyUnit currency = roundingQuery.getCurrency();
    if (currency != null) {
        RoundingMode roundingMode = roundingQuery.get(RoundingMode.class);
        if (roundingMode == null) {
            roundingMode = RoundingMode.HALF_EVEN;
        }
        if (Boolean.TRUE.equals(roundingQuery.getBoolean("cashRounding"))) {
            if (currency.getCurrencyCode().equals("CHF")) {
                return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 5);
            } else {
                return new DefaultCashRounding(currency, 1);
            }
        }
        return new DefaultRounding(currency, roundingMode);
    }
    Integer scale = roundingQuery.getScale();
    if (scale == null) {
        scale = 2;
    }
    MathContext mc = roundingQuery.get(MathContext.class);
    RoundingMode roundingMode = roundingQuery.get(RoundingMode.class);
    if (mc != null) {
        return new DefaultRounding(scale, mc.getRoundingMode());
    } else if (roundingMode != null) {
        return new DefaultRounding(scale, roundingMode);
    } else if (roundingQuery.getRoundingName() != null && DEFAULT_ROUNDING_NAME.equals(roundingQuery.getRoundingName())) {
        return Monetary.getDefaultRounding();
    }
    return null;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext)

Example 89 with CurrencyUnit

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

the class MonetarySummaryMap method get.

@Override
public MonetarySummaryStatistics get(Object key) {
    if (CurrencyUnit.class.isInstance(key)) {
        CurrencyUnit unit = CurrencyUnit.class.cast(key);
        MonetarySummaryStatistics stats = map.get(key);
        if (stats == null) {
            stats = new DefaultMonetarySummaryStatistics(unit);
            map.put(unit, stats);
        }
        return stats;
    }
    return map.get(key);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 90 with CurrencyUnit

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

the class ExchangeCurrencyOperatorTest method shouldExchangeCurrencyPositiveValue.

@Test
public void shouldExchangeCurrencyPositiveValue() {
    CurrencyUnit real = Monetary.getCurrency("BRL");
    MonetaryAmount money = Money.parse("EUR 2.35");
    MonetaryAmount result = ConversionOperators.exchange(real).apply(money);
    assertNotNull(result);
    assertEquals(result.getCurrency(), real);
    assertEquals(2.35, result.getNumber().doubleValue());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

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