Let RandomUtils reuse RandomStringUtils.random()

This commit is contained in:
Gary Gregory 2024-07-13 18:43:03 -04:00
parent e30167b3b3
commit 845ba4a77a
3 changed files with 9 additions and 26 deletions

View File

@ -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>

View File

@ -62,7 +62,7 @@ public class RandomStringUtils {
}
});
private static SecureRandom random() {
static SecureRandom random() {
return RANDOM.get();
}

View File

@ -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 static boolean nextBoolean() {
*/
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 static double nextDouble(final double startInclusive, final double endExc
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 static float nextFloat(final float startInclusive, final float endExclusi
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 static int nextInt(final int startInclusive, final int endExclusive) {
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 @@ private static long nextLong(final long n) {
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 static long nextLong(final long startInclusive, final long endExclusive)
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