Skip to content

Commit

Permalink
Implement BigDecimalSummaryStatistics and BigIntegerSummaryStatistics.
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 Dec 23, 2016
1 parent 6dab5ff commit e0275c4
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 8 deletions.
17 changes: 13 additions & 4 deletions RELEASE_NOTE_DRAFT.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
8.1.0 (July 2016)
8.1.0 (TBD 2017)
====================

This is the 8.1.0 minor release.

New Functionality
-----------------

* Placeholder.
* Implement BigDecimalSummaryStatistics and BigIntegerSummaryStatistics.
* Implement SummaryStatistics and Collectors2.summarizing.
* Integrate JaCoCo for test coverage.
* Implement flatCollect on Collectors2.
* Modify PersonAndPetKataTest.getAgeStatisticsOfPets to use summaryStatistics.
* Update reference guide.
* Add Abstract primitive Stacks.
* Add the Eclipse Collections reference guide.

Optimizations
-------------

* Placeholder.
* Change collect and collectWith with target collections on InternalArrayIterate to use ensureCapacity for FastLists and ArrayLists.

Bug fixes
---------

* Placeholder.
* Remove JMH tests and generator codes from Javadoc output.
* Update links to point to the Eclipse Collections Kata instead of the GS Collections Kata.
* Fix addAll() method in CompositeFastList to return false on isEmpty().

Acquiring Eclipse Collections
-----------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2016 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 java.math.BigDecimal;
import java.math.MathContext;
import java.util.Optional;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.procedure.Procedure;

/**
* BigDecimalSummaryStatistics can be used to keep a rolling count, sum, min, max and average of BigDecimal values.
*
* @see Collectors2#summarizingBigDecimal(Function)
*/
public class BigDecimalSummaryStatistics implements Procedure<BigDecimal>
{
private static final long serialVersionUID = 1L;
private long count;
private BigDecimal sum = BigDecimal.ZERO;
private BigDecimal min;
private BigDecimal max;

@Override
public void value(BigDecimal each)
{
this.count++;
if (each != null)
{
this.sum = this.sum.add(each);
this.min = this.min == null ? each : this.min.min(each);
this.max = this.max == null ? each : this.max.max(each);
}
}

public long getCount()
{
return this.count;
}

public BigDecimal getSum()
{
return this.sum;
}

public BigDecimal getMin()
{
return this.min;
}

public Optional<BigDecimal> getMinOptional()
{
return Optional.ofNullable(this.min);
}

public BigDecimal getMax()
{
return this.max;
}

public Optional<BigDecimal> getMaxOptional()
{
return Optional.ofNullable(this.max);
}

public BigDecimal getAverage(MathContext context)
{
return this.count == 0 ? BigDecimal.ZERO : this.getSum().divide(BigDecimal.valueOf(this.count), context);
}

public BigDecimal getAverage()
{
return this.getAverage(MathContext.DECIMAL128);
}

public BigDecimalSummaryStatistics merge(BigDecimalSummaryStatistics summaryStatistics)
{
this.count += summaryStatistics.count;
this.sum = this.sum.add(summaryStatistics.sum);
if (summaryStatistics.min != null)
{
this.min = this.min == null ? summaryStatistics.min : this.min.min(summaryStatistics.min);
}
if (summaryStatistics.max != null)
{
this.max = this.max == null ? summaryStatistics.max : this.max.max(summaryStatistics.max);
}
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2016 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 java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.Optional;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.procedure.Procedure;

/**
* BigIntegerSummaryStatistics can be used to keep a rolling count, sum, min, max and average of BigInteger values.
*
* @see Collectors2#summarizingBigInteger(Function)
*/
public class BigIntegerSummaryStatistics implements Procedure<BigInteger>
{
private static final long serialVersionUID = 1L;
private long count;
private BigInteger sum = BigInteger.ZERO;
private BigInteger min;
private BigInteger max;

@Override
public void value(BigInteger each)
{
this.count++;
if (each != null)
{
this.sum = this.sum.add(each);
this.min = this.min == null ? each : this.min.min(each);
this.max = this.max == null ? each : this.max.max(each);
}
}

public long getCount()
{
return this.count;
}

public BigInteger getSum()
{
return this.sum;
}

public BigInteger getMin()
{
return this.min;
}

public Optional<BigInteger> getMinOptional()
{
return Optional.ofNullable(this.min);
}

public BigInteger getMax()
{
return this.max;
}

public Optional<BigInteger> getMaxOptional()
{
return Optional.ofNullable(this.max);
}

public BigDecimal getAverage(MathContext context)
{
return this.count == 0L ? BigDecimal.ZERO : new BigDecimal(this.getSum()).divide(BigDecimal.valueOf(this.count), context);
}

public BigDecimal getAverage()
{
return this.getAverage(MathContext.DECIMAL128);
}

public BigIntegerSummaryStatistics merge(BigIntegerSummaryStatistics summaryStatistics)
{
this.count += summaryStatistics.count;
this.sum = this.sum.add(summaryStatistics.sum);
if (summaryStatistics.min != null)
{
this.min = this.min == null ? summaryStatistics.min : this.min.min(summaryStatistics.min);
}
if (summaryStatistics.max != null)
{
this.max = this.max == null ? summaryStatistics.max : this.max.max(summaryStatistics.max);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

package org.eclipse.collections.impl.collector;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -90,7 +90,6 @@
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.ListIterate;

/**
* <p>A set of Collectors for Eclipse Collections types and algorithms.</p>
Expand Down Expand Up @@ -1687,6 +1686,30 @@ private Collectors2()
return summaryStatistics.toCollector();
}

/**
* Returns a BigDecimalSummaryStatistics applying the specified function to each element of the stream or collection.
*/
public static <T> Collector<T, ?, BigDecimalSummaryStatistics> summarizingBigDecimal(Function<? super T, BigDecimal> function)
{
return Collector.of(
BigDecimalSummaryStatistics::new,
(stats, each) -> stats.value(function.apply(each)),
BigDecimalSummaryStatistics::merge,
Collector.Characteristics.UNORDERED);
}

/**
* Returns a BigIntegerSummaryStatistics applying the specified function to each element of the stream or collection.
*/
public static <T> Collector<T, ?, BigIntegerSummaryStatistics> summarizingBigInteger(Function<? super T, BigInteger> function)
{
return Collector.of(
BigIntegerSummaryStatistics::new,
(stats, each) -> stats.value(function.apply(each)),
BigIntegerSummaryStatistics::merge,
Collector.Characteristics.UNORDERED);
}

private static <T, R extends Collection<T>> BinaryOperator<R> mergeCollections()
{
return (collection1, collection2) ->
Expand Down
Loading

0 comments on commit e0275c4

Please sign in to comment.