-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BigDecimalSummaryStatistics and BigIntegerSummaryStatistics.
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
Showing
7 changed files
with
554 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ons/src/main/java/org/eclipse/collections/impl/collector/BigDecimalSummaryStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ons/src/main/java/org/eclipse/collections/impl/collector/BigIntegerSummaryStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.