Class TypeTransformerRegistry
java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_base.typetransformer.TypeTransformerRegistry
The TypeTransformerRegistry is a registry for TypeTransformer which can transform an objetc of one type/class to another.
There exists many implementations available but none of them fits ours needs.
Nevertheless we list them here:
- org.apache.commons.beanutils.ConvertUtilsBean (only target type and not source type) - from documentation: converting String scalar values to objects of the specified Class
- org.apache.commons.collections4.Transformer - converts elements in lists
- com.google.common.base.Converter - used for converting back and forth between different representations of the same information
- transmorph
- morph
- apache commons convert - exactly what we need but only in sandbox and not properties
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final boolean
If true, then the tranformation can be performed with multiple steps (TypeTransformer
).private static final int
The additional tranformation cost (determined by the environment variable MELT_TRANSFORMATION_HIERARCHY_COST )to add if hierarchy is allowed e.g.private static final org.slf4j.Logger
Default logger.Cache for Superclasses and interfaces for a given class.The map which contains all transformers. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
static void
addAllTransformersViaServiceRegistry
(ClassLoader classloader) static void
This can be called in case the service registry does not work (in case of SEALS - due to the classloader).static void
addTransformer
(TypeTransformer<?, ?> transformer) private static String
classRepresentation
(Set<Object> objects) static void
clear()
Removes all transformers.static Set<TypeTransformer<?,
?>> Get all registered tranformers.static String
getAllSuperClassesAndIterfaces
(Class<?> clazz) Given a class return all superclasses and interfaces except the Object class (which woul be too generic).getAllSuperClassesAndIterfacesWithCost
(Class<?> clazz, int hierarchyCost) Given a class return all superclasses and interfaces except the Object class (which would be too generic).static <T> TransformationRoute<T>
getClassTransformationRoute
(Class<?> source, Class<T> target) Return the transformation from a given class to a target class.static <T> TransformationRoute<T>
getClassTransformationRoute
(Class<?> source, Class<T> target, Properties parameters) static <T> TransformationRoute<T>
getClassTransformationRoute
(Class<?> source, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) static <T> TransformationRoute<T>
getClassTransformationRouteMultipleRepresentations
(Iterable<Class<?>> sources, Class<T> target) static <T> TransformationRoute<T>
getClassTransformationRouteMultipleRepresentations
(Iterable<Class<?>> sources, Class<T> target, Properties parameters) static <T> TransformationRoute<T>
getClassTransformationRouteMultipleRepresentations
(Iterable<Class<?>> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) Returns type transformation route for one of the source classes to the target class.If no transformation is available, then a TypeTransformationException is thrown.private static int
private static Class<?>
static <T> T
getNewInstance
(Class<T> clazz) static <T> ObjectTransformationRoute<T>
getObjectTransformationRoute
(Object source, Class<T> target) static <T> ObjectTransformationRoute<T>
getObjectTransformationRoute
(Object source, Class<T> target, Properties parameters) static <T> ObjectTransformationRoute<T>
getObjectTransformationRoute
(Object source, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) static <T> ObjectTransformationRoute<T>
getObjectTransformationRouteMultipleRepresentations
(Iterable<Object> sources, Class<T> target) static <T> ObjectTransformationRoute<T>
getObjectTransformationRouteMultipleRepresentations
(Iterable<Object> sources, Class<T> target, Properties parameters) static <T> ObjectTransformationRoute<T>
getObjectTransformationRouteMultipleRepresentations
(Iterable<Object> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) Transforms an object with multiple representations to one class.This means, you have multiple objects representing the same information and want to choose the right one with the lowest transformation cost.static <T> List<T>
getTransformedListOfObjectsMultipleRepresentations
(List<Set<Object>> sourceObjects, Class<T> targetType) Directly get the transformed object or null if something went wrong.No transformation properties are provided.static <T> List<T>
getTransformedListOfObjectsMultipleRepresentations
(List<Set<Object>> sourceObjects, Class<T> targetType, Object transformationProperties) Directly get the transformed object or null if something went wrong.static <T> List<T>
getTransformedListOfObjectsMultipleRepresentations
(List<Set<Object>> sourceObjects, Class<T> targetType, Properties transformationProperties) Directly get the transformed list of objects or null if something went wrong.static <T> T
getTransformedObject
(Object sourceObject, Class<? extends T> targetType) Directly get the transformed object or null if something went wrong.static <T> T
getTransformedObject
(Object sourceObject, Class<? extends T> targetType, Object transformationProperties) Directly get the transformed object or null if something went wrong.static <T> T
getTransformedObject
(Object sourceObject, Class<T> targetType, Properties transformationProperties) Directly get the transformed object or null if something went wrong.static <T> T
getTransformedObjectMultipleRepresentations
(Set<Object> sourceObjects, Class<T> targetType) Directly get the transformed object or null if something went wrong.No transformation parameters are provided.static <T> T
getTransformedObjectMultipleRepresentations
(Set<Object> sourceObjects, Class<T> targetType, Object transformationProperties) Directly get the transformed object or null if something went wrong.static <T> T
getTransformedObjectMultipleRepresentations
(Set<Object> sourceObjects, Class<T> targetType, Properties transformationProperties) Directly get the transformed object or null if something went wrong.static <T> T
getTransformedObjectOrNewInstance
(Object sourceObject, Class<? extends T> targetType) Directly get the transformed object or a new instance something went wrong.static <T> T
getTransformedObjectOrNewInstance
(Object sourceObject, Class<? extends T> targetType, Object transformationProperties) Directly get the transformed object or a new instance something went wrong.static <T> T
getTransformedObjectOrNewInstance
(Object sourceObject, Class<T> targetType, Properties transformationProperties) Directly get the transformed object or new instance if something went wrong.static Properties
getTransformedProperties
(Object parameters) Transforms a given object to java.lang:Properties or throws an exception if something went wrong.If parameter is null, then null is returned.static Properties
getTransformedPropertiesOrNewInstance
(Object parameters) Transforms a given object to java.lang:Properties or throws an exception if something went wrong.static void
removeTransformer
(TypeTransformer<?, ?> transformer) private static <T> TransformationRoute<T>
transformInOneStep
(Iterable<Class<?>> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost)
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGERDefault logger. -
TRANFORMERS
The map which contains all transformers. -
HIERARCHY_TRANSFORMATION_COST
private static final int HIERARCHY_TRANSFORMATION_COSTThe additional tranformation cost (determined by the environment variable MELT_TRANSFORMATION_HIERARCHY_COST )to add if hierarchy is allowed e.g. when we have aTypeTransformer
between A (source) and B(target) and we allow hierarchy then it is also possible to transform between any subclass of A and any superclass of B. The HIERARCHY_TRANSFORMATION_COST is added to the initial transformaion cost with each layer in the class hierarchy. If the number is smaller than zero (e.g. -1), then the hierarchy is disabled. Default: hierarchy enabled with 30 as cost. -
ALLOW_MULTI_STEP
private static final boolean ALLOW_MULTI_STEPIf true, then the tranformation can be performed with multiple steps (TypeTransformer
). If false, only oneTypeTransformer
is allowed to be used for a transformation. It is set through the environment variable MELT_TRANSFORMATION_ALLOW_MULTI_STEP . The default is true. If too many transformers are used, then settings this property to false might help. -
MELT_DEFAULT_TRANSFORMERS
-
SUPER_CLASSES_CACHE
Cache for Superclasses and interfaces for a given class. Since there are ussually not so many classes, this information can directly be cache without much memory consumption.
-
-
Constructor Details
-
TypeTransformerRegistry
public TypeTransformerRegistry()
-
-
Method Details
-
getHierarchyTransformationCost
private static int getHierarchyTransformationCost() -
addTransformer
-
addAllTransformersViaServiceRegistry
public static void addAllTransformersViaServiceRegistry() -
addAllTransformersViaServiceRegistry
-
addMeltDefaultTransformers
public static void addMeltDefaultTransformers()This can be called in case the service registry does not work (in case of SEALS - due to the classloader). It will add a static set of default transformers available in melt if the corresponding class is available in the classpath. -
removeTransformer
-
clear
public static void clear()Removes all transformers. Use it with care. -
getAllRegisteredTypeTransformers
Get all registered tranformers. For debugging purposes.- Returns:
- all registered transformers.
-
getAllRegisteredTypeTransformersAsString
-
getObjectTransformationRoute
public static <T> ObjectTransformationRoute<T> getObjectTransformationRoute(Object source, Class<T> target) throws TypeTransformationException - Throws:
TypeTransformationException
-
getObjectTransformationRoute
public static <T> ObjectTransformationRoute<T> getObjectTransformationRoute(Object source, Class<T> target, Properties parameters) throws TypeTransformationException - Throws:
TypeTransformationException
-
getObjectTransformationRoute
public static <T> ObjectTransformationRoute<T> getObjectTransformationRoute(Object source, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) throws TypeTransformationException - Throws:
TypeTransformationException
-
getObjectTransformationRouteMultipleRepresentations
public static <T> ObjectTransformationRoute<T> getObjectTransformationRouteMultipleRepresentations(Iterable<Object> sources, Class<T> target) throws TypeTransformationException - Throws:
TypeTransformationException
-
getObjectTransformationRouteMultipleRepresentations
public static <T> ObjectTransformationRoute<T> getObjectTransformationRouteMultipleRepresentations(Iterable<Object> sources, Class<T> target, Properties parameters) throws TypeTransformationException - Throws:
TypeTransformationException
-
getObjectTransformationRouteMultipleRepresentations
public static <T> ObjectTransformationRoute<T> getObjectTransformationRouteMultipleRepresentations(Iterable<Object> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) throws TypeTransformationException Transforms an object with multiple representations to one class.This means, you have multiple objects representing the same information and want to choose the right one with the lowest transformation cost.- Type Parameters:
T
- the target type- Parameters:
sources
- the possible objects which all contains the same information (but in different classes).target
- the target class in which the object should be converted.parameters
- optional parameters for the conversion.hierarchyTransformationCost
- hierarchy transformation cost: seeHIERARCHY_TRANSFORMATION_COST
allowMultiStep
- allow multi step: seeALLOW_MULTI_STEP
- Returns:
- ObjectTransformationRoute which contains the transformers as well as the source object. The actual transformation is not yet executed.
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getClassTransformationRoute
public static <T> TransformationRoute<T> getClassTransformationRoute(Class<?> source, Class<T> target) throws TypeTransformationException Return the transformation from a given class to a target class.- Type Parameters:
T
- the target type- Parameters:
source
- the source classtarget
- the target class- Returns:
- the transformation route
- Throws:
TypeTransformationException
- if no route is found
-
getClassTransformationRoute
public static <T> TransformationRoute<T> getClassTransformationRoute(Class<?> source, Class<T> target, Properties parameters) throws TypeTransformationException - Throws:
TypeTransformationException
-
getClassTransformationRoute
public static <T> TransformationRoute<T> getClassTransformationRoute(Class<?> source, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) throws TypeTransformationException - Throws:
TypeTransformationException
-
getClassTransformationRouteMultipleRepresentations
public static <T> TransformationRoute<T> getClassTransformationRouteMultipleRepresentations(Iterable<Class<?>> sources, Class<T> target) throws TypeTransformationException - Throws:
TypeTransformationException
-
getClassTransformationRouteMultipleRepresentations
public static <T> TransformationRoute<T> getClassTransformationRouteMultipleRepresentations(Iterable<Class<?>> sources, Class<T> target, Properties parameters) throws TypeTransformationException - Throws:
TypeTransformationException
-
getClassTransformationRouteMultipleRepresentations
public static <T> TransformationRoute<T> getClassTransformationRouteMultipleRepresentations(Iterable<Class<?>> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost, boolean allowMultiStep) throws TypeTransformationException Returns type transformation route for one of the source classes to the target class.If no transformation is available, then a TypeTransformationException is thrown. If no classes are gioven, then null is returned.- Type Parameters:
T
- the target type- Parameters:
sources
- the iterable of source classestarget
- the target classparameters
- the parameters which are forwarded to each type transformerhierarchyTransformationCost
- negative value (below zero) if the hierarchy is not allowed, otherwise the cost for each subclass to superclass relation - see alsoHIERARCHY_TRANSFORMATION_COST
allowMultiStep
- allow multiple type transformers - see alsoALLOW_MULTI_STEP
- Returns:
- null if there is no path, otherwise instance of TransformationRoute (which can contain no transformers, when the source is a subclass of target class)
- Throws:
TypeTransformationException
- if no route is found
-
getMinimum
-
transformInOneStep
private static <T> TransformationRoute<T> transformInOneStep(Iterable<Class<?>> sources, Class<T> target, Properties parameters, int hierarchyTransformationCost) throws TypeTransformationException - Throws:
TypeTransformationException
-
getTransformedListOfObjectsMultipleRepresentations
public static <T> List<T> getTransformedListOfObjectsMultipleRepresentations(List<Set<Object>> sourceObjects, Class<T> targetType, Properties transformationProperties) throws TypeTransformationException Directly get the transformed list of objects or null if something went wrong.- Type Parameters:
T
- the type- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed list of objects will be added.targetType
- the tyoe of class to transform each object in the list totransformationProperties
- additional properties.- Returns:
- the transformed list of objects or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedListOfObjectsMultipleRepresentations
public static <T> List<T> getTransformedListOfObjectsMultipleRepresentations(List<Set<Object>> sourceObjects, Class<T> targetType, Object transformationProperties) throws TypeTransformationException Directly get the transformed object or null if something went wrong.- Type Parameters:
T
- the type- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed object will be added.targetType
- the tyoe of class to trasnform totransformationProperties
- additional properties.- Returns:
- the transformed object or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedListOfObjectsMultipleRepresentations
public static <T> List<T> getTransformedListOfObjectsMultipleRepresentations(List<Set<Object>> sourceObjects, Class<T> targetType) throws TypeTransformationException Directly get the transformed object or null if something went wrong.No transformation properties are provided.- Type Parameters:
T
- the type- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed object will be added.targetType
- the tyoe of class to trasnform to- Returns:
- the transformed object or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedObjectMultipleRepresentations
public static <T> T getTransformedObjectMultipleRepresentations(Set<Object> sourceObjects, Class<T> targetType, Properties transformationProperties) throws TypeTransformationException Directly get the transformed object or null if something went wrong.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed object will be added.targetType
- the tyoe of class to trasnform totransformationProperties
- additional properties.- Returns:
- the transformed object or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedObjectMultipleRepresentations
public static <T> T getTransformedObjectMultipleRepresentations(Set<Object> sourceObjects, Class<T> targetType, Object transformationProperties) throws TypeTransformationException Directly get the transformed object or null if something went wrong.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed object will be added.targetType
- the tyoe of class to trasnform totransformationProperties
- additional properties.- Returns:
- the transformed object or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedObjectMultipleRepresentations
public static <T> T getTransformedObjectMultipleRepresentations(Set<Object> sourceObjects, Class<T> targetType) throws TypeTransformationException Directly get the transformed object or null if something went wrong.No transformation parameters are provided.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObjects
- the objects which all represent the same information. To this set, the transformed object will be added.targetType
- the tyoe of class to trasnform to- Returns:
- the transformed object or null
- Throws:
TypeTransformationException
- in case no transformation route is found
-
getTransformedObject
public static <T> T getTransformedObject(Object sourceObject, Class<T> targetType, Properties transformationProperties) throws TypeTransformationException Directly get the transformed object or null if something went wrong.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the tyoe of class to trasnform totransformationProperties
- additional properties which can be used during transformation.- Returns:
- the transformed object or null if source is null
- Throws:
TypeTransformationException
- in case no route is found
-
getTransformedObject
public static <T> T getTransformedObject(Object sourceObject, Class<? extends T> targetType, Object transformationProperties) throws TypeTransformationException Directly get the transformed object or null if something went wrong.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the tyoe of class to trasnform totransformationProperties
- additional properties which can be used during transformation.- Returns:
- the transformed object or null if source is null
- Throws:
TypeTransformationException
- in case no route is found
-
getTransformedObject
public static <T> T getTransformedObject(Object sourceObject, Class<? extends T> targetType) throws TypeTransformationException Directly get the transformed object or null if something went wrong. No tranformation parameters are provided.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the tyoe of class to trasnform to- Returns:
- the transformed object or null if source is null
- Throws:
TypeTransformationException
- in case no route is found
-
getTransformedObjectOrNewInstance
public static <T> T getTransformedObjectOrNewInstance(Object sourceObject, Class<T> targetType, Properties transformationProperties) Directly get the transformed object or new instance if something went wrong. If a new instance could not be generated, null is returned.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the type of class to trasnform totransformationProperties
- additional properties which can be used during transformation.- Returns:
- the transformed object
-
getTransformedObjectOrNewInstance
public static <T> T getTransformedObjectOrNewInstance(Object sourceObject, Class<? extends T> targetType, Object transformationProperties) Directly get the transformed object or a new instance something went wrong. If a new instance could not be generated, null is returned.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the tyoe of class to trasnform totransformationProperties
- additional properties which can be used during transformation.- Returns:
- the transformed object or new instance
-
getTransformedObjectOrNewInstance
public static <T> T getTransformedObjectOrNewInstance(Object sourceObject, Class<? extends T> targetType) Directly get the transformed object or a new instance something went wrong. No tranformation parameters are provided. If a new instance could not be generated, null is returned.- Type Parameters:
T
- the type of the return value- Parameters:
sourceObject
- the source objecttargetType
- the tyoe of class to trasnform to- Returns:
- the transformed object or new instance
-
getTransformedProperties
public static Properties getTransformedProperties(Object parameters) throws TypeTransformationException Transforms a given object to java.lang:Properties or throws an exception if something went wrong.If parameter is null, then null is returned.- Parameters:
parameters
- the object which represents parameters.- Returns:
- java.lang:Properties or new Properties() if something went wrong
- Throws:
TypeTransformationException
- in case transformation route is not found.
-
getTransformedPropertiesOrNewInstance
Transforms a given object to java.lang:Properties or throws an exception if something went wrong. If parameter is null, then null is returned.- Parameters:
parameters
- the object which represents parameters.- Returns:
- java.lang:Properties or new Properties() if something went wrong
-
getNewInstance
-
classRepresentation
-
getAllSuperClassesAndIterfacesWithCost
static Map<Class<?>,Integer> getAllSuperClassesAndIterfacesWithCost(Class<?> clazz, int hierarchyCost) Given a class return all superclasses and interfaces except the Object class (which would be too generic). The value of the map is multiplied with the hierarchyCost- Parameters:
clazz
- the class to start searchinghierarchyCost
- the hierarchy cost which is multiplied with every level- Returns:
- a map which contains all superclasses and interfaces with corresponding depth
-
getAllSuperClassesAndIterfaces
Given a class return all superclasses and interfaces except the Object class (which woul be too generic).- Parameters:
clazz
- the class to start searching- Returns:
- a map which contains all superclasses and interfaces with corresponding depth
-