java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_base.typetransformer.TypeTransformerRegistry

public class TypeTransformerRegistry extends Object
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
and many more.
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
      Default logger.
    • TRANFORMERS

      private static final Map<Class<?>,Map<Class<?>,Set<TypeTransformer<?,?>>>> TRANFORMERS
      The map which contains all transformers.
    • HIERARCHY_TRANSFORMATION_COST

      private static final int HIERARCHY_TRANSFORMATION_COST
      The additional tranformation cost (determined by the environment variable MELT_TRANSFORMATION_HIERARCHY_COST )to add if hierarchy is allowed e.g. when we have a TypeTransformer 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_STEP
      If true, then the tranformation can be performed with multiple steps (TypeTransformer). If false, only one TypeTransformer 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

      private static final List<String> MELT_DEFAULT_TRANSFORMERS
    • SUPER_CLASSES_CACHE

      private static final Map<Class<?>,Map<Class<?>,Integer>> 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

      public static void addTransformer(TypeTransformer<?,?> transformer)
    • addAllTransformersViaServiceRegistry

      public static void addAllTransformersViaServiceRegistry()
    • addAllTransformersViaServiceRegistry

      public static void addAllTransformersViaServiceRegistry(ClassLoader classloader)
    • 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

      public static void removeTransformer(TypeTransformer<?,?> transformer)
    • clear

      public static void clear()
      Removes all transformers. Use it with care.
    • getAllRegisteredTypeTransformers

      public static Set<TypeTransformer<?,?>> getAllRegisteredTypeTransformers()
      Get all registered tranformers. For debugging purposes.
      Returns:
      all registered transformers.
    • getAllRegisteredTypeTransformersAsString

      public static String 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: see HIERARCHY_TRANSFORMATION_COST
      allowMultiStep - allow multi step: see ALLOW_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 class
      target - 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 classes
      target - the target class
      parameters - the parameters which are forwarded to each type transformer
      hierarchyTransformationCost - negative value (below zero) if the hierarchy is not allowed, otherwise the cost for each subclass to superclass relation - see also HIERARCHY_TRANSFORMATION_COST
      allowMultiStep - allow multiple type transformers - see also ALLOW_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

      private static Class<?> getMinimum(Map<Class<?>,Integer> distances, Set<Class<?>> vertexes)
    • 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 to
      transformationProperties - 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 to
      transformationProperties - 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 to
      transformationProperties - 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 to
      transformationProperties - 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 object
      targetType - the tyoe of class to trasnform to
      transformationProperties - 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 object
      targetType - the tyoe of class to trasnform to
      transformationProperties - 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 object
      targetType - 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 object
      targetType - the type of class to trasnform to
      transformationProperties - 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 object
      targetType - the tyoe of class to trasnform to
      transformationProperties - 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 object
      targetType - 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

      public static Properties getTransformedPropertiesOrNewInstance(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.
      Parameters:
      parameters - the object which represents parameters.
      Returns:
      java.lang:Properties or new Properties() if something went wrong
    • getNewInstance

      public static <T> T getNewInstance(Class<T> clazz)
    • classRepresentation

      private static String classRepresentation(Set<Object> objects)
    • 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 searching
      hierarchyCost - the hierarchy cost which is multiplied with every level
      Returns:
      a map which contains all superclasses and interfaces with corresponding depth
    • getAllSuperClassesAndIterfaces

      static Map<Class<?>,Integer> getAllSuperClassesAndIterfaces(Class<?> clazz)
      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