You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by kh...@apache.org on 2019/06/02 19:50:17 UTC
[commons-statistics] 04/06: WIP - STATISTICS-14 - Improved.
This is an automated email from the ASF dual-hosted git repository.
khmarbaise pushed a commit to branch STATISTICS-14
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 9822824dd55165186b3ebe7b57a152c4a28bde82
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Sun Jun 2 20:33:17 2019 +0200
WIP - STATISTICS-14 - Improved.
---
.../descriptive/BigDecimalSummaryStatistics.java | 49 +++++++++++------
.../BigDecimalSummaryStatisticsTest.java | 61 ++++++++++++++++------
2 files changed, 78 insertions(+), 32 deletions(-)
diff --git a/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java b/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
index 28a43a1..39d99fd 100644
--- a/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
+++ b/commons-statistics-bigdecimal/src/main/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatistics.java
@@ -17,7 +17,6 @@
package org.apache.commons.statistics.bigdecimal.descriptive;
import java.math.BigDecimal;
-import java.util.DoubleSummaryStatistics;
import java.util.function.Consumer;
/**
@@ -25,8 +24,6 @@ import java.util.function.Consumer;
*/
public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
- private static final BigDecimal INFINITY = BigDecimal.ONE;
-
/**
* internal counter.
*/
@@ -73,7 +70,8 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
* @param min the minimum value
* @param max the maximum value
* @param sum the sum of all values
- * @throws IllegalArgumentException if the arguments are inconsistent or given with {@code null}.
+ * @throws IllegalArgumentException if the arguments are inconsistent or given with {@code
+ * null}.
*/
public BigDecimalSummaryStatistics(long count, BigDecimal min, BigDecimal max,
BigDecimal sum) {
@@ -99,6 +97,7 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
this.sum = sum;
this.min = min;
this.max = max;
+ // this.variance = null ? ?
}
/**
@@ -112,11 +111,17 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
if (value == null) {
throw new IllegalArgumentException("value is not allowed to be null.");
}
+
count++;
sum = sum.add(value);
- min = min == null ? value : min.min(value);
- max = max == null ? value : max.max(value);
+ if (min == null) {
+ min = value;
+ max = value;
+ } else {
+ min = min.min(value);
+ max = max.max(value);
+ }
}
/**
@@ -133,8 +138,15 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
count += other.count;
sum = sum.add(other.sum);
- min = min == null ? other.min : min.min(other.min);
- max = max == null ? other.max : max.max(other.max);
+ // We are only checking one of min, max cause they are
+ // both intialised the same.
+ if (min == null) {
+ min = other.min;
+ max = other.max;
+ } else {
+ min = min.min(other.min);
+ max = max.max(other.max);
+ }
}
/**
@@ -156,28 +168,33 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
}
/**
- * Returns the minimum value recorded, or {@link Double#POSITIVE_INFINITY} if no values have
- * been recorded.
+ * Returns the minimum value recorded.
*
- * @return the minimum value, or {@link Double#POSITIVE_INFINITY} if none
+ * @throws IllegalStateException in case of {@code count=0}.
+ * @implSpec We can't give back a thing like Double#POSITIVE_INFINITY cause this can't be
+ * converted to a BigDecimal.
*/
public final BigDecimal getMin() {
if (this.count == 0) {
- return BigDecimal.valueOf(Double.POSITIVE_INFINITY);
+ throw new IllegalStateException(
+ "Minimum can not be calculated cause we have no values yet.");
} else {
return min;
}
}
/**
- * Returns the maximum value recorded, or {@link Double#NEGATIVE_INFINITY} if no values have
- * been recorded.
+ * Returns the maximum value recorded.
*
- * @return the maximum value, or {@link Double#NEGATIVE_INFINITY} if none
+ * @return the maximum value
+ * @throws IllegalStateException in case of {@code count=0}.
+ * @implSpec We can't give back a thing like Double#NEGATIVE_INFINITY cause this can't be
+ * converted to a BigDecimal.
*/
public final BigDecimal getMax() {
if (this.count == 0) {
- return BigDecimal.valueOf(Double.NEGATIVE_INFINITY);
+ throw new IllegalStateException(
+ "Maximum can not be calculated cause we have no values yet.");
} else {
return max;
}
diff --git a/commons-statistics-bigdecimal/src/test/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatisticsTest.java b/commons-statistics-bigdecimal/src/test/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatisticsTest.java
index 219aa04..ac97bf7 100644
--- a/commons-statistics-bigdecimal/src/test/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatisticsTest.java
+++ b/commons-statistics-bigdecimal/src/test/java/org/apache/commons/statistics/bigdecimal/descriptive/BigDecimalSummaryStatisticsTest.java
@@ -18,8 +18,10 @@ package org.apache.commons.statistics.bigdecimal.descriptive;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import java.math.BigDecimal;
+import java.util.DoubleSummaryStatistics;
import java.util.stream.LongStream;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.BeforeEach;
@@ -133,15 +135,21 @@ class BigDecimalSummaryStatisticsTest {
void sumShouldResultInZeroWhenNoValuesHaveBeenProvided() {
assertThat(this.bigDecimalSummaryStatistics.getSum()).isEqualTo(BigDecimal.ZERO);
}
+
@Test
- @DisplayName("min should result in positiv infinity if no values have been provided.")
- void minShouldResultInNegativeInfinityWhenNoValuesHaveBeenProvided() {
- assertThat(this.bigDecimalSummaryStatistics.getMin()).isEqualTo(BigDecimal.valueOf(Double.POSITIVE_INFINITY));
+ @DisplayName("min should result in exception if no values have been provided.")
+ void minShouldResultInIllegalStateExceptionWhenNoValuesHaveBeenProvided() {
+ assertThatIllegalStateException()
+ .isThrownBy(() -> this.bigDecimalSummaryStatistics.getMin())
+ .withMessage("Minimum can not be calculated cause we have no values yet.");
}
+
@Test
- @DisplayName("max should result in negative infinity if no values have been provided.")
- void maxShouldResultInNegativeInfinityWhenNoValuesHaveBeenProvided() {
- assertThat(this.bigDecimalSummaryStatistics.getMax()).isEqualTo(BigDecimal.valueOf(Double.NEGATIVE_INFINITY));
+ @DisplayName("max should result in exception if no values have been provided.")
+ void maxShouldResultInIllegalArgumentExceptionWhenNoValuesHaveBeenProvided() {
+ assertThatIllegalStateException()
+ .isThrownBy(() -> this.bigDecimalSummaryStatistics.getMax())
+ .withMessage("Maximum can not be calculated cause we have no values yet.");
}
}
@@ -185,20 +193,41 @@ class BigDecimalSummaryStatisticsTest {
}
- }
+ @Test
+ @DisplayName("statistics for elements 1..42.")
+ void summaryStatisticsForOneToFourtyTwo() {
+ BigDecimalSummaryStatistics collect =
+ LongStream.rangeClosed(1, 42)
+ .mapToObj(BigDecimal::valueOf)
+ .collect(BigDecimalSummaryStatistics::new,
+ BigDecimalSummaryStatistics::accept,
+ BigDecimalSummaryStatistics::combine);
- @Nested
- class TrialTest {
+ assertThat(collect.getCount()).isEqualTo(42L);
+ assertThat(collect.getAverage().doubleValue()).isEqualTo(21.5d, EPSILON);
+// BigDecimalSummaryStatisticsAssert.assertThat(collect).getAverage().isEqualTo(5.5d, EPSILON);
+ assertThat(collect.getSum().doubleValue()).isEqualTo(903d, EPSILON);
+ assertThat(collect.getMin()).isEqualTo(BigDecimal.ONE);
+ assertThat(collect.getMax()).isEqualTo(BigDecimal.valueOf(42));
+ }
@Test
- void XXX() {
- Double d = Double.POSITIVE_INFINITY;
- BigDecimal d1 = BigDecimal.ONE;
- BigDecimal d0 = BigDecimal.ZERO;
+ @DisplayName("statistics for elements 1..1_234_567.")
+ void summaryStatisticsForOneTXXXX() {
+ BigDecimalSummaryStatistics collect =
+ LongStream.rangeClosed(1, 1_234_567)
+ .mapToObj(BigDecimal::valueOf)
+ .collect(BigDecimalSummaryStatistics::new,
+ BigDecimalSummaryStatistics::accept,
+ BigDecimalSummaryStatistics::combine);
+
+ assertThat(collect.getCount()).isEqualTo(1_234_567L);
+ assertThat(collect.getAverage().doubleValue()).isEqualTo(617_284d, EPSILON);
+// BigDecimalSummaryStatisticsAssert.assertThat(collect).getAverage().isEqualTo(5.5d, EPSILON);
+ assertThat(collect.getSum().doubleValue()).isEqualTo(762_078_456_028d, EPSILON);
+ assertThat(collect.getMin()).isEqualTo(BigDecimal.ONE);
+ assertThat(collect.getMax()).isEqualTo(BigDecimal.valueOf(1_234_567));
- BigDecimal result = d1.divide(d0);
- System.out.println("result:" + result);
- System.out.println("D:=" + d);
}
}