org.modelmapper
Class ModelMapper

java.lang.Object
  extended by org.modelmapper.ModelMapper

public class ModelMapper
extends Object

ModelMapper framework entry point. Performs object mapping and contains mapping related configuration.

Author:
Jonathan Halterman

Constructor Summary
ModelMapper()
          Creates a new ModelMapper.
 
Method Summary
<S,D> void
addConverter(Converter<S,D> converter)
          Registers the converter to use when mapping instances of types S to D.
<S,D> TypeMap<S,D>
addMappings(PropertyMap<S,D> propertyMap)
          Adds mappings from the propertyMap into the TypeMap corresponding to source type S and destination type D.
<S,D> TypeMap<S,D>
createTypeMap(Class<S> sourceType, Class<D> destinationType)
          Creates a TypeMap for the sourceType and destinationType using the ModelMapper's configuration.
<S,D> TypeMap<S,D>
createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration)
          Creates a TypeMap for the sourceType and destinationType using the given configuration.
 Configuration getConfiguration()
          Returns the ModelMapper's configuration.
<S,D> TypeMap<S,D>
getTypeMap(Class<S> sourceType, Class<D> destinationType)
          Returns the TypeMap for the sourceType and destinationType, else returns null if none exists.
 Collection<TypeMap<?,?>> getTypeMaps()
          Returns all TypeMaps for the ModelMapper.
<D> D
map(Object source, Class<D> destinationType)
          Maps source to an instance of destinationType.
 void map(Object source, Object destination)
          Maps source to destination.
 void validate()
          Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that a Converter was set for the TypeMap.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModelMapper

public ModelMapper()
Creates a new ModelMapper.

Method Detail

addConverter

public <S,D> void addConverter(Converter<S,D> converter)
Registers the converter to use when mapping instances of types S to D. The converter will be set against TypeMap corresponding to the converter's type arguments S and D.

Type Parameters:
S - source type
D - destination type
Parameters:
converter - to register
Throws:
IllegalArgumentException - if converter is null or if type arguments S and D are not declared for the converter
See Also:
TypeMap.setConverter(Converter)

addMappings

public <S,D> TypeMap<S,D> addMappings(PropertyMap<S,D> propertyMap)
Adds mappings from the propertyMap into the TypeMap corresponding to source type S and destination type D. Explicit mappings defined in the propertyMap will override any implicit mappings for the same properties.

Type Parameters:
S - source type
D - destination type
Parameters:
propertyMap - from which mappings should be loaded
Returns:
TypeMap corresponding to the propertyMap
Throws:
ConfigurationException - if an error occurred while reading mappings from the propertyMap
IllegalArgumentException - if propertyMap is null
ConfigurationException - if a configuration error occurs while adding mappings for the propertyMap

createTypeMap

public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType,
                                        Class<D> destinationType)
Creates a TypeMap for the sourceType and destinationType using the ModelMapper's configuration.

Type Parameters:
S - source type
D - destination type
Parameters:
sourceType -
destinationType -
Throws:
IllegalStateException - if a TypeMap already exists for sourceType and destinationType
ConfigurationException - if the ModelMapper cannot create the TypeMap
See Also:
getTypeMap(Class, Class)

createTypeMap

public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType,
                                        Class<D> destinationType,
                                        Configuration configuration)
Creates a TypeMap for the sourceType and destinationType using the given configuration.

Type Parameters:
S - source type
D - destination type
Parameters:
sourceType -
destinationType -
configuration -
Throws:
IllegalStateException - if a TypeMap already exists for sourceType and destinationType
ConfigurationException - if the ModelMapper cannot create the TypeMap
See Also:
getTypeMap(Class, Class)

getConfiguration

public Configuration getConfiguration()
Returns the ModelMapper's configuration.


getTypeMap

public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType,
                                     Class<D> destinationType)
Returns the TypeMap for the sourceType and destinationType, else returns null if none exists.

Type Parameters:
S - source type
D - destination type
Throws:
IllegalArgumentException - is sourceType or destinationType are null
See Also:
createTypeMap(Class, Class)

getTypeMaps

public Collection<TypeMap<?,?>> getTypeMaps()
Returns all TypeMaps for the ModelMapper.


map

public <D> D map(Object source,
                 Class<D> destinationType)
Maps source to an instance of destinationType. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists for source.getClass() and destinationType then one is created.

Type Parameters:
D - destination type
Parameters:
source - object to map from
destinationType - type to map to
Returns:
fully mapped instance of destinationType
Throws:
IllegalArgumentException - if source or destinationType are null
ConfigurationException - if the ModelMapper cannot find or create the TypeMap
MappingException - if a runtime error occurs while mapping

map

public void map(Object source,
                Object destination)
Maps source to destination. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists for source.getClass() and destination.getClass() then one is created.

Parameters:
source - object to map from
destination - object to map to
Throws:
IllegalArgumentException - if source or destination are null
ConfigurationException - if the ModelMapper cannot find or create the TypeMap
MappingException - if an error occurs while mapping

validate

public void validate()
Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that a Converter was set for the TypeMap. If not, a ConfigurationException is thrown detailing any missing mappings.

Throws:
ValidationException - if any TypeMaps contain unmapped properties


Copyright © 2011. All Rights Reserved.