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:19 UTC

[commons-statistics] 06/06: WIP - STATISTICS-14 - Improved code.

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 a16e51615e208e4d957151670e5cadde7308c28d
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Sun Jun 2 21:30:55 2019 +0200

    WIP - STATISTICS-14 - Improved code.
---
 .../descriptive/BigDecimalSummaryStatistics.java   | 42 +++++++++++-----------
 .../BigDecimalSummaryStatisticsTest.java           | 11 +++---
 2 files changed, 27 insertions(+), 26 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 355f638..a2625fc 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
@@ -60,7 +60,7 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
      * an empty instance is constructed.
      *
      * <p>If the arguments are inconsistent then an {@code IllegalArgumentException}
-     * is thrown.  The necessary consistent argument conditions are:
+     * is thrown. The necessary consistent argument conditions are:
      * <ul>
      * <li>{@code count >= 0}</li>
      * <li>{@code min <= max}</li>
@@ -78,26 +78,28 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
 
         if (count < 0L) {
             throw new IllegalArgumentException("count must be greater or equal to zero.");
-        }
-        if (min == null) {
-            throw new IllegalArgumentException("min is not allowed to be null.");
-        }
-        if (max == null) {
-            throw new IllegalArgumentException("max is not allowed to be null.");
-        }
-        if (sum == null) {
-            throw new IllegalArgumentException("sum is not allowed to be null.");
-        }
-
-        if (min.compareTo(max) > 0) {
-            throw new IllegalArgumentException("Minimum is greater than maximum.");
+        } else if (count > 0L) {
+            if (min == null) {
+                throw new IllegalArgumentException("min is not allowed to be null.");
+            }
+            if (max == null) {
+                throw new IllegalArgumentException("max is not allowed to be null.");
+            }
+            if (sum == null) {
+                throw new IllegalArgumentException("sum is not allowed to be null.");
+            }
+
+            if (min.compareTo(max) > 0) {
+                throw new IllegalArgumentException("Minimum is greater than maximum.");
+            }
+
+            this.count = count;
+            this.sum = sum;
+
+            this.min = min;
+            this.max = max;
         }
 
-        this.count = count;
-        this.sum = sum;
-        this.min = min;
-        this.max = max;
-        // this.variance = null ? ?
     }
 
     /**
@@ -138,8 +140,6 @@ public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {
         count += other.count;
         sum = sum.add(other.sum);
 
-        // We are only checking one of min, max cause they are
-        // both intialised the same.
         if (min == null) {
             min = other.min;
             max = other.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 a75c402..6140a96 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
@@ -55,7 +55,7 @@ class BigDecimalSummaryStatisticsTest {
         @DisplayName("should fail if max is given null.")
         void shouldFailWithNullForMin() {
             assertThatIllegalArgumentException()
-                .isThrownBy(() -> new BigDecimalSummaryStatistics(0L, null, null, null))
+                .isThrownBy(() -> new BigDecimalSummaryStatistics(1L, null, null, null))
                 .withMessage("min is not allowed to be null.");
         }
 
@@ -63,7 +63,7 @@ class BigDecimalSummaryStatisticsTest {
         @DisplayName("should fail if max is given null.")
         void shouldFailWithNullForMax() {
             assertThatIllegalArgumentException()
-                .isThrownBy(() -> new BigDecimalSummaryStatistics(0L, BigDecimal.ZERO, null, null))
+                .isThrownBy(() -> new BigDecimalSummaryStatistics(1L, BigDecimal.ZERO, null, null))
                 .withMessage("max is not allowed to be null.");
         }
 
@@ -72,7 +72,7 @@ class BigDecimalSummaryStatisticsTest {
         void shouldFailWithNullForSum() {
             assertThatIllegalArgumentException()
                 .isThrownBy(
-                    () -> new BigDecimalSummaryStatistics(0L, BigDecimal.ZERO, BigDecimal.TEN,
+                    () -> new BigDecimalSummaryStatistics(1L, BigDecimal.ZERO, BigDecimal.TEN,
                         null))
                 .withMessage("sum is not allowed to be null.");
         }
@@ -82,14 +82,14 @@ class BigDecimalSummaryStatisticsTest {
         void shouldFailForMinGreaterThanMax() {
             assertThatIllegalArgumentException()
                 .isThrownBy(
-                    () -> new BigDecimalSummaryStatistics(0L, BigDecimal.ONE, BigDecimal.ZERO,
+                    () -> new BigDecimalSummaryStatistics(1L, BigDecimal.ONE, BigDecimal.ZERO,
                         BigDecimal.ZERO))
                 .withMessage("Minimum is greater than maximum.");
         }
     }
 
     @Nested
-    @DisplayName("Parameter test")
+    @DisplayName("Parameter test for method")
     class ParameterTest {
 
         private BigDecimalSummaryStatistics bigDecimalSummaryStatistics;
@@ -210,6 +210,7 @@ class BigDecimalSummaryStatisticsTest {
             assertThat(collect.getMax()).isEqualTo(BigDecimal.valueOf(42));
 
         }
+
         @Test
         @DisplayName("statistics for elements 1..1_234_567.")
         void summaryStatisticsForOneTXXXX() {