|
16 | 16 | */ |
17 | 17 | package org.apache.commons.text; |
18 | 18 |
|
| 19 | +import java.util.function.IntUnaryOperator; |
| 20 | + |
19 | 21 | /** |
20 | 22 | * TextRandomProvider implementations are used by {@link RandomStringGenerator} |
21 | 23 | * as a source of randomness. It is highly recommended that the |
22 | 24 | * <a href="https://commons.apache.org/proper/commons-rng/">Apache Commons RNG</a> |
23 | 25 | * library be used to provide the random number generation. |
24 | 26 | * |
25 | 27 | * <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: |
28 | 32 | * </p> |
29 | 33 | * <pre> |
30 | 34 | * {@code |
31 | 35 | * UniformRandomProvider rng = RandomSource.create(...); |
32 | 36 | * RandomStringGenerator gen = RandomStringGenerator.builder() |
33 | | - * .usingRandom(rng::nextInt) |
| 37 | + * .usingRandom(rng::applyAsInt) |
34 | 38 | * // additional builder calls as needed |
35 | 39 | * .build(); |
36 | 40 | * } |
37 | 41 | * </pre> |
38 | 42 | * @since 1.1 |
39 | 43 | */ |
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 | + } |
41 | 57 |
|
42 | 58 | /** |
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)}. |
47 | 64 | */ |
| 65 | + @Deprecated |
48 | 66 | int nextInt(int max); |
49 | 67 | } |
0 commit comments