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