use of java.util.function.ToLongBiFunction in project portfolio by buchen.
the class PerformanceIndex method calculateAbsoluteInvestedCapital.
/**
* Calculates the absolute invested capital, i.e. starting with the first
* transaction recorded for the client.
*/
public long[] calculateAbsoluteInvestedCapital() {
ToLongBiFunction<Money, LocalDateTime> convertIfNecessary = (amount, date) -> {
if (amount.getCurrencyCode().equals(getCurrencyConverter().getTermCurrency()))
return amount.getAmount();
else
return getCurrencyConverter().convert(date, amount).getAmount();
};
long startValue = 0;
Interval interval = getActualInterval();
LocalDateTime intervalStart = interval.getStart().atStartOfDay();
for (Account account : getClient().getAccounts()) startValue += //
account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DEPOSIT || t.getType() == AccountTransaction.Type.REMOVAL).filter(//
t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
if (t.getType() == AccountTransaction.Type.DEPOSIT)
return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else if (t.getType() == AccountTransaction.Type.REMOVAL)
return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else
return 0;
}).sum();
for (Portfolio portfolio : getClient().getPortfolios()) startValue += //
portfolio.getTransactions().stream().filter(t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND || t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND).filter(//
t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
if (t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND)
return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else if (t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND)
return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else
return 0;
}).sum();
return calculateInvestedCapital(startValue);
}
Aggregations