Builder class to see available properties.
// Generates a 20 code point string, using only the letters a-z
RandomStringGenerator generator = RandomStringGenerator.builder().withinRange('a', 'z').build();
String randomLetters = generator.generate(20);
// Using Apache Commons RNG for randomness
UniformRandomProvider rng = RandomSource.create(...);
// Generates a 20 code point string, using only the letters a-z
RandomStringGenerator generator = RandomStringGenerator.builder()
.withinRange('a', 'z')
.usingRandom(rng::nextInt)
.build();
String randomLetters = generator.generate(20);
RandomStringGenerator instances are thread-safe when using the default random number generator (RNG). If a custom RNG is set by calling the method
Builder., thread-safety must be ensured externally.
| Modifier and Type | Class and Description |
|---|---|
| public static class | RandomStringGenerator.
References Deprecated
A builder for generating Builder is deprecated or references (maybe indirectly) at least one deprecated element.
RandomStringGenerator instances.
|
| Modifier and Type | Field and Description |
|---|---|
| private final List | characterList
The source of provided characters. |
| private final Set | inclusivePredicates
Filters for code points. |
| private final int | maximumCodePoint
The largest allowed code point (inclusive). |
| private final int | minimumCodePoint
The smallest allowed code point (inclusive). |
| private final IntUnaryOperator | random
The source of randomness for this generator. |
| Access | Constructor and Description |
|---|---|
| private | RandomStringGenerator(final RandomStringGenerator.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Constructs the generator. |
| Modifier and Type | Method and Description |
|---|---|
| public static RandomStringGenerator. | Returns: a new builder.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Constructs a new builder. |
| public String | Returns: The generated string.the number of code points to generate. length)Generates a random string, containing the specified number of code points. |
| public String | Returns: The generated string.the minimum (inclusive) number of code points to generate. minLengthInclusive, final int the maximum (inclusive) number of code points to generate. maxLengthInclusive)Generates a random string, containing between the minimum (inclusive) and the maximum (inclusive) number of code points. |
| private int | Returns: The random number.the minimum value allowed. minInclusive, final int the maximum value allowed. maxInclusive)Generates a random number within a range, using a |
| private int | Returns: The random number.predefined char list. characterList)Generates a random number within a range, using a |
| characterList | back to summary |
|---|---|
| private final List<Character> characterList The source of provided characters. | |
| inclusivePredicates | back to summary |
|---|---|
| private final Set<CharacterPredicate> inclusivePredicates Filters for code points. | |
| maximumCodePoint | back to summary |
|---|---|
| private final int maximumCodePoint The largest allowed code point (inclusive). | |
| minimumCodePoint | back to summary |
|---|---|
| private final int minimumCodePoint The smallest allowed code point (inclusive). | |
| random | back to summary |
|---|---|
| private final IntUnaryOperator random The source of randomness for this generator. | |
| RandomStringGenerator | back to summary |
|---|---|
| private RandomStringGenerator(final RandomStringGenerator. References Deprecated
See corresponding docs for further information. Constructs the generator. | |
| builder | back to summary |
|---|---|
| public static RandomStringGenerator. References Deprecated
See corresponding docs for further information. Constructs a new builder.
| |
| generate | back to summary |
|---|---|
| public String generate(final int length) Generates a random string, containing the specified number of code points. Code points are randomly selected between the minimum and maximum values defined in the generator. Surrogate and private use characters are not returned, although the resulting string may contain pairs of surrogates that together encode a supplementary character. Note the number of
| |
| generate | back to summary |
|---|---|
| public String generate(final int minLengthInclusive, final int maxLengthInclusive) Generates a random string, containing between the minimum (inclusive) and the maximum (inclusive) number of code points.
| |
| generateRandomNumber | back to summary |
|---|---|
| private int generateRandomNumber(final int minInclusive, final int maxInclusive) Generates a random number within a range, using a
| |
| generateRandomNumber | back to summary |
|---|---|
| private int generateRandomNumber(final List<Character> characterList) Generates a random number within a range, using a | |
References Deprecated
Builder is deprecated or references (maybe indirectly) at least one deprecated element.
See corresponding docs for further information.
RandomStringGenerator instances.
The behavior of a generator is controlled by properties set by this builder. Each property has a default value, which can be overridden by calling the
methods defined in this class, prior to calling build().
All the property setting methods return the Builder instance to allow for method chaining.
The minimum and maximum code point values are defined using withinRange(int, int). The default values are 0 and
Character#MAX_CODE_POINT respectively.
The source of randomness can be set using usingRandom(TextRandomProvider), otherwise ThreadLocalRandom is used.
The type of code points returned can be filtered using filteredBy(CharacterPredicate., which defines a collection of tests that are applied
to the randomly generated code points. The code points will only be included in the result if they pass at least one of the tests. Some commonly used
predicates are provided by the CharacterPredicates enum.
This class is not thread safe.
| Modifier and Type | Field and Description |
|---|---|
| private boolean | accumulate
Whether calls accumulates the source of provided characters. |
| private Set | characterSet
The source of provided characters. |
| public static final int | DEFAULT_LENGTH
The default string length produced by this builder: . |
| public static final int | DEFAULT_MAXIMUM_CODE_POINT
The default maximum code point allowed: |
| public static final int | DEFAULT_MINIMUM_CODE_POINT
The default minimum code point allowed: . |
| private Set | inclusivePredicates
Filters for code points. |
| private int | maximumCodePoint
The maximum code point allowed. |
| private int | minimumCodePoint
The minimum code point allowed. |
| private IntUnaryOperator | random
The source of randomness. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public RandomStringGenerator | Returns: A newRandomStringGeneratorImplements org.
Deprecated
Use
get().
Builds a new |
| public RandomStringGenerator. | Returns: this instance.the predicates, may be predicates)null or empty.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Limits the characters in the generated string to those that match at least one of the predicates supplied. |
| public RandomStringGenerator | Returns: A newRandomStringGenerator.Implements java. Builds a new |
| private void | |
| public RandomStringGenerator. | Returns: this instance.set of predefined Characters for random string generation the Character can be, may be chars)null or empty.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Limits the characters in the generated string to those who match at supplied list of Character. |
| public RandomStringGenerator. | Returns: this instance.whether calls accumulates the source of provided characters. The default is accumulate)false.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Sets whether calls accumulates the source of provided characters. |
| public RandomStringGenerator. | Returns: this instance.the source of randomness, may be random)null.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Overrides the default source of randomness. |
| public RandomStringGenerator. | Returns: this instance.the source of randomness, may be random)null.
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Overrides the default source of randomness. |
| public RandomStringGenerator. | Returns: this instance.array of characters array, expected is to pass min, max pairs through this arg. pairs)
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the array of minimum and maximum char allowed in the generated string. |
| public RandomStringGenerator. | Returns: this instance.the smallest code point allowed (inclusive). minimumCodePoint, final int the largest code point allowed (inclusive). maximumCodePoint)
References Deprecated
RandomStringGenerator.Builder is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the minimum and maximum code points allowed in the generated string. |
| accumulate | back to summary |
|---|---|
| private boolean accumulate Whether calls accumulates the source of provided characters. The default is | |
| characterSet | back to summary |
|---|---|
| private Set<Character> characterSet The source of provided characters. | |
| DEFAULT_LENGTH | back to summary |
|---|---|
| public static final int DEFAULT_LENGTH The default string length produced by this builder: . | |
| DEFAULT_MAXIMUM_CODE_POINT | back to summary |
|---|---|
| public static final int DEFAULT_MAXIMUM_CODE_POINT The default maximum code point allowed: | |
| DEFAULT_MINIMUM_CODE_POINT | back to summary |
|---|---|
| public static final int DEFAULT_MINIMUM_CODE_POINT The default minimum code point allowed: . | |
| inclusivePredicates | back to summary |
|---|---|
| private Set<CharacterPredicate> inclusivePredicates Filters for code points. | |
| maximumCodePoint | back to summary |
|---|---|
| private int maximumCodePoint The maximum code point allowed. | |
| minimumCodePoint | back to summary |
|---|---|
| private int minimumCodePoint The minimum code point allowed. | |
| random | back to summary |
|---|---|
| private IntUnaryOperator random The source of randomness. | |
| Builder | back to summary |
|---|---|
| public Builder() Creates a new instance. | |
| build | back to summary |
|---|---|
| public RandomStringGenerator build() Implements org. Deprecated Use Builds a new
| |
| filteredBy | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Limits the characters in the generated string to those that match at least one of the predicates supplied.
Passing
| |
| get | back to summary |
|---|---|
| public RandomStringGenerator get() Implements java. Builds a new
| |
| initCharList | back to summary |
|---|---|
| private void initCharList() | |
| selectFrom | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Limits the characters in the generated string to those who match at supplied list of Character.
Passing
| |
| setAccumulate | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Sets whether calls accumulates the source of provided characters. The default is
| |
| usingRandom | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Overrides the default source of randomness. It is highly recommended that a random number generator library like Apache Commons RNG be used to provide the random number generation.
Passing
| |
| usingRandom | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Overrides the default source of randomness. It is highly recommended that a random number generator library like Apache Commons RNG be used to provide the random number generation.
Passing
| |
| withinRange | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Sets the array of minimum and maximum char allowed in the generated string. For example:
| |
| withinRange | back to summary |
|---|---|
| public RandomStringGenerator. References Deprecated
See corresponding docs for further information. Sets the minimum and maximum code points allowed in the generated string.
| |