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 2024/02/22 08:30:26 UTC

(commons-statistics) branch master updated (d3cd5c8 -> 89a8e28)

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

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


    from d3cd5c8  Canonical download location has moved
     new 66c68c8  Throw NPE if user-supplied arguments are null
     new 2238d91  Fix variable name
     new 89a8e28  Add since tag

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/statistics/ranking/NaturalRanking.java | 30 +++++++++---
 .../commons/statistics/ranking/package-info.java   |  2 +
 .../statistics/ranking/NaturalRankingTest.java     | 56 +++++++++++++++-------
 3 files changed, 65 insertions(+), 23 deletions(-)


(commons-statistics) 03/03: Add since tag

Posted by ah...@apache.org.
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-statistics.git

commit 89a8e285e9504c2f01a28f27cebbb4ebf94e45dc
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Feb 22 08:20:13 2024 +0000

    Add since tag
---
 .../main/java/org/apache/commons/statistics/ranking/package-info.java   | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/package-info.java b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/package-info.java
index ff9a5b5..f11581a 100644
--- a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/package-info.java
+++ b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/package-info.java
@@ -17,5 +17,7 @@
 
 /**
  * Classes providing rank transformations.
+ *
+ * @since 1.1
  */
 package org.apache.commons.statistics.ranking;


(commons-statistics) 01/03: Throw NPE if user-supplied arguments are null

Posted by ah...@apache.org.
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-statistics.git

commit 66c68c802ade16523298d91882a6eb9e6c925a5f
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Feb 22 08:19:46 2024 +0000

    Throw NPE if user-supplied arguments are null
---
 .../commons/statistics/ranking/NaturalRanking.java | 30 ++++++++++++++----
 .../statistics/ranking/NaturalRankingTest.java     | 36 +++++++++++++++++-----
 2 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java
index 1445592..5bb81ac 100644
--- a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java
+++ b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java
@@ -17,6 +17,7 @@
 package org.apache.commons.statistics.ranking;
 
 import java.util.Arrays;
+import java.util.Objects;
 import java.util.SplittableRandom;
 import java.util.function.DoubleUnaryOperator;
 import java.util.function.IntUnaryOperator;
@@ -31,7 +32,7 @@ import java.util.function.IntUnaryOperator;
  * {@link TiesStrategy#AVERAGE}, respectively.
  *
  * <p>When using {@link TiesStrategy#RANDOM}, a generator of random values in {@code [0, x)}
- * supplied as a {@link IntUnaryOperator} argument; otherwise a default is created
+ * can be supplied as a {@link IntUnaryOperator} argument; otherwise a default is created
  * on-demand. The source of randomness can be supplied using a method reference.
  * The following example creates a ranking with NaN values with the highest
  * ranking and ties resolved randomly:
@@ -83,6 +84,12 @@ import java.util.function.IntUnaryOperator;
  * @since 1.1
  */
 public class NaturalRanking implements RankingAlgorithm {
+    /** Message for a null user-supplied {@link NaNStrategy}. */
+    private static final String NULL_NAN_STRATEGY = "nanStrategy";
+    /** Message for a null user-supplied {@link TiesStrategy}. */
+    private static final String NULL_TIES_STRATEGY = "tiesStrategy";
+    /** Message for a null user-supplied source of randomness. */
+    private static final String NULL_RANDOM_SOURCE = "randomIntFunction";
     /** Default NaN strategy. */
     private static final NaNStrategy DEFAULT_NAN_STRATEGY = NaNStrategy.FAILED;
     /** Default ties strategy. */
@@ -121,9 +128,11 @@ public class NaturalRanking implements RankingAlgorithm {
      * source of randomness is used to resolve ties.
      *
      * @param tiesStrategy TiesStrategy to use.
+     * @throws NullPointerException if the strategy is {@code null}
      */
     public NaturalRanking(TiesStrategy tiesStrategy) {
-        this(DEFAULT_NAN_STRATEGY, tiesStrategy, null);
+        this(DEFAULT_NAN_STRATEGY,
+            Objects.requireNonNull(tiesStrategy, NULL_TIES_STRATEGY), null);
     }
 
     /**
@@ -131,9 +140,11 @@ public class NaturalRanking implements RankingAlgorithm {
      * {@link TiesStrategy#AVERAGE}.
      *
      * @param nanStrategy NaNStrategy to use.
+     * @throws NullPointerException if the strategy is {@code null}
      */
     public NaturalRanking(NaNStrategy nanStrategy) {
-        this(nanStrategy, DEFAULT_TIES_STRATEGY, null);
+        this(Objects.requireNonNull(nanStrategy, NULL_NAN_STRATEGY),
+            DEFAULT_TIES_STRATEGY, null);
     }
 
     /**
@@ -145,10 +156,12 @@ public class NaturalRanking implements RankingAlgorithm {
      *
      * @param nanStrategy NaNStrategy to use.
      * @param tiesStrategy TiesStrategy to use.
+     * @throws NullPointerException if any strategy is {@code null}
      */
     public NaturalRanking(NaNStrategy nanStrategy,
                           TiesStrategy tiesStrategy) {
-        this(nanStrategy, tiesStrategy, null);
+        this(Objects.requireNonNull(nanStrategy, NULL_NAN_STRATEGY),
+            Objects.requireNonNull(tiesStrategy, NULL_TIES_STRATEGY), null);
     }
 
     /**
@@ -157,9 +170,11 @@ public class NaturalRanking implements RankingAlgorithm {
      *
      * @param randomIntFunction Source of random index data.
      * Function maps positive {@code x} randomly to {@code [0, x)}
+     * @throws NullPointerException if the source of randomness is {@code null}
      */
     public NaturalRanking(IntUnaryOperator randomIntFunction) {
-        this(DEFAULT_NAN_STRATEGY, TiesStrategy.RANDOM, randomIntFunction);
+        this(DEFAULT_NAN_STRATEGY, TiesStrategy.RANDOM,
+            Objects.requireNonNull(randomIntFunction, NULL_RANDOM_SOURCE));
     }
 
     /**
@@ -169,10 +184,12 @@ public class NaturalRanking implements RankingAlgorithm {
      * @param nanStrategy NaNStrategy to use.
      * @param randomIntFunction Source of random index data.
      * Function maps positive {@code x} randomly to {@code [0, x)}
+     * @throws NullPointerException if the strategy or source of randomness are {@code null}
      */
     public NaturalRanking(NaNStrategy nanStrategy,
                           IntUnaryOperator randomIntFunction) {
-        this(nanStrategy, TiesStrategy.RANDOM, randomIntFunction);
+        this(Objects.requireNonNull(nanStrategy, NULL_NAN_STRATEGY), TiesStrategy.RANDOM,
+            Objects.requireNonNull(randomIntFunction, NULL_RANDOM_SOURCE));
     }
 
     /**
@@ -183,6 +200,7 @@ public class NaturalRanking implements RankingAlgorithm {
     private NaturalRanking(NaNStrategy nanStrategy,
                            TiesStrategy tiesStrategy,
                            IntUnaryOperator randomIntFunction) {
+        // User-supplied arguments are checked for non-null in the respective constructor
         this.nanStrategy = nanStrategy;
         this.tiesStrategy = tiesStrategy;
         this.randomIntFunction = randomIntFunction;
diff --git a/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java b/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
index d292a01..7d8c5e3 100644
--- a/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
+++ b/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.statistics.ranking;
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.function.IntUnaryOperator;
 import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
@@ -30,6 +31,7 @@ import org.apache.commons.rng.sampling.PermutationSampler;
 import org.apache.commons.rng.simple.RandomSource;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.CsvSource;
@@ -56,7 +58,7 @@ class NaturalRankingTest {
     void testProperties() {
         final TiesStrategy defaultTs = TiesStrategy.AVERAGE;
         final NaNStrategy defaultNs = NaNStrategy.FAILED;
-        final IntUnaryOperator randomSource = null;
+        final IntUnaryOperator randomSource = x -> x;
         NaturalRanking ranking;
 
         ranking = new NaturalRanking();
@@ -89,12 +91,32 @@ class NaturalRankingTest {
     }
 
     @Test
-    void testNullStrategy() {
-        final double[] data = new double[5];
-        final NaturalRanking r1 = new NaturalRanking((NaNStrategy)null);
-        Assertions.assertThrows(NullPointerException.class, () -> r1.apply(data));
-        final NaturalRanking r2 = new NaturalRanking((TiesStrategy)null);
-        Assertions.assertThrows(NullPointerException.class, () -> r2.apply(data));
+    void testNullArguments() {
+        final TiesStrategy nullTiesStrategy = null;
+        final TiesStrategy tiesStrategy = TiesStrategy.AVERAGE;
+        final NaNStrategy nullNaNStrategy = null;
+        final NaNStrategy nanStrategy = NaNStrategy.FIXED;
+        final IntUnaryOperator nullRandomness = null;
+        final IntUnaryOperator randomness = x -> x;
+        assertThrowsNPEWithKeywords(() -> new NaturalRanking(nullTiesStrategy), "ties", "strategy");
+        assertThrowsNPEWithKeywords(() -> new NaturalRanking(nullNaNStrategy), "nan", "strategy");
+        assertThrowsNPEWithKeywords(
+            () -> new NaturalRanking(nullNaNStrategy, tiesStrategy), "nan", "strategy");
+        assertThrowsNPEWithKeywords(
+            () -> new NaturalRanking(nanStrategy, nullTiesStrategy), "ties", "strategy");
+        assertThrowsNPEWithKeywords(() -> new NaturalRanking(nullRandomness), "random");
+        assertThrowsNPEWithKeywords(
+            () -> new NaturalRanking(nullNaNStrategy, randomness), "nan", "strategy");
+        assertThrowsNPEWithKeywords(
+            () -> new NaturalRanking(nanStrategy, nullRandomness), "random");
+    }
+
+    private static void assertThrowsNPEWithKeywords(Executable executable, String... keywords) {
+        final NullPointerException e = Assertions.assertThrows(NullPointerException.class, executable);
+        final String msg = e.getMessage().toLowerCase(Locale.ROOT);
+        for (final String s : keywords) {
+            Assertions.assertTrue(msg.contains(s), () -> "Missing keyword: " + s);
+        }
     }
 
     /**


(commons-statistics) 02/03: Fix variable name

Posted by ah...@apache.org.
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-statistics.git

commit 2238d916a259d8578143252e3a8363b053a492e5
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu Feb 22 08:20:01 2024 +0000

    Fix variable name
---
 .../statistics/ranking/NaturalRankingTest.java       | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java b/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
index 7d8c5e3..98017dd 100644
--- a/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
+++ b/commons-statistics-ranking/src/test/java/org/apache/commons/statistics/ranking/NaturalRankingTest.java
@@ -530,11 +530,11 @@ class NaturalRankingTest {
      * @param expectedGroups Expected groups (if null the algorithm is expected to raise an {@link IllegalArgumentException})
      * @param msg Prefix for any assertion failure message
      */
-    private static void assertRanks(RankingAlgorithm ranking, double[] data, int[] expectedGroup, String msg) {
-        if (expectedGroup == null) {
+    private static void assertRanks(RankingAlgorithm ranking, double[] data, int[] expectedGroups, String msg) {
+        if (expectedGroups == null) {
             Assertions.assertThrows(IllegalArgumentException.class, () -> ranking.apply(data));
         } else {
-            Assertions.assertEquals(data.length, expectedGroup.length, "Groups must be assigned to all data");
+            Assertions.assertEquals(data.length, expectedGroups.length, "Groups must be assigned to all data");
 
             final double[] original = data.clone();
             final double[] ranks = ranking.apply(data);
@@ -549,14 +549,14 @@ class NaturalRankingTest {
             int numberOfElements = 0;
             int unchanged = 0;
             int removed = 0;
-            for (int i = 0; i < expectedGroup.length; i++) {
-                if (expectedGroup[i] > 0) {
-                    max = Math.max(max, expectedGroup[i]);
+            for (int i = 0; i < expectedGroups.length; i++) {
+                if (expectedGroups[i] > 0) {
+                    max = Math.max(max, expectedGroups[i]);
                     // Reduce to only the expected elements.
                     // This filters unchanged/removed elements.
-                    expectedGroup[numberOfElements] = expectedGroup[i];
+                    expectedGroups[numberOfElements] = expectedGroups[i];
                     numberOfElements++;
-                } else if (expectedGroup[i] == 0) {
+                } else if (expectedGroups[i] == 0) {
                     Assertions.assertEquals(data[i], ranks[i], "Element was changed");
                     // Flag for removal
                     ranks[i] = -1;
@@ -586,7 +586,7 @@ class NaturalRankingTest {
             // Count groups sizes
             final int[] sizes = new int[max + 1];
             for (int i = 0; i < numberOfElements; i++) {
-                sizes[expectedGroup[i]]++;
+                sizes[expectedGroups[i]]++;
             }
             // Each must be non-zero
             for (int i = 1; i <= max; i++) {
@@ -620,7 +620,7 @@ class NaturalRankingTest {
                 Assertions.assertEquals(r, (int) r, () -> "Non-integer rank: " + r);
                 final int rank = (int) r;
                 final BitSet groupSet = rankToGroup[rank];
-                final int group = expectedGroup[i];
+                final int group = expectedGroups[i];
                 Assertions.assertTrue(groupSet.get(rank),
                     () -> String.format("Unexpected rank %d in group %d", rank, group));
                 groupSet.clear(rank);