package gw.api.util uses gw.api.financials.CurrencyAmount uses gw.pl.currency.MonetaryAmount uses java.math.BigDecimal uses java.util.Collections uses java.util.Date uses java.util.Map /** * A multi-currency summation utility class. * * Supports summing monetary amounts in different currencies. Monetary amounts * are added to the summation after which the summation value in a specified * currency can be calculated using the {@link FXRateUtil} to perform the * conversions. */ class MultiCurrencySummation { var _summationMap : Map construct() { this({}) } construct(summationMap : Map) { _summationMap = summationMap } @java.lang.Deprecated final function add(sumValue : CurrencyAmount) { add(sumValue.Currency, sumValue.Amount) } final function add(sumValue : MonetaryAmount) { add(sumValue.Currency, sumValue.Amount) } final function add(currency : Currency, amount : BigDecimal) { var temp = _summationMap.get(currency) if (temp == null) { temp = amount } else if (amount != null) { temp += amount } _summationMap.put(currency, temp) } function add(summationMap : Map) { summationMap.eachKeyAndValue(\ currency, amount -> this.add(currency, amount)) } final function hasNullResult() : boolean { return _summationMap.Empty || (Collections.frequency(_summationMap.Values, null) == _summationMap.size()) } final function sum(currency : Currency) : MonetaryAmount { return sum(currency, \ value -> FXRateUtil.convertAmount(value, currency)) } final function sum(currency : Currency, market : FXRateMarket) : MonetaryAmount { return sum(currency, \ value -> FXRateUtil.convertAmount(value, currency, market)) } final function sum(currency : Currency, rateDate : Date) : MonetaryAmount { return sum(currency, \ value -> FXRateUtil.convertAmount(value, currency, rateDate)) } final function sum(currency : Currency, market : FXRateMarket, rateDate : Date) : MonetaryAmount { return sum(currency, \ value -> FXRateUtil.convertAmount(value, currency, rateDate, market)) } private function sum(currency : Currency, converter(amount:MonetaryAmount) : MonetaryAmount) : MonetaryAmount { if ((_summationMap.size() == 1) && _summationMap.containsKey(currency)) { var result = _summationMap.Values.first() return result == null ? null : new MonetaryAmount(result, currency)//TODO: _summationMap.Values.first()?.ofCurrency(currency) } var summation = new MonetaryAmount(0, currency)//TODO: 0bd.ofCurrency(currency) _summationMap.eachKeyAndValue(\ amountCurrency, amount -> { if (amount != null) { if (currency.equals(amountCurrency)) { summation = summation.add(new MonetaryAmount(amount, currency))//TODO: .addAsSameCurrency(amount) } else { summation += converter(new MonetaryAmount(amount, amountCurrency)/*TODO: amount.ofCurrency(amountCurrency)*/) } } }) return summation } }