@ThreadSafe public abstract class AbstractRandomDataProviderStrategy extends Object implements RandomDataProviderStrategy
DataProviderStrategy
This default implementation returns values based on a random generator. Convinient for subclassing and redefining behaviour. Don't use this implementation if you seek deterministic values
All values returned by this implementation are different from zero.
DataProviderStrategy.Order
Modifier and Type | Field and Description |
---|---|
private Map<Class<?>,Map<String,AttributeStrategy<?>>> |
attributeClassStrategies
Mapping between attributes and attribute strategies
|
private Map<Class<? extends Annotation>,AttributeStrategy<?>> |
attributeStrategies
Mapping between annotations and attribute strategies
|
private AbstractConstructorComparator |
constructorHeavyComparator
The constructor comparator
|
private AbstractConstructorComparator |
constructorLightComparator
The constructor comparator
|
private Map<Class<?>,Class<?>> |
factoryTypes
A list of user-submitted factories to build interfaces and abstract classes
|
private AtomicBoolean |
isMemoizationEnabled
Flag to enable/disable the memoization setting.
|
private static org.slf4j.Logger |
LOG
Application logger
|
private int |
maxDepth
How many times it is allowed to PODAM to create an instance of the same
class in a recursive hierarchy
|
private Map<Class<?>,Map<Type[],Object>> |
memoizationTable
A map to keep one object for each class.
|
private AbstractMethodComparator |
methodHeavyComparator
The constructor comparator
|
private AbstractMethodComparator |
methodLightComparator
The constructor comparator
|
private AtomicInteger |
nbrOfCollectionElements
The number of collection elements.
|
private Map<Class<?>,Class<?>> |
specificTypes
A list of user-submitted specific implementations for interfaces and
abstract classes
|
private ConcurrentHashMap<Class<?>,TypeManufacturer<?>> |
typeManufacturers
A mapping between types and their registered manufacturers
|
Constructor and Description |
---|
AbstractRandomDataProviderStrategy()
Implementation of the Singleton pattern
|
AbstractRandomDataProviderStrategy(int nbrOfCollectionElements) |
Modifier and Type | Method and Description |
---|---|
RandomDataProviderStrategy |
addOrReplaceAttributeStrategy(Class<?> type,
String attributeName,
AttributeStrategy<?> attributeStrategy)
Registers @AttributeStrategy implementation, which will be used to
instantiate objects of a specified type.
|
RandomDataProviderStrategy |
addOrReplaceAttributeStrategy(Class<? extends Annotation> annotationClass,
AttributeStrategy<?> attributeStrategy)
Bind an annotation to attribute strategy class.
|
<T> AbstractRandomDataProviderStrategy |
addOrReplaceFactory(Class<T> abstractClass,
Class<?> factoryClass)
Binds an interface/abstract class to its factory.
|
<T> DataProviderStrategy |
addOrReplaceSpecific(Class<T> abstractClass,
Class<? extends T> specificClass)
Bind an interface/abstract class to a specific implementation.
|
<T> DataProviderStrategy |
addOrReplaceTypeManufacturer(Class<? extends T> type,
TypeManufacturer<T> typeManufacturer)
Registers @TypeManufacturer implementation, which will be used to
instantiate objects of a specified type.
|
void |
cacheMemoizedObject(AttributeMetadata attributeMetadata,
Object instance)
Saves object in memoization cache
|
void |
clearMemoizationCache()
Clears memoization cache
|
AbstractConstructorComparator |
getConstructorHeavyComparator()
Getter for constructor heavy comparator.
|
AbstractConstructorComparator |
getConstructorLightComparator()
Getter for constructor light comparator.
|
Class<?> |
getFactoryClass(Class<?> nonInstantiatableClass)
Resolves factory classes for abstract classes and interfaces.
|
int |
getMaxDepth(Class<?> type)
Returns how many times it is allowed to PODAM to create an instance
of the same class in a recursive hierarchy
|
Object |
getMemoizedObject(AttributeMetadata attributeMetadata)
Obtains object from memoization cache
|
AbstractMethodComparator |
getMethodHeavyComparator()
Getter for method heavy comparator.
|
AbstractMethodComparator |
getMethodLightComparator()
Getter for method light comparator.
|
int |
getNumberOfCollectionElements(Class<?> type)
Returns the number of default collection elements.
|
<T> Class<? extends T> |
getSpecificClass(Class<T> nonInstantiatableClass)
Resolves abstract classes and interfaces.
|
AttributeStrategy<?> |
getStrategyForAnnotation(Class<? extends Annotation> annotationClass)
Finds attribute strategies for annotations.
|
AttributeStrategy<?> |
getStrategyForAttribute(ClassAttribute attribute)
Finds attribute strategies for attribute.
|
<T> T |
getTypeValue(AttributeMetadata attributeMetadata,
ManufacturingContext manufacturingCtx,
Class<T> pojoType)
Obtains a type value
|
boolean |
isMemoizationEnabled()
Returns whether the memoization setting is enabled.
|
private void |
log(AttributeMetadata attributeMetadata) |
RandomDataProviderStrategy |
removeAttributeStrategy(Class<?> type,
String attributeName)
Removes @AttributeStrategy implementation from for the specific attribute
|
RandomDataProviderStrategy |
removeAttributeStrategy(Class<? extends Annotation> annotationClass)
Remove binding of an annotation to attribute strategy
|
<T> AbstractRandomDataProviderStrategy |
removeFactory(Class<T> abstractClass)
Remove binding of an interface/abstract class to its factory
|
<T> DataProviderStrategy |
removeSpecific(Class<T> abstractClass)
Remove binding of an interface/abstract class to a specific
implementation
|
<T> DataProviderStrategy |
removeTypeManufacturer(Class<T> type)
Remove binding of a class type to a specific
implementation of type manufacturer
|
void |
setConstructorHeavyComparator(AbstractConstructorComparator constructorHeavyComparator)
Setter for constructor heavy comparator.
|
void |
setConstructorLightComparator(AbstractConstructorComparator constructorLightComparator)
Setter for constructor öight comparator.
|
void |
setDefaultNumberOfCollectionElements(int newNumberOfCollectionElements)
Sets the new default number of default collection elements.
|
void |
setMaxDepth(int maxDepth)
Max depth setter
|
void |
setMemoization(boolean isMemoizationEnabled)
Sets whether memoization is enabled
|
void |
setMethodHeavyComparator(AbstractMethodComparator methodHeavyComparator)
Setter for method heavy comparator.
|
void |
setMethodLightComparator(AbstractMethodComparator methodLightComparator)
Setter for method light comparator.
|
void |
sort(Constructor<?>[] constructors,
DataProviderStrategy.Order order)
Rearranges POJO's constructors in order they will be tried to produce the
POJO.
|
void |
sort(Method[] methods,
DataProviderStrategy.Order order)
Rearranges POJO's methods in order they will be tried to produce the
POJO.
|
private static final org.slf4j.Logger LOG
private int maxDepth
private final AtomicInteger nbrOfCollectionElements
private final AtomicBoolean isMemoizationEnabled
private final Map<Class<?>,Map<Type[],Object>> memoizationTable
private final ConcurrentHashMap<Class<?>,TypeManufacturer<?>> typeManufacturers
private final Map<Class<?>,Class<?>> specificTypes
private final Map<Class<?>,Class<?>> factoryTypes
private final Map<Class<? extends Annotation>,AttributeStrategy<?>> attributeStrategies
private final Map<Class<?>,Map<String,AttributeStrategy<?>>> attributeClassStrategies
private AbstractConstructorComparator constructorHeavyComparator
private AbstractConstructorComparator constructorLightComparator
private AbstractMethodComparator methodHeavyComparator
private AbstractMethodComparator methodLightComparator
public AbstractRandomDataProviderStrategy()
public AbstractRandomDataProviderStrategy(int nbrOfCollectionElements)
public int getNumberOfCollectionElements(Class<?> type)
Implementations of this interface need to provide this value.
getNumberOfCollectionElements
in interface DataProviderStrategy
type
- Type of collection's elementspublic void setDefaultNumberOfCollectionElements(int newNumberOfCollectionElements)
setDefaultNumberOfCollectionElements
in interface DataProviderStrategy
newNumberOfCollectionElements
- The new number of collection elements.public int getMaxDepth(Class<?> type)
Implementations of this interface need to provide this value.
getMaxDepth
in interface DataProviderStrategy
type
- Type of POJO to be createdpublic void setMaxDepth(int maxDepth)
maxDepth
- defines new max depthpublic boolean isMemoizationEnabled()
Implementations of this interface need to provide this value.
isMemoizationEnabled
in interface DataProviderStrategy
public void setMemoization(boolean isMemoizationEnabled)
setMemoization
in interface DataProviderStrategy
isMemoizationEnabled
- whether memoization is enabledpublic Object getMemoizedObject(AttributeMetadata attributeMetadata)
getMemoizedObject
in interface DataProviderStrategy
attributeMetadata
- attribute metadata for instance to be fetchedpublic void cacheMemoizedObject(AttributeMetadata attributeMetadata, Object instance)
cacheMemoizedObject
in interface DataProviderStrategy
attributeMetadata
- attribute metadata for instance being memoizedinstance
- object instance to save in memoization cachepublic void clearMemoizationCache()
clearMemoizationCache
in interface DataProviderStrategy
public void sort(Constructor<?>[] constructors, DataProviderStrategy.Order order)
sort
in interface DataProviderStrategy
constructors
- Array of POJO's constructorsorder
- DataProviderStrategy.Order
how to sort constructorspublic void sort(Method[] methods, DataProviderStrategy.Order order)
sort
in interface DataProviderStrategy
methods
- Array of POJO's methodsorder
- DataProviderStrategy.Order
how to sort constructorspublic <T> DataProviderStrategy addOrReplaceTypeManufacturer(Class<? extends T> type, TypeManufacturer<T> typeManufacturer)
addOrReplaceTypeManufacturer
in interface DataProviderStrategy
T
- type of objects to be produced by the manufacturertype
- the specific class type the specified manufacturer
will instantiate.typeManufacturer
- type manufacturer implementation to be registeredpublic <T> DataProviderStrategy removeTypeManufacturer(Class<T> type)
removeTypeManufacturer
in interface DataProviderStrategy
T
- type of objects to be produced by the manufacturertype
- the specific class type to remove bindingpublic <T> T getTypeValue(AttributeMetadata attributeMetadata, ManufacturingContext manufacturingCtx, Class<T> pojoType)
getTypeValue
in interface DataProviderStrategy
T
- type of returned objectattributeMetadata
- The AttributeMetadata informationmanufacturingCtx
- The pojo manufacturing context with generic attribute type argument typespojoType
- The class of the requested typepublic <T> AbstractRandomDataProviderStrategy addOrReplaceFactory(Class<T> abstractClass, Class<?> factoryClass)
addOrReplaceFactory
in interface RandomDataProviderStrategy
T
- return typeabstractClass
- the interface/abstract class to bindfactoryClass
- factory class for instantiation of
abstractClass
.public <T> AbstractRandomDataProviderStrategy removeFactory(Class<T> abstractClass)
removeFactory
in interface RandomDataProviderStrategy
T
- return typeabstractClass
- the interface/abstract class to remove bindingpublic Class<?> getFactoryClass(Class<?> nonInstantiatableClass)
Should return factory class type, which can be used to instantiate an abstract class type or interface.
getFactoryClass
in interface DataProviderStrategy
nonInstantiatableClass
- Abstract class type or interfacenonInstantiatableClass
.public <T> DataProviderStrategy addOrReplaceSpecific(Class<T> abstractClass, Class<? extends T> specificClass)
addOrReplaceSpecific
in interface DataProviderStrategy
T
- return typeabstractClass
- the interface/abstract class to bindspecificClass
- the specific class implementing or extending
abstractClass
.public <T> DataProviderStrategy removeSpecific(Class<T> abstractClass)
removeSpecific
in interface DataProviderStrategy
T
- return typeabstractClass
- the interface/abstract class to remove bindingpublic <T> Class<? extends T> getSpecificClass(Class<T> nonInstantiatableClass)
Should return specific class type, which can be instantiated and assigned to abstract class type or interface.
getSpecificClass
in interface DataProviderStrategy
T
- The type of class being resolvednonInstantiatableClass
- Abstract class type or interfacenonInstantiatableClass
.public RandomDataProviderStrategy addOrReplaceAttributeStrategy(Class<? extends Annotation> annotationClass, AttributeStrategy<?> attributeStrategy)
addOrReplaceAttributeStrategy
in interface RandomDataProviderStrategy
annotationClass
- the annotation classattributeStrategy
- the attribute strategypublic RandomDataProviderStrategy removeAttributeStrategy(Class<? extends Annotation> annotationClass)
removeAttributeStrategy
in interface RandomDataProviderStrategy
annotationClass
- the annotation class to remove bindingpublic AttributeStrategy<?> getStrategyForAnnotation(Class<? extends Annotation> annotationClass)
Searches for mapping between annotations and attribute strategies, which will be used then for populating fields or constructor parameters.
getStrategyForAnnotation
in interface DataProviderStrategy
annotationClass
- Annotation class to inspectpublic RandomDataProviderStrategy addOrReplaceAttributeStrategy(Class<?> type, String attributeName, AttributeStrategy<?> attributeStrategy)
addOrReplaceAttributeStrategy
in interface DataProviderStrategy
type
- the specific class type the specified manufacturer
will instantiate.attributeName
- attribute name to use attributeStrategy forattributeStrategy
- attribute strategy to be registeredpublic RandomDataProviderStrategy removeAttributeStrategy(Class<?> type, String attributeName)
removeAttributeStrategy
in interface DataProviderStrategy
type
- the specific class type the specified manufacturer
will instantiate.attributeName
- attribute name to use attributeStrategy forpublic AttributeStrategy<?> getStrategyForAttribute(ClassAttribute attribute)
Searches for mapping between class attribute and attribute strategies, which will be used then for populating fields or constructor parameters.
getStrategyForAttribute
in interface DataProviderStrategy
attribute
- attribute to be manufacturedpublic AbstractConstructorComparator getConstructorLightComparator()
getConstructorLightComparator
in interface RandomDataProviderStrategy
public void setConstructorLightComparator(AbstractConstructorComparator constructorLightComparator)
Default implementations are
ConstructorHeavyFirstComparator
and
ConstructorLightFirstComparator
.
setConstructorLightComparator
in interface RandomDataProviderStrategy
constructorLightComparator
- constructor comparator to setpublic AbstractConstructorComparator getConstructorHeavyComparator()
getConstructorHeavyComparator
in interface RandomDataProviderStrategy
public void setConstructorHeavyComparator(AbstractConstructorComparator constructorHeavyComparator)
Default implementations are
ConstructorHeavyFirstComparator
and
ConstructorLightFirstComparator
.
setConstructorHeavyComparator
in interface RandomDataProviderStrategy
constructorHeavyComparator
- constructor comparator to setpublic AbstractMethodComparator getMethodLightComparator()
getMethodLightComparator
in interface RandomDataProviderStrategy
public void setMethodLightComparator(AbstractMethodComparator methodLightComparator)
Default implementations is
MethodHeavyFirstComparator
.
setMethodLightComparator
in interface RandomDataProviderStrategy
methodLightComparator
- method comparator to setpublic AbstractMethodComparator getMethodHeavyComparator()
getMethodHeavyComparator
in interface RandomDataProviderStrategy
public void setMethodHeavyComparator(AbstractMethodComparator methodHeavyComparator)
Default implementations is
MethodHeavyFirstComparator
.
setMethodHeavyComparator
in interface RandomDataProviderStrategy
methodHeavyComparator
- method comparator to setprivate void log(AttributeMetadata attributeMetadata)
Copyright © 2023. All rights reserved.