Class RandomStringGenerator.Builder
- java.lang.Object
-
- org.apache.commons.text.RandomStringGenerator.Builder
-
- All Implemented Interfaces:
Builder<RandomStringGenerator>
- Enclosing class:
- RandomStringGenerator
public static class RandomStringGenerator.Builder extends java.lang.Object implements Builder<RandomStringGenerator>
A builder for generating
RandomStringGeneratorinstances.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
Builderinstance to allow for method chaining.The minimum and maximum code point values are defined using
withinRange(int, int). The default values are0andCharacter.MAX_CODE_POINTrespectively.The source of randomness can be set using
usingRandom(TextRandomProvider), otherwiseThreadLocalRandomis 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 theCharacterPredicatesenum.This class is not thread safe.
- Since:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_LENGTHThe default string length produced by this builder: 0.static intDEFAULT_MAXIMUM_CODE_POINTThe default maximum code point allowed:Character.MAX_CODE_POINT(1114111).static intDEFAULT_MINIMUM_CODE_POINTThe default minimum code point allowed: 0.
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RandomStringGeneratorbuild()Builds theRandomStringGeneratorusing the properties specified.RandomStringGenerator.BuilderfilteredBy(CharacterPredicate... predicates)Limits the characters in the generated string to those that match at least one of the predicates supplied.RandomStringGenerator.BuilderselectFrom(char... chars)Limits the characters in the generated string to those who match at supplied list of Character.RandomStringGenerator.BuilderusingRandom(TextRandomProvider random)Overrides the default source of randomness.RandomStringGenerator.BuilderwithinRange(char[]... pairs)Specifies the array of minimum and maximum char allowed in the generated string.RandomStringGenerator.BuilderwithinRange(int minimumCodePoint, int maximumCodePoint)Specifies the minimum and maximum code points allowed in the generated string.
-
-
-
Field Detail
-
DEFAULT_MAXIMUM_CODE_POINT
public static final int DEFAULT_MAXIMUM_CODE_POINT
The default maximum code point allowed:Character.MAX_CODE_POINT(1114111).- See Also:
- Constant Field Values
-
DEFAULT_LENGTH
public static final int DEFAULT_LENGTH
The default string length produced by this builder: 0.- See Also:
- Constant Field Values
-
DEFAULT_MINIMUM_CODE_POINT
public static final int DEFAULT_MINIMUM_CODE_POINT
The default minimum code point allowed: 0.- See Also:
- Constant Field Values
-
-
Method Detail
-
withinRange
public RandomStringGenerator.Builder withinRange(int minimumCodePoint, int maximumCodePoint)
Specifies 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, to allow method chaining- Throws:
java.lang.IllegalArgumentException- ifmaximumCodePoint >Character.MAX_CODE_POINTjava.lang.IllegalArgumentException- ifminimumCodePoint < 0java.lang.IllegalArgumentException- ifminimumCodePoint > maximumCodePoint
-
withinRange
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'}};- Parameters:
pairs- array of characters array, expected is to pass min, max pairs through this arg.- Returns:
this, to allow method chaining.
-
filteredBy
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
nullor 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 benullor empty- Returns:
this, to allow method chaining
-
usingRandom
public 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,
TextRandomProvideris 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
nullto this method will revert to the default source of randomness.- Parameters:
random- the source of randomness, may benull- Returns:
this, to allow method chaining
-
selectFrom
public RandomStringGenerator.Builder selectFrom(char... chars)
Limits the characters in the generated string to those who match at supplied list of Character.
Passing
nullor 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 benullor empty- Returns:
this, to allow method chaining- Since:
- 1.2
-
build
public RandomStringGenerator build()
Builds the
RandomStringGeneratorusing the properties specified.- Specified by:
buildin interfaceBuilder<RandomStringGenerator>- Returns:
- The configured
RandomStringGenerator
-
-