Skip to content

Commit f78a3a0

Browse files
committed
Add RandomStringGenerator.Builder.usingRandom(IntUnaryOperator)
1 parent 31e3f0e commit f78a3a0

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
5050
<!-- ADD -->
5151
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface StringLookup now extends UnaryOperator&lt;String&gt;.</action>
5252
<action type="add" dev="ggregory" due-to="Gary Gregory">Interface TextRandomProvider extends IntUnaryOperator.</action>
53+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RandomStringGenerator.Builder.usingRandom(IntUnaryOperator).</action>
5354
<!-- UPDATE -->
5455
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 81 to 84 #668.</action>
5556
<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: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Set;
2424
import java.util.concurrent.ThreadLocalRandom;
25+
import java.util.function.IntUnaryOperator;
2526

2627
import org.apache.commons.lang3.ArrayUtils;
2728
import org.apache.commons.lang3.StringUtils;
@@ -118,7 +119,7 @@ public static class Builder implements org.apache.commons.text.Builder<RandomStr
118119
/**
119120
* The source of randomness.
120121
*/
121-
private TextRandomProvider random;
122+
private IntUnaryOperator random;
122123

123124
/**
124125
* The source of provided characters.
@@ -216,7 +217,42 @@ public Builder selectFrom(final char... chars) {
216217
* be used to provide the random number generation.
217218
*
218219
* <p>
219-
* When using Java 8 or later, {@link TextRandomProvider} is a
220+
* {@link TextRandomProvider} is a
221+
* functional interface and need not be explicitly implemented:
222+
* </p>
223+
* <pre>
224+
* {@code
225+
* UniformRandomProvider rng = RandomSource.create(...);
226+
* RandomStringGenerator gen = RandomStringGenerator.builder()
227+
* .usingRandom(rng::nextInt)
228+
* // additional builder calls as needed
229+
* .build();
230+
* }
231+
* </pre>
232+
*
233+
* <p>
234+
* Passing {@code null} to this method will revert to the default source of
235+
* randomness.
236+
* </p>
237+
*
238+
* @param random
239+
* the source of randomness, may be {@code null}
240+
* @return {@code this}, to allow method chaining
241+
* @since 1.14.0
242+
*/
243+
public Builder usingRandom(final IntUnaryOperator random) {
244+
this.random = random;
245+
return this;
246+
}
247+
248+
/**
249+
* Overrides the default source of randomness. It is highly
250+
* recommended that a random number generator library like
251+
* <a href="https://commons.apache.org/proper/commons-rng/">Apache Commons RNG</a>
252+
* be used to provide the random number generation.
253+
*
254+
* <p>
255+
* {@link TextRandomProvider} is a
220256
* functional interface and need not be explicitly implemented:
221257
* </p>
222258
* <pre>
@@ -334,7 +370,7 @@ public static Builder builder() {
334370
/**
335371
* The source of randomness for this generator.
336372
*/
337-
private final TextRandomProvider random;
373+
private final IntUnaryOperator random;
338374

339375
/**
340376
* The source of provided characters.
@@ -355,7 +391,7 @@ public static Builder builder() {
355391
* @param characterList list of predefined set of characters.
356392
*/
357393
private RandomStringGenerator(final int minimumCodePoint, final int maximumCodePoint,
358-
final Set<CharacterPredicate> inclusivePredicates, final TextRandomProvider random,
394+
final Set<CharacterPredicate> inclusivePredicates, final IntUnaryOperator random,
359395
final List<Character> characterList) {
360396
this.minimumCodePoint = minimumCodePoint;
361397
this.maximumCodePoint = maximumCodePoint;

src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
2323
import static org.junit.jupiter.api.Assertions.fail;
2424

25+
import java.util.function.IntUnaryOperator;
26+
2527
import org.apache.commons.text.RandomStringGenerator.Builder;
2628
import org.junit.jupiter.api.Test;
2729

@@ -278,10 +280,19 @@ public void testSetLength() {
278280
}
279281

280282
@Test
281-
public void testUsingRandom() {
283+
public void testUsingRandomTextRandomProvider() {
282284
final char testChar = 'a';
283285
final TextRandomProvider testRandom = n -> testChar;
286+
final String str = RandomStringGenerator.builder().usingRandom(testRandom).build().generate(10);
287+
for (final char c : str.toCharArray()) {
288+
assertEquals(testChar, c);
289+
}
290+
}
284291

292+
@Test
293+
public void testUsingRandomIntUnaryOperator() {
294+
final char testChar = 'a';
295+
final IntUnaryOperator testRandom = n -> testChar;
285296
final String str = RandomStringGenerator.builder().usingRandom(testRandom).build().generate(10);
286297
for (final char c : str.toCharArray()) {
287298
assertEquals(testChar, c);

0 commit comments

Comments
 (0)