Package org.jeasy.random
Class EasyRandomParameters
- java.lang.Object
-
- org.jeasy.random.EasyRandomParameters
-
public class EasyRandomParameters extends java.lang.Object
Parameters of anEasyRandom
instance.- Author:
- Mahmoud Ben Hassine ([email protected])
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EasyRandomParameters.Range<T>
Utility class to hold a range of values.
-
Field Summary
Fields Modifier and Type Field Description static java.nio.charset.Charset
DEFAULT_CHARSET
Default charset for Strings.static EasyRandomParameters.Range<java.lang.Integer>
DEFAULT_COLLECTION_SIZE_RANGE
Default collection size range.static int
DEFAULT_DATE_RANGE
Default date range in which dates will be generated: [now - 10 years, now + 10 years].static EasyRandomParameters.Range<java.time.ZonedDateTime>
DEFAULT_DATES_RANGE
Default dates range.static int
DEFAULT_OBJECT_POOL_SIZE
Number of different objects to generate for a type.static int
DEFAULT_RANDOMIZATION_DEPTH
Default value for randomization depth, which mean, that randomization depth is unlimitedstatic long
DEFAULT_SEED
Default seed.static EasyRandomParameters.Range<java.lang.Integer>
DEFAULT_STRING_LENGTH_RANGE
Default string length size.
-
Constructor Summary
Constructors Constructor Description EasyRandomParameters()
Create a newEasyRandomParameters
with default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EasyRandomParameters
bypassSetters(boolean bypassSetters)
Flag to bypass setters if any and use reflection directly instead.EasyRandomParameters
charset(java.nio.charset.Charset charset)
Set the charset to use for character based fields.EasyRandomParameters
collectionSizeRange(int minCollectionSize, int maxCollectionSize)
Set the collection size range.EasyRandomParameters
copy()
Return a shallow copy of randomization parameters.EasyRandomParameters
dateRange(java.time.LocalDate min, java.time.LocalDate max)
Set the date range.EasyRandomParameters
excludeField(java.util.function.Predicate<java.lang.reflect.Field> predicate)
Exclude a field from being randomized.EasyRandomParameters
excludeType(java.util.function.Predicate<java.lang.Class<?>> predicate)
Exclude a type from being randomized.EasyRandomParameters
exclusionPolicy(ExclusionPolicy exclusionPolicy)
Provide a custom exclusion policy.java.nio.charset.Charset
getCharset()
EasyRandomParameters.Range<java.lang.Integer>
getCollectionSizeRange()
EasyRandomParameters.Range<java.time.LocalDate>
getDateRange()
ExclusionPolicy
getExclusionPolicy()
java.util.Set<java.util.function.Predicate<java.lang.reflect.Field>>
getFieldExclusionPredicates()
ObjectFactory
getObjectFactory()
int
getObjectPoolSize()
int
getRandomizationDepth()
RandomizerProvider
getRandomizerProvider()
long
getSeed()
EasyRandomParameters.Range<java.lang.Integer>
getStringLengthRange()
EasyRandomParameters.Range<java.time.LocalTime>
getTimeRange()
java.util.Set<java.util.function.Predicate<java.lang.Class<?>>>
getTypeExclusionPredicates()
EasyRandomParameters
ignoreRandomizationErrors(boolean ignoreRandomizationErrors)
With this parameter, any randomization error will be silently ignored and the corresponding field will be set to null.boolean
isBypassSetters()
boolean
isIgnoreRandomizationErrors()
boolean
isOverrideDefaultInitialization()
boolean
isScanClasspathForConcreteTypes()
EasyRandomParameters
objectFactory(ObjectFactory objectFactory)
Provide a custom object factory.EasyRandomParameters
objectPoolSize(int objectPoolSize)
Set the number of different objects to generate for a type.EasyRandomParameters
overrideDefaultInitialization(boolean overrideDefaultInitialization)
Should default initialization of field values be overridden? E.g.EasyRandomParameters
randomizationDepth(int randomizationDepth)
Set the randomization depth for objects graph.<T> EasyRandomParameters
randomize(java.lang.Class<T> type, Randomizer<T> randomizer)
Register a custom randomizer for a given type.<T> EasyRandomParameters
randomize(java.util.function.Predicate<java.lang.reflect.Field> predicate, Randomizer<T> randomizer)
Register a custom randomizer for the given field predicate.EasyRandomParameters
randomizerProvider(RandomizerProvider randomizerProvider)
Provide a custom randomizer provider.EasyRandomParameters
randomizerRegistry(RandomizerRegistry registry)
Register aRandomizerRegistry
.EasyRandomParameters
scanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes)
Should the classpath be scanned for concrete types when a field with an interface or abstract class type is encountered? Deactivated by default.EasyRandomParameters
seed(long seed)
Set the initial random seed.void
setBypassSetters(boolean bypassSetters)
void
setCharset(java.nio.charset.Charset charset)
void
setCollectionSizeRange(EasyRandomParameters.Range<java.lang.Integer> collectionSizeRange)
void
setDateRange(EasyRandomParameters.Range<java.time.LocalDate> dateRange)
void
setExclusionPolicy(ExclusionPolicy exclusionPolicy)
void
setIgnoreRandomizationErrors(boolean ignoreRandomizationErrors)
void
setObjectFactory(ObjectFactory objectFactory)
void
setObjectPoolSize(int objectPoolSize)
void
setOverrideDefaultInitialization(boolean overrideDefaultInitialization)
void
setRandomizationDepth(int randomizationDepth)
void
setRandomizerProvider(RandomizerProvider randomizerProvider)
void
setScanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes)
void
setSeed(long seed)
void
setStringLengthRange(EasyRandomParameters.Range<java.lang.Integer> stringLengthRange)
void
setTimeRange(EasyRandomParameters.Range<java.time.LocalTime> timeRange)
EasyRandomParameters
stringLengthRange(int minStringLength, int maxStringLength)
Set the string length range.EasyRandomParameters
timeRange(java.time.LocalTime min, java.time.LocalTime max)
Set the time range.
-
-
-
Field Detail
-
DEFAULT_SEED
public static final long DEFAULT_SEED
Default seed.- See Also:
- Constant Field Values
-
DEFAULT_CHARSET
public static final java.nio.charset.Charset DEFAULT_CHARSET
Default charset for Strings.
-
DEFAULT_COLLECTION_SIZE_RANGE
public static final EasyRandomParameters.Range<java.lang.Integer> DEFAULT_COLLECTION_SIZE_RANGE
Default collection size range.
-
DEFAULT_OBJECT_POOL_SIZE
public static final int DEFAULT_OBJECT_POOL_SIZE
Number of different objects to generate for a type.- See Also:
- Constant Field Values
-
DEFAULT_RANDOMIZATION_DEPTH
public static final int DEFAULT_RANDOMIZATION_DEPTH
Default value for randomization depth, which mean, that randomization depth is unlimited- See Also:
- Constant Field Values
-
DEFAULT_STRING_LENGTH_RANGE
public static final EasyRandomParameters.Range<java.lang.Integer> DEFAULT_STRING_LENGTH_RANGE
Default string length size.
-
DEFAULT_DATE_RANGE
public static final int DEFAULT_DATE_RANGE
Default date range in which dates will be generated: [now - 10 years, now + 10 years].- See Also:
- Constant Field Values
-
DEFAULT_DATES_RANGE
public static final EasyRandomParameters.Range<java.time.ZonedDateTime> DEFAULT_DATES_RANGE
Default dates range.
-
-
Constructor Detail
-
EasyRandomParameters
public EasyRandomParameters()
Create a newEasyRandomParameters
with default values.
-
-
Method Detail
-
getCollectionSizeRange
public EasyRandomParameters.Range<java.lang.Integer> getCollectionSizeRange()
-
setCollectionSizeRange
public void setCollectionSizeRange(EasyRandomParameters.Range<java.lang.Integer> collectionSizeRange)
-
getDateRange
public EasyRandomParameters.Range<java.time.LocalDate> getDateRange()
-
setDateRange
public void setDateRange(EasyRandomParameters.Range<java.time.LocalDate> dateRange)
-
getTimeRange
public EasyRandomParameters.Range<java.time.LocalTime> getTimeRange()
-
setTimeRange
public void setTimeRange(EasyRandomParameters.Range<java.time.LocalTime> timeRange)
-
getStringLengthRange
public EasyRandomParameters.Range<java.lang.Integer> getStringLengthRange()
-
setStringLengthRange
public void setStringLengthRange(EasyRandomParameters.Range<java.lang.Integer> stringLengthRange)
-
getSeed
public long getSeed()
-
setSeed
public void setSeed(long seed)
-
getObjectPoolSize
public int getObjectPoolSize()
-
setObjectPoolSize
public void setObjectPoolSize(int objectPoolSize)
-
getRandomizationDepth
public int getRandomizationDepth()
-
setRandomizationDepth
public void setRandomizationDepth(int randomizationDepth)
-
getCharset
public java.nio.charset.Charset getCharset()
-
setCharset
public void setCharset(java.nio.charset.Charset charset)
-
isScanClasspathForConcreteTypes
public boolean isScanClasspathForConcreteTypes()
-
setScanClasspathForConcreteTypes
public void setScanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes)
-
isOverrideDefaultInitialization
public boolean isOverrideDefaultInitialization()
-
setOverrideDefaultInitialization
public void setOverrideDefaultInitialization(boolean overrideDefaultInitialization)
-
isIgnoreRandomizationErrors
public boolean isIgnoreRandomizationErrors()
-
setIgnoreRandomizationErrors
public void setIgnoreRandomizationErrors(boolean ignoreRandomizationErrors)
-
isBypassSetters
public boolean isBypassSetters()
-
setBypassSetters
public void setBypassSetters(boolean bypassSetters)
-
getExclusionPolicy
public ExclusionPolicy getExclusionPolicy()
-
setExclusionPolicy
public void setExclusionPolicy(ExclusionPolicy exclusionPolicy)
-
getObjectFactory
public ObjectFactory getObjectFactory()
-
setObjectFactory
public void setObjectFactory(ObjectFactory objectFactory)
-
getRandomizerProvider
public RandomizerProvider getRandomizerProvider()
-
setRandomizerProvider
public void setRandomizerProvider(RandomizerProvider randomizerProvider)
-
getFieldExclusionPredicates
public java.util.Set<java.util.function.Predicate<java.lang.reflect.Field>> getFieldExclusionPredicates()
-
getTypeExclusionPredicates
public java.util.Set<java.util.function.Predicate<java.lang.Class<?>>> getTypeExclusionPredicates()
-
randomize
public <T> EasyRandomParameters randomize(java.util.function.Predicate<java.lang.reflect.Field> predicate, Randomizer<T> randomizer)
Register a custom randomizer for the given field predicate. The predicate must at least specify the field type- Type Parameters:
T
- The field type- Parameters:
predicate
- to identify the fieldrandomizer
- to register- Returns:
- the current
EasyRandomParameters
instance for method chaining - See Also:
FieldPredicates
-
randomize
public <T> EasyRandomParameters randomize(java.lang.Class<T> type, Randomizer<T> randomizer)
Register a custom randomizer for a given type.- Type Parameters:
T
- The field type- Parameters:
type
- class of the type to randomizerandomizer
- the customRandomizer
to use- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
excludeField
public EasyRandomParameters excludeField(java.util.function.Predicate<java.lang.reflect.Field> predicate)
Exclude a field from being randomized.- Parameters:
predicate
- to identify the field to exclude- Returns:
- the current
EasyRandomParameters
instance for method chaining - See Also:
FieldPredicates
-
excludeType
public EasyRandomParameters excludeType(java.util.function.Predicate<java.lang.Class<?>> predicate)
Exclude a type from being randomized.- Parameters:
predicate
- to identify the type to exclude- Returns:
- the current
EasyRandomParameters
instance for method chaining - See Also:
FieldPredicates
-
exclusionPolicy
public EasyRandomParameters exclusionPolicy(ExclusionPolicy exclusionPolicy)
Provide a custom exclusion policy.- Parameters:
exclusionPolicy
- to use- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
objectFactory
public EasyRandomParameters objectFactory(ObjectFactory objectFactory)
Provide a custom object factory.- Parameters:
objectFactory
- to use- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
randomizerProvider
public EasyRandomParameters randomizerProvider(RandomizerProvider randomizerProvider)
Provide a custom randomizer provider.- Parameters:
randomizerProvider
- to use- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
seed
public EasyRandomParameters seed(long seed)
Set the initial random seed.- Parameters:
seed
- the initial seed- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
collectionSizeRange
public EasyRandomParameters collectionSizeRange(int minCollectionSize, int maxCollectionSize)
Set the collection size range.- Parameters:
minCollectionSize
- the minimum collection sizemaxCollectionSize
- the maximum collection size- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
stringLengthRange
public EasyRandomParameters stringLengthRange(int minStringLength, int maxStringLength)
Set the string length range.- Parameters:
minStringLength
- the minimum string lengthmaxStringLength
- the maximum string length- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
objectPoolSize
public EasyRandomParameters objectPoolSize(int objectPoolSize)
Set the number of different objects to generate for a type.- Parameters:
objectPoolSize
- the number of objects to generate in the pool- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
randomizationDepth
public EasyRandomParameters randomizationDepth(int randomizationDepth)
Set the randomization depth for objects graph.- Parameters:
randomizationDepth
- the maximum randomization depth- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
charset
public EasyRandomParameters charset(java.nio.charset.Charset charset)
Set the charset to use for character based fields.- Parameters:
charset
- the charset to use- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
dateRange
public EasyRandomParameters dateRange(java.time.LocalDate min, java.time.LocalDate max)
Set the date range.- Parameters:
min
- datemax
- date- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
timeRange
public EasyRandomParameters timeRange(java.time.LocalTime min, java.time.LocalTime max)
Set the time range.- Parameters:
min
- timemax
- time- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
randomizerRegistry
public EasyRandomParameters randomizerRegistry(RandomizerRegistry registry)
Register aRandomizerRegistry
.- Parameters:
registry
- theRandomizerRegistry
to register- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
scanClasspathForConcreteTypes
public EasyRandomParameters scanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes)
Should the classpath be scanned for concrete types when a field with an interface or abstract class type is encountered? Deactivated by default.- Parameters:
scanClasspathForConcreteTypes
- whether to scan the classpath or not- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
ignoreRandomizationErrors
public EasyRandomParameters ignoreRandomizationErrors(boolean ignoreRandomizationErrors)
With this parameter, any randomization error will be silently ignored and the corresponding field will be set to null. Deactivated by default.- Parameters:
ignoreRandomizationErrors
- whether to silently ignore randomization errors or not- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
overrideDefaultInitialization
public EasyRandomParameters overrideDefaultInitialization(boolean overrideDefaultInitialization)
Should default initialization of field values be overridden? E.g. should the values of thestrings
andintegers
fields below be kept untouched or should they be randomized.public class Bean { Set<String> strings = new HashSet<>(); List<Integer> integers; public Bean() { integers = Arrays.asList(1, 2, 3); } }
- Parameters:
overrideDefaultInitialization
- whether to override default initialization of field values or not- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
bypassSetters
public EasyRandomParameters bypassSetters(boolean bypassSetters)
Flag to bypass setters if any and use reflection directly instead. False by default.- Parameters:
bypassSetters
- true if setters should be ignored- Returns:
- the current
EasyRandomParameters
instance for method chaining
-
copy
public EasyRandomParameters copy()
Return a shallow copy of randomization parameters.- Returns:
- a shallow copy of randomization parameters.
-
-