Package org.jeasy.random
Class EasyRandomParameters
- java.lang.Object
-
- org.jeasy.random.EasyRandomParameters
-
public class EasyRandomParameters extends java.lang.ObjectParameters of anEasyRandominstance.- Author:
- Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEasyRandomParameters.Range<T>Utility class to hold a range of values.
-
Field Summary
Fields Modifier and Type Field Description static java.nio.charset.CharsetDEFAULT_CHARSETDefault charset for Strings.static EasyRandomParameters.Range<java.lang.Integer>DEFAULT_COLLECTION_SIZE_RANGEDefault collection size range.static intDEFAULT_DATE_RANGEDefault date range in which dates will be generated: [now - 10 years, now + 10 years].static EasyRandomParameters.Range<java.time.ZonedDateTime>DEFAULT_DATES_RANGEDefault dates range.static intDEFAULT_OBJECT_POOL_SIZENumber of different objects to generate for a type.static intDEFAULT_RANDOMIZATION_DEPTHDefault value for randomization depth, which mean, that randomization depth is unlimitedstatic longDEFAULT_SEEDDefault seed.static EasyRandomParameters.Range<java.lang.Integer>DEFAULT_STRING_LENGTH_RANGEDefault string length size.
-
Constructor Summary
Constructors Constructor Description EasyRandomParameters()Create a newEasyRandomParameterswith default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EasyRandomParametersbypassSetters(boolean bypassSetters)Flag to bypass setters if any and use reflection directly instead.EasyRandomParameterscharset(java.nio.charset.Charset charset)Set the charset to use for character based fields.EasyRandomParameterscollectionSizeRange(int minCollectionSize, int maxCollectionSize)Set the collection size range.EasyRandomParameterscopy()Return a shallow copy of randomization parameters.EasyRandomParametersdateRange(java.time.LocalDate min, java.time.LocalDate max)Set the date range.EasyRandomParametersexcludeField(java.util.function.Predicate<java.lang.reflect.Field> predicate)Exclude a field from being randomized.EasyRandomParametersexcludeType(java.util.function.Predicate<java.lang.Class<?>> predicate)Exclude a type from being randomized.EasyRandomParametersexclusionPolicy(ExclusionPolicy exclusionPolicy)Provide a custom exclusion policy.java.nio.charset.CharsetgetCharset()EasyRandomParameters.Range<java.lang.Integer>getCollectionSizeRange()EasyRandomParameters.Range<java.time.LocalDate>getDateRange()ExclusionPolicygetExclusionPolicy()java.util.Set<java.util.function.Predicate<java.lang.reflect.Field>>getFieldExclusionPredicates()ObjectFactorygetObjectFactory()intgetObjectPoolSize()intgetRandomizationDepth()RandomizerProvidergetRandomizerProvider()longgetSeed()EasyRandomParameters.Range<java.lang.Integer>getStringLengthRange()EasyRandomParameters.Range<java.time.LocalTime>getTimeRange()java.util.Set<java.util.function.Predicate<java.lang.Class<?>>>getTypeExclusionPredicates()EasyRandomParametersignoreRandomizationErrors(boolean ignoreRandomizationErrors)With this parameter, any randomization error will be silently ignored and the corresponding field will be set to null.booleanisBypassSetters()booleanisIgnoreRandomizationErrors()booleanisOverrideDefaultInitialization()booleanisScanClasspathForConcreteTypes()EasyRandomParametersobjectFactory(ObjectFactory objectFactory)Provide a custom object factory.EasyRandomParametersobjectPoolSize(int objectPoolSize)Set the number of different objects to generate for a type.EasyRandomParametersoverrideDefaultInitialization(boolean overrideDefaultInitialization)Should default initialization of field values be overridden? E.g.EasyRandomParametersrandomizationDepth(int randomizationDepth)Set the randomization depth for objects graph.<T> EasyRandomParametersrandomize(java.lang.Class<T> type, Randomizer<T> randomizer)Register a custom randomizer for a given type.<T> EasyRandomParametersrandomize(java.util.function.Predicate<java.lang.reflect.Field> predicate, Randomizer<T> randomizer)Register a custom randomizer for the given field predicate.EasyRandomParametersrandomizerProvider(RandomizerProvider randomizerProvider)Provide a custom randomizer provider.EasyRandomParametersrandomizerRegistry(RandomizerRegistry registry)Register aRandomizerRegistry.EasyRandomParametersscanClasspathForConcreteTypes(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.EasyRandomParametersseed(long seed)Set the initial random seed.voidsetBypassSetters(boolean bypassSetters)voidsetCharset(java.nio.charset.Charset charset)voidsetCollectionSizeRange(EasyRandomParameters.Range<java.lang.Integer> collectionSizeRange)voidsetDateRange(EasyRandomParameters.Range<java.time.LocalDate> dateRange)voidsetExclusionPolicy(ExclusionPolicy exclusionPolicy)voidsetIgnoreRandomizationErrors(boolean ignoreRandomizationErrors)voidsetObjectFactory(ObjectFactory objectFactory)voidsetObjectPoolSize(int objectPoolSize)voidsetOverrideDefaultInitialization(boolean overrideDefaultInitialization)voidsetRandomizationDepth(int randomizationDepth)voidsetRandomizerProvider(RandomizerProvider randomizerProvider)voidsetScanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes)voidsetSeed(long seed)voidsetStringLengthRange(EasyRandomParameters.Range<java.lang.Integer> stringLengthRange)voidsetTimeRange(EasyRandomParameters.Range<java.time.LocalTime> timeRange)EasyRandomParametersstringLengthRange(int minStringLength, int maxStringLength)Set the string length range.EasyRandomParameterstimeRange(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 newEasyRandomParameterswith 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
EasyRandomParametersinstance 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 customRandomizerto use- Returns:
- the current
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance for method chaining - See Also:
FieldPredicates
-
exclusionPolicy
public EasyRandomParameters exclusionPolicy(ExclusionPolicy exclusionPolicy)
Provide a custom exclusion policy.- Parameters:
exclusionPolicy- to use- Returns:
- the current
EasyRandomParametersinstance for method chaining
-
objectFactory
public EasyRandomParameters objectFactory(ObjectFactory objectFactory)
Provide a custom object factory.- Parameters:
objectFactory- to use- Returns:
- the current
EasyRandomParametersinstance for method chaining
-
randomizerProvider
public EasyRandomParameters randomizerProvider(RandomizerProvider randomizerProvider)
Provide a custom randomizer provider.- Parameters:
randomizerProvider- to use- Returns:
- the current
EasyRandomParametersinstance for method chaining
-
seed
public EasyRandomParameters seed(long seed)
Set the initial random seed.- Parameters:
seed- the initial seed- Returns:
- the current
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance for method chaining
-
randomizerRegistry
public EasyRandomParameters randomizerRegistry(RandomizerRegistry registry)
Register aRandomizerRegistry.- Parameters:
registry- theRandomizerRegistryto register- Returns:
- the current
EasyRandomParametersinstance 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
EasyRandomParametersinstance 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
EasyRandomParametersinstance for method chaining
-
overrideDefaultInitialization
public EasyRandomParameters overrideDefaultInitialization(boolean overrideDefaultInitialization)
Should default initialization of field values be overridden? E.g. should the values of thestringsandintegersfields below be kept untouched or should they be randomized.
Deactivated by default.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
EasyRandomParametersinstance 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
EasyRandomParametersinstance for method chaining
-
copy
public EasyRandomParameters copy()
Return a shallow copy of randomization parameters.- Returns:
- a shallow copy of randomization parameters.
-
-