You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/17 00:31:01 UTC

[commons-numbers] 03/03: Check all indices and the running cumulative are strictly positive.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 42a2f65b5b6543f548f951adc7c4b13e5f731a94
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Dec 17 00:30:54 2019 +0000

    Check all indices and the running cumulative are strictly positive.
---
 .../numbers/arrays/MultidimensionalCounter.java        | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
index cd8a47f..c2c94a8 100644
--- a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/MultidimensionalCounter.java
@@ -79,14 +79,14 @@ public final class MultidimensionalCounter {
         int tS = 1;
         for (int i = last - 1; i >= 0; i--) {
             final int index = i + 1;
+            checkStrictlyPositive("index size", size[index]);
             tS *= size[index];
+            checkStrictlyPositive("cumulative size", tS);
             uniCounterOffset[i] = tS;
         }
 
         totalSize = tS * size[0];
-        if (totalSize <= 0) {
-            throw new IllegalArgumentException("Negative size: " + totalSize);
-        }
+        checkStrictlyPositive("total size", totalSize);
     }
 
     /**
@@ -190,6 +190,18 @@ public final class MultidimensionalCounter {
     }
 
     /**
+     * Check the size is strictly positive: {@code size > 0}.
+     *
+     * @param name the name of the size
+     * @param size the size
+     */
+    private static void checkStrictlyPositive(String name, int size) {
+        if (size <= 0) {
+            throw new IllegalArgumentException("Not positive " + name + ": " + size);
+        }
+    }
+
+    /**
      * Creates the message for the index out of bounds exception.
      *
      * @param size the size