Class AbstractRandomDataProviderStrategy
- java.lang.Object
-
- uk.co.jemos.podam.api.AbstractRandomDataProviderStrategy
-
- All Implemented Interfaces:
DataProviderStrategy,RandomDataProviderStrategy
- Direct Known Subclasses:
RandomDataProviderStrategyImpl
@ThreadSafe public abstract class AbstractRandomDataProviderStrategy extends Object implements RandomDataProviderStrategy
Default abstract implementation of aDataProviderStrategyThis 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.
- Since:
- 1.0.0
- Author:
- mtedone
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface uk.co.jemos.podam.api.DataProviderStrategy
DataProviderStrategy.Order
-
-
Field Summary
Fields Modifier and Type Field Description private Map<Class<?>,Map<String,AttributeStrategy<?>>>attributeClassStrategiesMapping between attributes and attribute strategiesprivate Map<Class<? extends Annotation>,AttributeStrategy<?>>attributeStrategiesMapping between annotations and attribute strategiesprivate AbstractConstructorComparatorconstructorHeavyComparatorThe constructor comparatorprivate AbstractConstructorComparatorconstructorLightComparatorThe constructor comparatorprivate Map<Class<?>,Class<?>>factoryTypesA list of user-submitted factories to build interfaces and abstract classesprivate AtomicBooleanisMemoizationEnabledFlag to enable/disable the memoization setting.private static org.slf4j.LoggerLOGApplication loggerprivate intmaxDepthHow many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchyprivate Map<Class<?>,Map<Type[],Object>>memoizationTableA map to keep one object for each class.private AbstractMethodComparatormethodHeavyComparatorThe constructor comparatorprivate AbstractMethodComparatormethodLightComparatorThe constructor comparatorprivate AtomicIntegernbrOfCollectionElementsThe number of collection elements.private Map<Class<?>,Class<?>>specificTypesA list of user-submitted specific implementations for interfaces and abstract classesprivate ConcurrentHashMap<Class<?>,TypeManufacturer<?>>typeManufacturersA mapping between types and their registered manufacturers
-
Constructor Summary
Constructors Constructor Description AbstractRandomDataProviderStrategy()Implementation of the Singleton patternAbstractRandomDataProviderStrategy(int nbrOfCollectionElements)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RandomDataProviderStrategyaddOrReplaceAttributeStrategy(Class<?> type, String attributeName, AttributeStrategy<?> attributeStrategy)Registers @AttributeStrategy implementation, which will be used to instantiate objects of a specified type.RandomDataProviderStrategyaddOrReplaceAttributeStrategy(Class<? extends Annotation> annotationClass, AttributeStrategy<?> attributeStrategy)Bind an annotation to attribute strategy class.<T> AbstractRandomDataProviderStrategyaddOrReplaceFactory(Class<T> abstractClass, Class<?> factoryClass)Binds an interface/abstract class to its factory.<T> DataProviderStrategyaddOrReplaceSpecific(Class<T> abstractClass, Class<? extends T> specificClass)Bind an interface/abstract class to a specific implementation.<T> DataProviderStrategyaddOrReplaceTypeManufacturer(Class<? extends T> type, TypeManufacturer<T> typeManufacturer)Registers @TypeManufacturer implementation, which will be used to instantiate objects of a specified type.voidcacheMemoizedObject(AttributeMetadata attributeMetadata, Object instance)Saves object in memoization cachevoidclearMemoizationCache()Clears memoization cacheAbstractConstructorComparatorgetConstructorHeavyComparator()Getter for constructor heavy comparator.AbstractConstructorComparatorgetConstructorLightComparator()Getter for constructor light comparator.Class<?>getFactoryClass(Class<?> nonInstantiatableClass)Resolves factory classes for abstract classes and interfaces.intgetMaxDepth(Class<?> type)Returns how many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchyObjectgetMemoizedObject(AttributeMetadata attributeMetadata)Obtains object from memoization cacheAbstractMethodComparatorgetMethodHeavyComparator()Getter for method heavy comparator.AbstractMethodComparatorgetMethodLightComparator()Getter for method light comparator.intgetNumberOfCollectionElements(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> TgetTypeValue(AttributeMetadata attributeMetadata, Map<String,Type> genericTypesArgumentsMap, Class<T> pojoType)Obtains a type valuebooleanisMemoizationEnabled()Returns whether the memoization setting is enabled.private voidlog(AttributeMetadata attributeMetadata)RandomDataProviderStrategyremoveAttributeStrategy(Class<?> type, String attributeName)Removes @AttributeStrategy implementation from for the specific attributeRandomDataProviderStrategyremoveAttributeStrategy(Class<? extends Annotation> annotationClass)Remove binding of an annotation to attribute strategy<T> AbstractRandomDataProviderStrategyremoveFactory(Class<T> abstractClass)Remove binding of an interface/abstract class to its factory<T> DataProviderStrategyremoveSpecific(Class<T> abstractClass)Remove binding of an interface/abstract class to a specific implementation<T> DataProviderStrategyremoveTypeManufacturer(Class<T> type)Remove binding of a class type to a specific implementation of type manufacturervoidsetConstructorHeavyComparator(AbstractConstructorComparator constructorHeavyComparator)Setter for constructor heavy comparator.voidsetConstructorLightComparator(AbstractConstructorComparator constructorLightComparator)Setter for constructor öight comparator.voidsetDefaultNumberOfCollectionElements(int newNumberOfCollectionElements)Sets the new default number of default collection elements.voidsetMaxDepth(int maxDepth)Max depth settervoidsetMemoization(boolean isMemoizationEnabled)Sets whether memoization is enabledvoidsetMethodHeavyComparator(AbstractMethodComparator methodHeavyComparator)Setter for method heavy comparator.voidsetMethodLightComparator(AbstractMethodComparator methodLightComparator)Setter for method light comparator.voidsort(Constructor<?>[] constructors, DataProviderStrategy.Order order)Rearranges POJO's constructors in order they will be tried to produce the POJO.voidsort(Method[] methods, DataProviderStrategy.Order order)Rearranges POJO's methods in order they will be tried to produce the POJO.
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
Application logger
-
maxDepth
private int maxDepth
How many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchy
-
nbrOfCollectionElements
private final AtomicInteger nbrOfCollectionElements
The number of collection elements.
-
isMemoizationEnabled
private final AtomicBoolean isMemoizationEnabled
Flag to enable/disable the memoization setting.
-
memoizationTable
private final Map<Class<?>,Map<Type[],Object>> memoizationTable
A map to keep one object for each class. If memoization is enabled, the factory will use this table to avoid creating objects of the same class multiple times.
-
typeManufacturers
private final ConcurrentHashMap<Class<?>,TypeManufacturer<?>> typeManufacturers
A mapping between types and their registered manufacturers
-
specificTypes
private final Map<Class<?>,Class<?>> specificTypes
A list of user-submitted specific implementations for interfaces and abstract classes
-
factoryTypes
private final Map<Class<?>,Class<?>> factoryTypes
A list of user-submitted factories to build interfaces and abstract classes
-
attributeStrategies
private final Map<Class<? extends Annotation>,AttributeStrategy<?>> attributeStrategies
Mapping between annotations and attribute strategies
-
attributeClassStrategies
private final Map<Class<?>,Map<String,AttributeStrategy<?>>> attributeClassStrategies
Mapping between attributes and attribute strategies
-
constructorHeavyComparator
private AbstractConstructorComparator constructorHeavyComparator
The constructor comparator
-
constructorLightComparator
private AbstractConstructorComparator constructorLightComparator
The constructor comparator
-
methodHeavyComparator
private AbstractMethodComparator methodHeavyComparator
The constructor comparator
-
methodLightComparator
private AbstractMethodComparator methodLightComparator
The constructor comparator
-
-
Method Detail
-
getNumberOfCollectionElements
public int getNumberOfCollectionElements(Class<?> type)
Returns the number of default collection elements.Implementations of this interface need to provide this value.
- Specified by:
getNumberOfCollectionElementsin interfaceDataProviderStrategy- Parameters:
type- Type of collection's elements- Returns:
- The number of default collection elements
-
setDefaultNumberOfCollectionElements
public void setDefaultNumberOfCollectionElements(int newNumberOfCollectionElements)
Sets the new default number of default collection elements.- Specified by:
setDefaultNumberOfCollectionElementsin interfaceDataProviderStrategy- Parameters:
newNumberOfCollectionElements- The new number of collection elements.
-
getMaxDepth
public int getMaxDepth(Class<?> type)
Returns how many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchyImplementations of this interface need to provide this value.
- Specified by:
getMaxDepthin interfaceDataProviderStrategy- Parameters:
type- Type of POJO to be created- Returns:
- How many times it is allowed to create an instance of the same class in a recursive hierarchy
-
setMaxDepth
public void setMaxDepth(int maxDepth)
Max depth setter- Parameters:
maxDepth- defines new max depth
-
isMemoizationEnabled
public boolean isMemoizationEnabled()
Returns whether the memoization setting is enabled. When memoization is enabled, only one object will be created for each type. Every next property of the same type will be a reference to the same object. This can dramatically improve performance but with the expense of not having objects with different values.Implementations of this interface need to provide this value.
- Specified by:
isMemoizationEnabledin interfaceDataProviderStrategy- Returns:
- Whether the memoization setting is enabled.
-
setMemoization
public void setMemoization(boolean isMemoizationEnabled)
Sets whether memoization is enabled- Specified by:
setMemoizationin interfaceDataProviderStrategy- Parameters:
isMemoizationEnabled- whether memoization is enabled
-
getMemoizedObject
public Object getMemoizedObject(AttributeMetadata attributeMetadata)
Obtains object from memoization cache- Specified by:
getMemoizedObjectin interfaceDataProviderStrategy- Parameters:
attributeMetadata- attribute metadata for instance to be fetched- Returns:
- object instance from memoization cache
-
cacheMemoizedObject
public void cacheMemoizedObject(AttributeMetadata attributeMetadata, Object instance)
Saves object in memoization cache- Specified by:
cacheMemoizedObjectin interfaceDataProviderStrategy- Parameters:
attributeMetadata- attribute metadata for instance being memoizedinstance- object instance to save in memoization cache
-
clearMemoizationCache
public void clearMemoizationCache()
Clears memoization cache- Specified by:
clearMemoizationCachein interfaceDataProviderStrategy
-
sort
public void sort(Constructor<?>[] constructors, DataProviderStrategy.Order order)
Rearranges POJO's constructors in order they will be tried to produce the POJO. Default strategy consist of putting constructors with less parameters to be tried first.- Specified by:
sortin interfaceDataProviderStrategy- Parameters:
constructors- Array of POJO's constructorsorder-DataProviderStrategy.Orderhow to sort constructors
-
sort
public void sort(Method[] methods, DataProviderStrategy.Order order)
Rearranges POJO's methods in order they will be tried to produce the POJO. Default strategy consist of putting methods with more parameters to be tried first.- Specified by:
sortin interfaceDataProviderStrategy- Parameters:
methods- Array of POJO's methodsorder-DataProviderStrategy.Orderhow to sort constructors
-
addOrReplaceTypeManufacturer
public <T> DataProviderStrategy addOrReplaceTypeManufacturer(Class<? extends T> type, TypeManufacturer<T> typeManufacturer)
Registers @TypeManufacturer implementation, which will be used to instantiate objects of a specified type. Use this to alter factory behaviour.- Specified by:
addOrReplaceTypeManufacturerin interfaceDataProviderStrategy- Type Parameters:
T- type of objects to be produced by the manufacturer- Parameters:
type- the specific class type the specified manufacturer will instantiate.typeManufacturer- type manufacturer implementation to be registered- Returns:
- itself
-
removeTypeManufacturer
public <T> DataProviderStrategy removeTypeManufacturer(Class<T> type)
Remove binding of a class type to a specific implementation of type manufacturer- Specified by:
removeTypeManufacturerin interfaceDataProviderStrategy- Type Parameters:
T- type of objects to be produced by the manufacturer- Parameters:
type- the specific class type to remove binding- Returns:
- itself
-
getTypeValue
public <T> T getTypeValue(AttributeMetadata attributeMetadata, Map<String,Type> genericTypesArgumentsMap, Class<T> pojoType)
Obtains a type value- Specified by:
getTypeValuein interfaceDataProviderStrategy- Type Parameters:
T- type of returned object- Parameters:
attributeMetadata- The AttributeMetadata informationgenericTypesArgumentsMap- The generic attribute type argument typespojoType- The class of the requested type- Returns:
- The type value
-
addOrReplaceFactory
public <T> AbstractRandomDataProviderStrategy addOrReplaceFactory(Class<T> abstractClass, Class<?> factoryClass)
Binds an interface/abstract class to its factory. If the strategy previously contained a binding for the interface/abstract class, the old value is replaced by the new value. If you want to implement more sophisticated binding strategy, override this class.- Specified by:
addOrReplaceFactoryin interfaceRandomDataProviderStrategy- Type Parameters:
T- return type- Parameters:
abstractClass- the interface/abstract class to bindfactoryClass- factory class for instantiation ofabstractClass.- Returns:
- itself
-
removeFactory
public <T> AbstractRandomDataProviderStrategy removeFactory(Class<T> abstractClass)
Remove binding of an interface/abstract class to its factory- Specified by:
removeFactoryin interfaceRandomDataProviderStrategy- Type Parameters:
T- return type- Parameters:
abstractClass- the interface/abstract class to remove binding- Returns:
- itself
-
getFactoryClass
public Class<?> getFactoryClass(Class<?> nonInstantiatableClass)
Resolves factory classes for abstract classes and interfaces.Should return factory class type, which can be used to instantiate an abstract class type or interface.
- Specified by:
getFactoryClassin interfaceDataProviderStrategy- Parameters:
nonInstantiatableClass- Abstract class type or interface- Returns:
- Non-abstract factory class type to instantiate
nonInstantiatableClass.
-
addOrReplaceSpecific
public <T> DataProviderStrategy addOrReplaceSpecific(Class<T> abstractClass, Class<? extends T> specificClass)
Bind an interface/abstract class to a specific implementation. If the strategy previously contained a binding for the interface/abstract class, the old value will not be replaced by the new value. If you want to force the value replacement, invoke removeSpecific before invoking this method. If you want to implement more sophisticated binding strategy, override this class.- Specified by:
addOrReplaceSpecificin interfaceDataProviderStrategy- Type Parameters:
T- return type- Parameters:
abstractClass- the interface/abstract class to bindspecificClass- the specific class implementing or extendingabstractClass.- Returns:
- itself
-
removeSpecific
public <T> DataProviderStrategy removeSpecific(Class<T> abstractClass)
Remove binding of an interface/abstract class to a specific implementation- Specified by:
removeSpecificin interfaceDataProviderStrategy- Type Parameters:
T- return type- Parameters:
abstractClass- the interface/abstract class to remove binding- Returns:
- itself
-
getSpecificClass
public <T> Class<? extends T> getSpecificClass(Class<T> nonInstantiatableClass)
Resolves abstract classes and interfaces.Should return specific class type, which can be instantiated and assigned to abstract class type or interface.
- Specified by:
getSpecificClassin interfaceDataProviderStrategy- Type Parameters:
T- The type of class being resolved- Parameters:
nonInstantiatableClass- Abstract class type or interface- Returns:
- Non-abstract class type derived from
nonInstantiatableClass.
-
addOrReplaceAttributeStrategy
public RandomDataProviderStrategy addOrReplaceAttributeStrategy(Class<? extends Annotation> annotationClass, AttributeStrategy<?> attributeStrategy)
Bind an annotation to attribute strategy class. If the strategy previously contained a binding for the annotation, the old will be replaced. If you want to implement more sophisticated binding strategy, override this class.- Specified by:
addOrReplaceAttributeStrategyin interfaceRandomDataProviderStrategy- Parameters:
annotationClass- the annotation classattributeStrategy- the attribute strategy- Returns:
- itself
-
removeAttributeStrategy
public RandomDataProviderStrategy removeAttributeStrategy(Class<? extends Annotation> annotationClass)
Remove binding of an annotation to attribute strategy- Specified by:
removeAttributeStrategyin interfaceRandomDataProviderStrategy- Parameters:
annotationClass- the annotation class to remove binding- Returns:
- itself
-
getStrategyForAnnotation
public AttributeStrategy<?> getStrategyForAnnotation(Class<? extends Annotation> annotationClass)
Finds attribute strategies for annotations.Searches for mapping between annotations and attribute strategies, which will be used then for populating fields or constructor parameters.
- Specified by:
getStrategyForAnnotationin interfaceDataProviderStrategy- Parameters:
annotationClass- Annotation class to inspect- Returns:
- attribute strategy associated with given annotation
-
addOrReplaceAttributeStrategy
public RandomDataProviderStrategy addOrReplaceAttributeStrategy(Class<?> type, String attributeName, AttributeStrategy<?> attributeStrategy)
Registers @AttributeStrategy implementation, which will be used to instantiate objects of a specified type. Use this to alter factory behaviour.- Specified by:
addOrReplaceAttributeStrategyin interfaceDataProviderStrategy- Parameters:
type- the specific class type the specified manufacturer will instantiate.attributeName- attribute name to use attributeStrategy forattributeStrategy- attribute strategy to be registered- Returns:
- itself
-
removeAttributeStrategy
public RandomDataProviderStrategy removeAttributeStrategy(Class<?> type, String attributeName)
Removes @AttributeStrategy implementation from for the specific attribute- Specified by:
removeAttributeStrategyin interfaceDataProviderStrategy- Parameters:
type- the specific class type the specified manufacturer will instantiate.attributeName- attribute name to use attributeStrategy for- Returns:
- itself
-
getStrategyForAttribute
public AttributeStrategy<?> getStrategyForAttribute(ClassAttribute attribute)
Finds attribute strategies for attribute.Searches for mapping between class attribute and attribute strategies, which will be used then for populating fields or constructor parameters.
- Specified by:
getStrategyForAttributein interfaceDataProviderStrategy- Parameters:
attribute- attribute to be manufactured- Returns:
- attribute strategy associated with given attribute
-
getConstructorLightComparator
public AbstractConstructorComparator getConstructorLightComparator()
Getter for constructor light comparator.- Specified by:
getConstructorLightComparatorin interfaceRandomDataProviderStrategy- Returns:
- current constructor comparator used by strategy
-
setConstructorLightComparator
public void setConstructorLightComparator(AbstractConstructorComparator constructorLightComparator)
Setter for constructor öight comparator.Default implementations are
ConstructorHeavyFirstComparatorandConstructorLightFirstComparator.- Specified by:
setConstructorLightComparatorin interfaceRandomDataProviderStrategy- Parameters:
constructorLightComparator- constructor comparator to set
-
getConstructorHeavyComparator
public AbstractConstructorComparator getConstructorHeavyComparator()
Getter for constructor heavy comparator.- Specified by:
getConstructorHeavyComparatorin interfaceRandomDataProviderStrategy- Returns:
- current constructor comparator used by strategy
-
setConstructorHeavyComparator
public void setConstructorHeavyComparator(AbstractConstructorComparator constructorHeavyComparator)
Setter for constructor heavy comparator.Default implementations are
ConstructorHeavyFirstComparatorandConstructorLightFirstComparator.- Specified by:
setConstructorHeavyComparatorin interfaceRandomDataProviderStrategy- Parameters:
constructorHeavyComparator- constructor comparator to set
-
getMethodLightComparator
public AbstractMethodComparator getMethodLightComparator()
Getter for method light comparator.- Specified by:
getMethodLightComparatorin interfaceRandomDataProviderStrategy- Returns:
- current method comparator used by strategy
-
setMethodLightComparator
public void setMethodLightComparator(AbstractMethodComparator methodLightComparator)
Setter for method light comparator.Default implementations is
MethodHeavyFirstComparator.- Specified by:
setMethodLightComparatorin interfaceRandomDataProviderStrategy- Parameters:
methodLightComparator- method comparator to set
-
getMethodHeavyComparator
public AbstractMethodComparator getMethodHeavyComparator()
Getter for method heavy comparator.- Specified by:
getMethodHeavyComparatorin interfaceRandomDataProviderStrategy- Returns:
- current method comparator used by strategy
-
setMethodHeavyComparator
public void setMethodHeavyComparator(AbstractMethodComparator methodHeavyComparator)
Setter for method heavy comparator.Default implementations is
MethodHeavyFirstComparator.- Specified by:
setMethodHeavyComparatorin interfaceRandomDataProviderStrategy- Parameters:
methodHeavyComparator- method comparator to set
-
log
private void log(AttributeMetadata attributeMetadata)
-
-