use of javax.money.CurrencyUnit in project jsr354-ri 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.orElse(currency.getDefaultFractionDigits());
BigDecimal value = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode);
return amount.getFactory().setNumber(value).create();
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class JDKCurrencyProvider method getCurrencies.
/**
* Return a {@link CurrencyUnit} instances matching the given
* {@link javax.money.CurrencyContext}.
*
* @param currencyQuery the {@link javax.money.CurrencyContext} 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<>();
if (!currencyQuery.getCurrencyCodes().isEmpty()) {
for (String code : currencyQuery.getCurrencyCodes()) {
CurrencyUnit cu = CACHED.get(code);
if (cu != null) {
result.add(cu);
}
}
return result;
}
if (!currencyQuery.getCountries().isEmpty()) {
for (Locale country : currencyQuery.getCountries()) {
CurrencyUnit cu = getCurrencyUnit(country);
if (cu != null) {
result.add(cu);
}
}
return result;
}
if (!currencyQuery.getNumericCodes().isEmpty()) {
for (Integer numCode : currencyQuery.getNumericCodes()) {
List<CurrencyUnit> cus = getCurrencyUnits(numCode);
result.addAll(cus);
}
return result;
}
// No constraints defined, return all.
result.addAll(CACHED.values());
return result;
}
use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.
the class GroupMonetarySummaryStatistics method combine.
public GroupMonetarySummaryStatistics combine(GroupMonetarySummaryStatistics another) {
Objects.requireNonNull(another);
for (CurrencyUnit keyCurrency : another.groupSummary.keySet()) {
groupSummary.putIfAbsent(keyCurrency, new DefaultMonetarySummaryStatistics(keyCurrency));
groupSummary.merge(keyCurrency, another.groupSummary.get(keyCurrency), MonetarySummaryStatistics::combine);
}
return this;
}
use of javax.money.CurrencyUnit in project jsr354-ri-bp by JavaMoney.
the class MonetaryOperatorsTest method shouldRouding.
@Test
public void shouldRouding() {
CurrencyUnit euro = Monetary.getCurrency("EUR");
MonetaryAmount money = Money.parse("EUR 2.355432");
MonetaryAmount result = MonetaryOperators.rounding().apply(money);
assertNotNull(result);
assertEquals(result.getCurrency(), euro);
assertEquals(2.36, result.getNumber().doubleValue());
}
use of javax.money.CurrencyUnit in project jsr354-ri-bp by JavaMoney.
the class PercentOperatorTest method shouldReturnNegativeValue.
@Test
public void shouldReturnNegativeValue() {
CurrencyUnit currency = Monetary.getCurrency("BHD");
MonetaryAmount money = Money.parse("BHD -200.0");
MonetaryAmount result = operator.apply(money);
assertEquals(result.getCurrency(), currency);
assertEquals(result.getNumber().doubleValue(), -20.0);
}
Aggregations