Class RandomStringGenerator.Builder
- All Implemented Interfaces:
Supplier<RandomStringGenerator>
,Builder<RandomStringGenerator>
- Enclosing class:
RandomStringGenerator
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.
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The default string length produced by this builder: 0.static final int
The default maximum code point allowed:Character.MAX_CODE_POINT
(1114111).static final int
The default minimum code point allowed: 0. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Deprecated.filteredBy
(CharacterPredicate... predicates) Limits the characters in the generated string to those that match at least one of the predicates supplied.get()
Builds a newRandomStringGenerator
.selectFrom
(char... chars) Limits the characters in the generated string to those who match at supplied list of Character.setAccumulate
(boolean accumulate) Sets whether calls accumulates the source of provided characters.usingRandom
(IntUnaryOperator random) Overrides the default source of randomness.usingRandom
(TextRandomProvider random) Overrides the default source of randomness.withinRange
(char[]... pairs) Sets the array of minimum and maximum char allowed in the generated string.withinRange
(int minimumCodePoint, int maximumCodePoint) Sets the minimum and maximum code points allowed in the generated string.
-
Field Details
-
DEFAULT_MAXIMUM_CODE_POINT
The default maximum code point allowed:Character.MAX_CODE_POINT
(1114111).- See Also:
-
DEFAULT_LENGTH
The default string length produced by this builder: 0.- See Also:
-
DEFAULT_MINIMUM_CODE_POINT
The default minimum code point allowed: 0.- See Also:
-
-
Constructor Details
-
Builder
public Builder()Creates a new instance.
-
-
Method Details
-
build
Deprecated.Useget()
.Builds a newRandomStringGenerator
.- Specified by:
build
in interfaceBuilder<RandomStringGenerator>
- Returns:
- A new
RandomStringGenerator
-
filteredBy
Limits the characters in the generated string to those that match at least one of the predicates supplied.Passing
null
or an empty array to this method will revert to the default behavior of allowing any character. Multiple calls to this method will replace the previously stored predicates.- Parameters:
predicates
- the predicates, may benull
or empty.- Returns:
this
instance.
-
get
Builds a newRandomStringGenerator
.- Specified by:
get
in interfaceSupplier<RandomStringGenerator>
- Returns:
- A new
RandomStringGenerator
. - Since:
- 1.12.0
-
selectFrom
Limits the characters in the generated string to those who match at supplied list of Character.Passing
null
or an empty array to this method will revert to the default behavior of allowing any character. Multiple calls to this method will replace the previously stored Character.- Parameters:
chars
- set of predefined Characters for random string generation the Character can be, may benull
or empty- Returns:
this
instance.- Since:
- 1.2
-
setAccumulate
Sets whether calls accumulates the source of provided characters. The default isfalse
.RandomStringGenerator gen = RandomStringGenerator.builder() .setAccumulate(true) .withinRange(new char[][] { { 'a', 'z' }, { 'A', 'Z' }, { '0', '9' } }) .selectFrom('!', '"', '#', '$', '&', '\'', '(', ')', ',', '.', ':', ';', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~') // punctuation // additional builder calls as needed .build();
- Parameters:
accumulate
- whether calls accumulates the source of provided characters. The default isfalse
.- Returns:
this
instance.- Since:
- 1.14.0
-
usingRandom
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.TextRandomProvider
is a functional interface and need not be explicitly implemented:UniformRandomProvider rng = RandomSource.create(...); RandomStringGenerator gen = RandomStringGenerator.builder() .usingRandom(rng::nextInt) // additional builder calls as needed .build();
Passing
null
to this method will revert to the default source of randomness.- Parameters:
random
- the source of randomness, may benull
.- Returns:
this
instance.- Since:
- 1.14.0
-
usingRandom
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.TextRandomProvider
is a functional interface and need not be explicitly implemented:UniformRandomProvider rng = RandomSource.create(...); RandomStringGenerator gen = RandomStringGenerator.builder() .usingRandom(rng::nextInt) // additional builder calls as needed .build();
Passing
null
to this method will revert to the default source of randomness.- Parameters:
random
- the source of randomness, may benull
.- Returns:
this
instance.
-
withinRange
Sets the array of minimum and maximum char allowed in the generated string. For example:char[][] pairs = { { '0', '9' } }; char[][] pairs = { { 'a', 'z' } }; char[][] pairs = { { 'a', 'z' }, { '0', '9' } };
- Parameters:
pairs
- array of characters array, expected is to pass min, max pairs through this arg.- Returns:
this
instance.
-
withinRange
Sets the minimum and maximum code points allowed in the generated string.- Parameters:
minimumCodePoint
- the smallest code point allowed (inclusive).maximumCodePoint
- the largest code point allowed (inclusive).- Returns:
this
instance.- Throws:
IllegalArgumentException
- ifmaximumCodePoint >
Character.MAX_CODE_POINT
.IllegalArgumentException
- ifminimumCodePoint < 0
.IllegalArgumentException
- ifminimumCodePoint > maximumCodePoint
.
-
get()
.