/** * comparator to sort the {@link MonetaryAmount} considering the {@link ExchangeRate} * * @param provider the rate provider to be used, not null. * @return the sort of {@link MonetaryAmount} using {@link ExchangeRate} */ public static Comparator<? super MonetaryAmount> sortValuable(ExchangeRateProvider provider) { return (m1, m2) -> { CurrencyConversion conversion = provider.getCurrencyConversion(m1.getCurrency()); return m1.compareTo(conversion.apply(m2)); }; }
/** * return the maximum value, if the monetary amounts have different currencies, will converter * first using the given ExchangeRateProvider * * @param provider the ExchangeRateProvider to convert the currencies * @return the maximum value */ public static BinaryOperator<MonetaryAmount> max(ExchangeRateProvider provider) { return (m1, m2) -> { CurrencyConversion conversion = provider.getCurrencyConversion(m1.getCurrency()); if (m1.isGreaterThan(conversion.apply(m2))) { return m1; } return m2; }; }
/** * return the sum and convert all values to specific currency using the provider, if necessary * * @param provider the rate provider to be used, not null. * @param currency currency * @return the list convert to specific currency unit */ public static BinaryOperator<MonetaryAmount> sum( ExchangeRateProvider provider, CurrencyUnit currency) { CurrencyConversion currencyConversion = provider.getCurrencyConversion(currency); return (m1, m2) -> currencyConversion.apply(m1).add(currencyConversion.apply(m2)); }