Search in sources :

Example 66 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri 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 67 with CurrencyUnit

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

the class ToStringMonetaryAmountFormat method parserMonetaryAmount.

private ParserMonetaryAmount parserMonetaryAmount(CharSequence text) {
    String[] array = Objects.requireNonNull(text).toString().split(" ");
    if (array.length != 2) {
        throw new MonetaryParseException("An error happened when try to parse the Monetary Amount.", text, 0);
    }
    CurrencyUnit currencyUnit = Monetary.getCurrency(array[0]);
    BigDecimal number = new BigDecimal(array[1]);
    return new ParserMonetaryAmount(currencyUnit, number);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryParseException(javax.money.format.MonetaryParseException) BigDecimal(java.math.BigDecimal)

Example 68 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri 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 69 with CurrencyUnit

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

the class GroupMonetarySummaryStatistics method accept.

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

Example 70 with CurrencyUnit

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

the class TestCurrency method of.

public static CurrencyUnit of(Currency currency) {
    String key = ISO_NAMESPACE + ':' + currency.getCurrencyCode();
    CurrencyUnit cachedItem = CACHED.get(key);
    if (Objects.isNull(cachedItem)) {
        cachedItem = new JDKCurrencyAdapter(currency);
        CACHED.put(key, cachedItem);
    }
    return cachedItem;
}
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