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;
}
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);
}
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();
}
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;
}
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;
}
Aggregations