public static class RandomStringGenerator.Builder extends Object implements Builder<RandomStringGenerator>
A builder for generating RandomStringGenerator
instances.
The behaviour 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 |
---|---|
static int |
DEFAULT_LENGTH
The default string length produced by this builder: 0.
|
static int |
DEFAULT_MAXIMUM_CODE_POINT
The default maximum code point allowed:
Character.MAX_CODE_POINT
(1114111). |
static int |
DEFAULT_MINIMUM_CODE_POINT
The default minimum code point allowed: 0.
|
Constructor and Description |
---|
Builder() |
Modifier and Type | Method and Description |
---|---|
RandomStringGenerator |
build()
Builds the
RandomStringGenerator using the properties specified. |
RandomStringGenerator.Builder |
filteredBy(CharacterPredicate... predicates)
Limits the characters in the generated string to those that match at
least one of the predicates supplied.
|
RandomStringGenerator.Builder |
selectFrom(char... chars)
Limits the characters in the generated string to those who match at
supplied list of Character.
|
RandomStringGenerator.Builder |
usingRandom(TextRandomProvider random)
Overrides the default source of randomness.
|
RandomStringGenerator.Builder |
withinRange(char[]... pairs)
Specifies the array of minimum and maximum char allowed in the
generated string.
|
RandomStringGenerator.Builder |
withinRange(int minimumCodePoint,
int maximumCodePoint)
Specifies the minimum and maximum code points allowed in the
generated string.
|
public static final int DEFAULT_MAXIMUM_CODE_POINT
Character.MAX_CODE_POINT
(1114111).public static final int DEFAULT_LENGTH
public static final int DEFAULT_MINIMUM_CODE_POINT
public RandomStringGenerator.Builder withinRange(int minimumCodePoint, int maximumCodePoint)
Specifies the minimum and maximum code points allowed in the generated string.
minimumCodePoint
- the smallest code point allowed (inclusive)maximumCodePoint
- the largest code point allowed (inclusive)this
, to allow method chainingIllegalArgumentException
- if maximumCodePoint >
Character.MAX_CODE_POINT
IllegalArgumentException
- if minimumCodePoint < 0
IllegalArgumentException
- if minimumCodePoint > maximumCodePoint
public RandomStringGenerator.Builder withinRange(char[]... pairs)
Specifies 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'}};
pairs
- array of characters array, expected is to pass min, max pairs through this arg.this
, to allow method chaining.public RandomStringGenerator.Builder filteredBy(CharacterPredicate... predicates)
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 behaviour of allowing any character. Multiple calls to this
method will replace the previously stored predicates.
predicates
- the predicates, may be null
or emptythis
, to allow method chainingpublic RandomStringGenerator.Builder usingRandom(TextRandomProvider random)
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.
When using Java 8 or later, TextRandomProvider
is a
functional interface and need not be explicitly implemented:
UniformRandomProvider rng = RandomSource.create(...);
RandomStringGenerator gen = new RandomStringGenerator.Builder()
.usingRandom(rng::nextInt)
// additional builder calls as needed
.build();
Passing null
to this method will revert to the default source of
randomness.
random
- the source of randomness, may be null
this
, to allow method chainingpublic RandomStringGenerator.Builder selectFrom(char... chars)
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 behaviour of allowing any character. Multiple calls to this
method will replace the previously stored Character.
chars
- set of predefined Characters for random string generation
the Character can be, may be null
or emptythis
, to allow method chainingpublic RandomStringGenerator build()
Builds the RandomStringGenerator
using the properties specified.
build
in interface Builder<RandomStringGenerator>
RandomStringGenerator
Copyright © 2014–2018 The Apache Software Foundation. All rights reserved.