Sumarizando a moeda
Outra forma de agrupar o dinheiro é a partir do agrupamento do MonetaryAmount
, com ele é possível saber o valor mínimo, máximo, somatório, média e o número de elementos. Para isso é necessário informar a moeda, caso a moeda seja diferente da informada ele será ignorado.
public class AggregateSummaringMonetaryAmount {
public static void main(String[] args) {
CurrencyUnit dollar = Monetary.getCurrency("USD");
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = Money.of(10, dollar);
MonetaryAmount money2 = Money.of(10, real);
MonetaryAmount money3 = Money.of(10, dollar);
MonetaryAmount money4 = Money.of(9, real);
MonetaryAmount money5 = Money.of(25, dollar);
MonetarySummaryStatistics summary = Stream.of(money, money2, money3, money4, money5)
.collect(MonetaryFunctions.summarizingMonetary(dollar));
MonetaryAmount min = summary.getMin();//USD 10
MonetaryAmount max = summary.getMax();//USD 25
MonetaryAmount average = summary.getAverage();//USD 15
long count = summary.getCount();//3
MonetaryAmount sum = summary.getSum();//USD 45
}
}
O Moneta também tem suporte para agrupar os sumarizadores a partir da moeda.
public class AggregateGroupSummaringMonetaryAmount {
public static void main(String[] args) {
CurrencyUnit dollar = Monetary.getCurrency("USD");
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = Money.of(10, dollar);
MonetaryAmount money2 = Money.of(10, real);
MonetaryAmount money3 = Money.of(10, dollar);
MonetaryAmount money4 = Money.of(9, real);
MonetaryAmount money5 = Money.of(25, dollar);
GroupMonetarySummaryStatistics grouped = Stream.of(money, money2, money3, money4, money5)
.collect(MonetaryFunctions.groupBySummarizingMonetary());
Map<CurrencyUnit, MonetarySummaryStatistics> mapSummary = grouped.get();
MonetarySummaryStatistics summary = mapSummary.get(dollar);
MonetaryAmount min = summary.getMin();//USD 10
MonetaryAmount max = summary.getMax();//USD 25
MonetaryAmount average = summary.getAverage();//USD 15
long count = summary.getCount();//3
MonetaryAmount sum = summary.getSum();//USD 45
}
}
Também é possível sumarizar todos os valores convertendo as moedas para a moeda definida no método, para isso basta informar um ExchangeRateProvider
.
public class AggregateSummaringExchangeRateMonetaryAmount {
public static void main(String[] args) {
CurrencyUnit dollar = Monetary.getCurrency("USD");
CurrencyUnit real = Monetary.getCurrency("BRL");
ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IMF);
MonetaryAmount money = Money.of(10, dollar);
MonetaryAmount money2 = Money.of(10, real);
MonetaryAmount money3 = Money.of(10, dollar);
MonetaryAmount money4 = Money.of(9, real);
MonetaryAmount money5 = Money.of(25, dollar);
MonetarySummaryStatistics summary = Stream.of(money, money2, money3, money4, money5)
.collect(ConversionOperators.summarizingMonetary(dollar, provider));
MonetaryAmount min = summary.getMin();//USD 2.831248
MonetaryAmount max = summary.getMax();//USD 25
MonetaryAmount average = summary.getAverage();//USD 10.195416
long count = summary.getCount();//5
MonetaryAmount sum = summary.getSum();//50.97708
}
}