You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/07/13 22:43:26 UTC

(commons-lang) 01/02: Let RandomUtils reuse RandomStringUtils.random()

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

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

commit 845ba4a77a1ef2be9af26efc8a639a6368380f9a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jul 13 18:43:03 2024 -0400

    Let RandomUtils reuse RandomStringUtils.random()
---
 src/changes/changes.xml                            |  4 +--
 .../apache/commons/lang3/RandomStringUtils.java    |  2 +-
 .../java/org/apache/commons/lang3/RandomUtils.java | 29 +++++-----------------
 3 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 67ee2bcd2..d5775f0ca 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,7 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
     <title>Apache Commons Lang Release Notes</title>
   </properties>
   <body>
-  <release version="3.15.0" date="202Y-MM-DD" description="New features and bug fixes (Java 8 or above).">
+  <release version="3.15.0" date="2024-07-13" description="New features and bug fixes (Java 8 or above).">
     <!-- ADD -->
     <action issue="LANG-1724" type="add" dev="ggregory" due-to="Gary Gregory, Dennis Baerten">Customize text pattern in DiffResult#toString().</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DiffBuilder.Builder.</action>
@@ -143,7 +143,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1735" type="fix" dev="ggregory" due-to="Tobias Kiecker">Fix Javadoc for FluentBitSet.setInclusive(int, int) #1222.</action>
     <action                   type="fix" dev="ggregory" due-to="Tobias Kiecker">Same Javadoc changes as [TEXT-234] #1223.</action>  
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Remove duplicate static data in SerializationUtils.ClassLoaderAwareObjectInputStream.</action> 
-    <action                   type="fix" dev="ggregory" due-to="Gary Gregory, Henri Yandell, Fabrice Benhamouda">Reimplement RandomStringUtils on top of SecureRandom#getInstanceStrong() #1235.</action>
+    <action                   type="fix" dev="ggregory" due-to="Gary Gregory, Henri Yandell, Fabrice Benhamouda">Reimplement RandomUtils and RandomStringUtils on top of SecureRandom#getInstanceStrong() #1235.</action>
     <action issue="LANG-1657" type="fix" dev="ggregory" due-to="Matthias Welz, Andrew Thomas, Gary Gregory">DiffBuilder: Type constraint for method append(..., DiffResult) too strict #786.</action> 
     <!-- UPDATE -->
     <action                   type="update" dev="sebb" due-to="Dependabot, Gary Gregory">Bump commons-parent from 64 to 71 #1194, #1233.</action>
diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
index 419e5f0c5..e30e214eb 100644
--- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
@@ -62,7 +62,7 @@ public class RandomStringUtils {
         }
     });
 
-    private static SecureRandom random() {
+    static SecureRandom random() {
         return RANDOM.get();
     }
 
diff --git a/src/main/java/org/apache/commons/lang3/RandomUtils.java b/src/main/java/org/apache/commons/lang3/RandomUtils.java
index 19d71a3df..b3c652445 100644
--- a/src/main/java/org/apache/commons/lang3/RandomUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RandomUtils.java
@@ -17,13 +17,10 @@
 package org.apache.commons.lang3;
 
 import java.util.Random;
-import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * Utility library that supplements the standard {@link Random} class.
  *
- * <p>Caveat: Instances of {@link Random} are not cryptographically secure.</p>
- *
  * <p>Please note that the Apache Commons project provides a component
  * dedicated to pseudo-random number generation, namely
  * <a href="https://commons.apache.org/proper/commons-rng/">Commons RNG</a>, that may be
@@ -43,7 +40,7 @@ public class RandomUtils {
      * @since 3.5
      */
     public static boolean nextBoolean() {
-        return random().nextBoolean();
+        return RandomStringUtils.random().nextBoolean();
     }
 
     /**
@@ -56,9 +53,8 @@ public class RandomUtils {
      */
     public static byte[] nextBytes(final int count) {
         Validate.isTrue(count >= 0, "Count cannot be negative.");
-
         final byte[] result = new byte[count];
-        random().nextBytes(result);
+        RandomStringUtils.random().nextBytes(result);
         return result;
     }
 
@@ -89,12 +85,10 @@ public class RandomUtils {
         Validate.isTrue(endExclusive >= startInclusive,
                 "Start value must be smaller or equal to end value.");
         Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
-
         if (startInclusive == endExclusive) {
             return startInclusive;
         }
-
-        return startInclusive + (endExclusive - startInclusive) * random().nextDouble();
+        return startInclusive + (endExclusive - startInclusive) * RandomStringUtils.random().nextDouble();
     }
 
     /**
@@ -124,12 +118,10 @@ public class RandomUtils {
         Validate.isTrue(endExclusive >= startInclusive,
                 "Start value must be smaller or equal to end value.");
         Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
-
         if (startInclusive == endExclusive) {
             return startInclusive;
         }
-
-        return startInclusive + (endExclusive - startInclusive) * random().nextFloat();
+        return startInclusive + (endExclusive - startInclusive) * RandomStringUtils.random().nextFloat();
     }
 
     /**
@@ -159,12 +151,10 @@ public class RandomUtils {
         Validate.isTrue(endExclusive >= startInclusive,
                 "Start value must be smaller or equal to end value.");
         Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
-
         if (startInclusive == endExclusive) {
             return startInclusive;
         }
-
-        return startInclusive + random().nextInt(endExclusive - startInclusive);
+        return startInclusive + RandomStringUtils.random().nextInt(endExclusive - startInclusive);
     }
 
     /**
@@ -191,10 +181,9 @@ public class RandomUtils {
         long bits;
         long val;
         do {
-            bits = random().nextLong() >>> 1;
+            bits = RandomStringUtils.random().nextLong() >>> 1;
             val  = bits % n;
         } while (bits - val + n - 1 < 0);
-
         return val;
     }
 
@@ -214,18 +203,12 @@ public class RandomUtils {
         Validate.isTrue(endExclusive >= startInclusive,
                 "Start value must be smaller or equal to end value.");
         Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
-
         if (startInclusive == endExclusive) {
             return startInclusive;
         }
-
         return startInclusive + nextLong(endExclusive - startInclusive);
     }
 
-    private static ThreadLocalRandom random() {
-        return ThreadLocalRandom.current();
-    }
-
     /**
      * {@link RandomUtils} instances should NOT be constructed in standard
      * programming. Instead, the class should be used as