Search in sources :

Example 51 with CurrencyUnit

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

the class DefaultMonetaryCurrenciesSingletonSpi method getCurrencies.

@Override
public Set<CurrencyUnit> getCurrencies(CurrencyQuery query) {
    Set<CurrencyUnit> result = new HashSet<>();
    List<CurrencyProviderSpi> providers = collectProviders(query);
    for (CurrencyProviderSpi spi : providers) {
        try {
            result.addAll(spi.getCurrencies(query));
        } catch (Exception e) {
            Logger.getLogger(DefaultMonetaryCurrenciesSingletonSpi.class.getName()).log(Level.SEVERE, "Error loading currency provider names for " + spi.getClass().getName(), e);
        }
    }
    return result;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) CurrencyProviderSpi(javax.money.spi.CurrencyProviderSpi) HashSet(java.util.HashSet)

Example 52 with CurrencyUnit

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

the class ConvertMinorPartQuery method queryFrom.

/**
 * Gets the amount in minor units as a {@code long}.
 * <p>
 * This returns the monetary amount in terms of the minor units of the
 * currency, truncating the amount if necessary. For example, 'EUR 2.35'
 * will return 235, and 'BHD -1.345' will return -1345.
 * <p>
 * This method matches the API of {@link java.math.BigDecimal}.
 *
 * @return the minor units part of the amount
 * @throws ArithmeticException
 *             if the amount is too large for a {@code long}
 */
@Override
public Long queryFrom(MonetaryAmount amount) {
    Objects.requireNonNull(amount, "Amount required.");
    BigDecimal number = amount.getNumber().numberValue(BigDecimal.class);
    CurrencyUnit cur = amount.getCurrency();
    int scale = cur.getDefaultFractionDigits();
    if (scale < 0) {
        scale = 0;
    }
    number = number.setScale(scale, RoundingMode.DOWN);
    return number.movePointRight(number.scale()).longValueExact();
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) BigDecimal(java.math.BigDecimal)

Example 53 with CurrencyUnit

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

the class GroupMonetarySummaryStatistics method combine.

public GroupMonetarySummaryStatistics combine(GroupMonetarySummaryStatistics another) {
    Objects.requireNonNull(another);
    for (CurrencyUnit keyCurrency : another.groupSummary.keySet()) {
        MonetarySummaryStatistics stats = groupSummary.get(keyCurrency);
        if (stats == null) {
            stats = new DefaultMonetarySummaryStatistics(keyCurrency);
            groupSummary.put(keyCurrency, stats);
        }
        MonetarySummaryStatistics oldValue = groupSummary.get(keyCurrency);
        MonetarySummaryStatistics newValue = another.groupSummary.get(keyCurrency);
        if (oldValue != null) {
            newValue = oldValue.combine(another.groupSummary.get(keyCurrency));
        }
        groupSummary.put(keyCurrency, newValue);
    }
    return this;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 54 with CurrencyUnit

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

the class GroupMonetarySummaryStatistics method accept.

public GroupMonetarySummaryStatistics accept(MonetaryAmount amount) {
    CurrencyUnit currency = Objects.requireNonNull(amount).getCurrency();
    MonetarySummaryStatistics summary = groupSummary.get(currency);
    if (summary == null) {
        summary = new DefaultMonetarySummaryStatistics(currency);
        groupSummary.put(currency, summary);
    }
    summary.accept(amount);
    return this;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 55 with CurrencyUnit

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

the class RoudingMonetaryAmountOperatorTest method shouldReturnPositiveValueUsingRoudingTypeAndScale.

@Test
public void shouldReturnPositiveValueUsingRoudingTypeAndScale() {
    operator = new RoudingMonetaryAmountOperator(RoundingMode.HALF_EVEN, 3);
    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.352);
}
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