Skip to content

Commit

Permalink
Implement sumByBigDecimal and sumByBigInteger on Collectors2.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@gs.com>
  • Loading branch information
Donald Raab authored and Donald Raab committed Jan 18, 2017
1 parent dd0a423 commit 10596f8
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTE_DRAFT.md
Expand Up @@ -6,6 +6,7 @@ This is the 8.1.0 minor release.
New Functionality
-----------------

* Implement summingBigDecimal, sumByBigDecimal, summingBigInteger, sumByBigInteger on Collectors2.
* Implement BigDecimalSummaryStatistics and BigIntegerSummaryStatistics.
* Implement SummaryStatistics and Collectors2.summarizing.
* Integrate JaCoCo for test coverage.
Expand Down
Expand Up @@ -21,6 +21,8 @@
* BigDecimalSummaryStatistics can be used to keep a rolling count, sum, min, max and average of BigDecimal values.
*
* @see Collectors2#summarizingBigDecimal(Function)
*
* @since 8.1
*/
public class BigDecimalSummaryStatistics implements Procedure<BigDecimal>
{
Expand Down
Expand Up @@ -22,6 +22,8 @@
* BigIntegerSummaryStatistics can be used to keep a rolling count, sum, min, max and average of BigInteger values.
*
* @see Collectors2#summarizingBigInteger(Function)
*
* @since 8.1
*/
public class BigIntegerSummaryStatistics implements Procedure<BigInteger>
{
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.collections.api.RichIterable;
Expand Down Expand Up @@ -1193,6 +1194,86 @@ private Collectors2()
Collector.Characteristics.UNORDERED);
}

/**
* <p>Groups and sums the values using the two specified functions.</p>
* <p>Examples:</p>
* {@code MutableMap<Integer, BigDecimal> sumBy1 =
* Interval.oneTo(10).stream().collect(Collectors2.sumByBigDecimal(each -> (each.intValue() % 2), BigDecimal::new));}<br>
* {@code MutableMap<Integer, BigDecimal> sumBy2 =
* Interval.oneTo(10).reduceInPlace(Collectors2.sumByBigDecimal(each -> (each.intValue() % 2), BigDecimal::new));}<br>
* <p>
* Equivalent to using @{@link Iterate#sumByBigDecimal(Iterable, Function, Function)}
* </p>
* {@code MutableMap<Integer, BigDecimal> sumBy =
* Iterate.sumByBigDecimal(Interval.oneTo(10), each -> (each.intValue() % 2), BigDecimal::new));}<br>
*
* @since 8.1
*/
public static <T, V> Collector<T, ?, MutableMap<V, BigDecimal>> sumByBigDecimal(
Function<? super T, ? extends V> groupBy,
Function<? super T, BigDecimal> function)
{
return Collector.of(
Maps.mutable::empty,
(map, each) ->
{
V key = groupBy.apply(each);
BigDecimal oldValue = map.get(key);
BigDecimal valueToAdd = function.valueOf(each);
map.put(key, oldValue == null ? valueToAdd : oldValue.add(valueToAdd));
},
(map1, map2) ->
{
map2.forEachKeyValue((key, value) ->
{
BigDecimal oldValue = map1.get(key);
map1.put(key, oldValue == null ? value : oldValue.add(value));
});
return map1;
},
Collector.Characteristics.UNORDERED);
}

/**
* <p>Groups and sums the values using the two specified functions.</p>
* <p>Examples:</p>
* {@code MutableMap<Integer, BigInteger> sumBy1 =
* Interval.oneTo(10).stream().collect(Collectors2.sumByBigInteger(each -> (each.intValue() % 2), each -> BigInteger.valueOf(each.longValue())));}<br>
* {@code MutableMap<Integer, BigInteger> sumBy2 =
* Interval.oneTo(10).reduceInPlace(Collectors2.sumByBigInteger(each -> (each.intValue() % 2), each -> BigInteger.valueOf(each.longValue())));}<br>
* <p>
* Equivalent to using @{@link Iterate#sumByBigInteger(Iterable, Function, Function)}
* </p>
* {@code MutableMap<Integer, BigInteger> sumBy =
* Iterate.sumByBigInteger(Interval.oneTo(10), each -> (each.intValue() % 2), each -> BigInteger.valueOf(each.longValue())));}<br>
*
* @since 8.1
*/
public static <T, V> Collector<T, ?, MutableMap<V, BigInteger>> sumByBigInteger(
Function<? super T, ? extends V> groupBy,
Function<? super T, BigInteger> function)
{
return Collector.of(
Maps.mutable::empty,
(map, each) ->
{
V key = groupBy.apply(each);
BigInteger oldValue = map.get(key);
BigInteger valueToAdd = function.valueOf(each);
map.put(key, oldValue == null ? valueToAdd : oldValue.add(valueToAdd));
},
(map1, map2) ->
{
map2.forEachKeyValue((key, value) ->
{
BigInteger oldValue = map1.get(key);
map1.put(key, oldValue == null ? value : oldValue.add(value));
});
return map1;
},
Collector.Characteristics.UNORDERED);
}

/**
* <p>Returns all elements of the stream that return true when evaluating the predicate. This method is also
* commonly called filter. The new collection is created as the result of evaluating the provided Supplier.</p>
Expand Down Expand Up @@ -1673,6 +1754,8 @@ private Collectors2()
/**
* Returns a SummaryStatistics with results for int, long and double functions calculated for
* each element in the Stream or Collection this Collector is applied to.
*
* @since 8.1
*/
public static <T> Collector<T, ?, SummaryStatistics<T>> summarizing(
ImmutableList<IntFunction<? super T>> intFunctions,
Expand All @@ -1688,6 +1771,8 @@ private Collectors2()

/**
* Returns a BigDecimalSummaryStatistics applying the specified function to each element of the stream or collection.
*
* @since 8.1
*/
public static <T> Collector<T, ?, BigDecimalSummaryStatistics> summarizingBigDecimal(Function<? super T, BigDecimal> function)
{
Expand All @@ -1700,6 +1785,8 @@ private Collectors2()

/**
* Returns a BigIntegerSummaryStatistics applying the specified function to each element of the stream or collection.
*
* @since 8.1
*/
public static <T> Collector<T, ?, BigIntegerSummaryStatistics> summarizingBigInteger(Function<? super T, BigInteger> function)
{
Expand All @@ -1710,6 +1797,26 @@ private Collectors2()
Collector.Characteristics.UNORDERED);
}

/**
* Returns a BigDecimal sum applying the specified function to each element of the stream or collection.
*
* @since 8.1
*/
public static <T> Collector<T, ?, BigDecimal> summingBigDecimal(Function<? super T, BigDecimal> function)
{
return Collectors.reducing(BigDecimal.ZERO, function, BigDecimal::add);
}

/**
* Returns a BigInteger sum applying the specified function to each element of the stream or collection.
*
* @since 8.1
*/
public static <T> Collector<T, ?, BigInteger> summingBigInteger(Function<? super T, BigInteger> function)
{
return Collectors.reducing(BigInteger.ZERO, function, BigInteger::add);
}

private static <T, R extends Collection<T>> BinaryOperator<R> mergeCollections()
{
return (collection1, collection2) ->
Expand Down
Expand Up @@ -21,10 +21,11 @@
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.tuple.Tuples;

/**
* A Summarizer can be used to aggregate statistics for multiple primitive attributes.
*
* @since 8.1
*/
public class SummaryStatistics<T> implements Procedure<T>
{
Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2017 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections.impl.collector;

import org.eclipse.collections.impl.test.Verify;
import org.junit.Test;

public class BigDecimalSummaryStatisticsSeralizationTest
{
@Test
public void serializedForm()
{
Verify.assertSerializedForm(
1L,
"rO0ABXNyAEJvcmcuZWNsaXBzZS5jb2xsZWN0aW9ucy5pbXBsLmNvbGxlY3Rvci5CaWdEZWNpbWFs\n"
+ "U3VtbWFyeVN0YXRpc3RpY3MAAAAAAAAAAQIABEoABWNvdW50TAADbWF4dAAWTGphdmEvbWF0aC9C\n"
+ "aWdEZWNpbWFsO0wAA21pbnEAfgABTAADc3VtcQB+AAF4cAAAAAAAAAAAcHBzcgAUamF2YS5tYXRo\n"
+ "LkJpZ0RlY2ltYWxUxxVX+YEoTwMAAkkABXNjYWxlTAAGaW50VmFsdAAWTGphdmEvbWF0aC9CaWdJ\n"
+ "bnRlZ2VyO3hyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAABzcgAUamF2YS5tYXRo\n"
+ "LkJpZ0ludGVnZXKM/J8fqTv7HQMABkkACGJpdENvdW50SQAJYml0TGVuZ3RoSQATZmlyc3ROb256\n"
+ "ZXJvQnl0ZU51bUkADGxvd2VzdFNldEJpdEkABnNpZ251bVsACW1hZ25pdHVkZXQAAltCeHEAfgAF\n"
+ "///////////////+/////gAAAAB1cgACW0Ks8xf4BghU4AIAAHhwAAAAAHh4",
new BigDecimalSummaryStatistics());
}
}
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2017 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections.impl.collector;

import org.eclipse.collections.impl.test.Verify;
import org.junit.Test;

public class BigIntegerSummaryStatisticsSeralizationTest
{
@Test
public void serializedForm()
{
Verify.assertSerializedForm(
1L,
"rO0ABXNyAEJvcmcuZWNsaXBzZS5jb2xsZWN0aW9ucy5pbXBsLmNvbGxlY3Rvci5CaWdJbnRlZ2Vy\n"
+ "U3VtbWFyeVN0YXRpc3RpY3MAAAAAAAAAAQIABEoABWNvdW50TAADbWF4dAAWTGphdmEvbWF0aC9C\n"
+ "aWdJbnRlZ2VyO0wAA21pbnEAfgABTAADc3VtcQB+AAF4cAAAAAAAAAAAcHBzcgAUamF2YS5tYXRo\n"
+ "LkJpZ0ludGVnZXKM/J8fqTv7HQMABkkACGJpdENvdW50SQAJYml0TGVuZ3RoSQATZmlyc3ROb256\n"
+ "ZXJvQnl0ZU51bUkADGxvd2VzdFNldEJpdEkABnNpZ251bVsACW1hZ25pdHVkZXQAAltCeHIAEGph\n"
+ "dmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhw///////////////+/////gAAAAB1cgACW0Ks8xf4\n"
+ "BghU4AIAAHhwAAAAAHg=",
new BigIntegerSummaryStatistics());
}
}

0 comments on commit 10596f8

Please sign in to comment.