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