Skip to content

Commit 98aae9a

Browse files
committed
Interface TextRandomProvider extends IntUnaryOperator
1 parent 7a799b2 commit 98aae9a

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
4949
<!-- FIX -->
5050
<!-- ADD -->
5151
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator&lt;String&gt;.</action>
52+
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface TextRandomProvider extends IntUnaryOperator.</action>
5253
<!-- UPDATE -->
5354
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 81 to 84 #668.</action>
5455
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io from 2.18.0 to 2.19.0.</action>

src/main/java/org/apache/commons/text/RandomStringGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public String generate(final int minLengthInclusive, final int maxLengthInclusiv
459459
*/
460460
private int generateRandomNumber(final int minInclusive, final int maxInclusive) {
461461
if (random != null) {
462-
return random.nextInt(maxInclusive - minInclusive + 1) + minInclusive;
462+
return random.applyAsInt(maxInclusive - minInclusive + 1) + minInclusive;
463463
}
464464
return ThreadLocalRandom.current().nextInt(minInclusive, maxInclusive + 1);
465465
}
@@ -474,7 +474,7 @@ private int generateRandomNumber(final int minInclusive, final int maxInclusive)
474474
private int generateRandomNumber(final List<Character> characterList) {
475475
final int listSize = characterList.size();
476476
if (random != null) {
477-
return String.valueOf(characterList.get(random.nextInt(listSize))).codePointAt(0);
477+
return String.valueOf(characterList.get(random.applyAsInt(listSize))).codePointAt(0);
478478
}
479479
return String.valueOf(characterList.get(ThreadLocalRandom.current().nextInt(0, listSize))).codePointAt(0);
480480
}

src/main/java/org/apache/commons/text/TextRandomProvider.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,52 @@
1616
*/
1717
package org.apache.commons.text;
1818

19+
import java.util.function.IntUnaryOperator;
20+
1921
/**
2022
* TextRandomProvider implementations are used by {@link RandomStringGenerator}
2123
* as a source of randomness. It is highly recommended that the
2224
* <a href="https://commons.apache.org/proper/commons-rng/">Apache Commons RNG</a>
2325
* library be used to provide the random number generation.
2426
*
2527
* <p>
26-
* When using Java 8 or later, TextRandomProvider is a functional interface and
27-
* need not be explicitly implemented. For example:
28+
* {@code TextRandomProvider} is a functional interface and need not be explicitly implemented.
29+
* </p>
30+
* <p>
31+
* For example:
2832
* </p>
2933
* <pre>
3034
* {@code
3135
* UniformRandomProvider rng = RandomSource.create(...);
3236
* RandomStringGenerator gen = RandomStringGenerator.builder()
33-
* .usingRandom(rng::nextInt)
37+
* .usingRandom(rng::applyAsInt)
3438
* // additional builder calls as needed
3539
* .build();
3640
* }
3741
* </pre>
3842
* @since 1.1
3943
*/
40-
public interface TextRandomProvider {
44+
public interface TextRandomProvider extends IntUnaryOperator {
45+
46+
/**
47+
* Generates an int value between 0 (inclusive) and the specified value (exclusive).
48+
*
49+
* @param max Bound on the random number to be returned. Must be positive.
50+
* @return a random int value between 0 (inclusive) and max (exclusive).
51+
* @since 1.14.0
52+
*/
53+
@Override
54+
default int applyAsInt(final int max) {
55+
return nextInt(max);
56+
}
4157

4258
/**
43-
* Generates an int value between 0 (inclusive) and the specified value
44-
* (exclusive).
45-
* @param max Bound on the random number to be returned. Must be positive.
46-
* @return a random int value between 0 (inclusive) and n (exclusive).
59+
* Generates an int value between 0 (inclusive) and the specified value (exclusive).
60+
*
61+
* @param max Bound on the random number to be returned. Must be positive.
62+
* @return a random int value between 0 (inclusive) and max (exclusive).
63+
* @deprecated Use {@link #applyAsInt(int)}.
4764
*/
65+
@Deprecated
4866
int nextInt(int max);
4967
}

0 commit comments

Comments
 (0)