public class ModelMapper extends Object
Configuration
and stores TypeMaps
.
map
.createTypeMap
.addMappings
supplying a PropertyMap
.getConfiguration()
.
validate()
.
Constructor and Description |
---|
ModelMapper()
Creates a new ModelMapper.
|
Modifier and Type | Method and Description |
---|---|
<S,D> void |
addConverter(Converter<S,D> converter)
Registers the
converter to use when mapping instances of types S to D . |
<S,D> void |
addConverter(Converter<S,D> converter,
Class<S> sourceType,
Class<D> destinationType)
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
configuration . |
<S,D> TypeMap<S,D> |
createTypeMap(Class<S> sourceType,
Class<D> destinationType,
String typeMapName)
Creates a TypeMap for the
sourceType and destinationType identified by the
typeMapName using the ModelMapper's configuration. |
<S,D> TypeMap<S,D> |
createTypeMap(Class<S> sourceType,
Class<D> destinationType,
String typeMapName,
Configuration configuration)
Creates a TypeMap for the
sourceType and destinationType identified by the
typeMapName using the configuration . |
<S,D> TypeMap<S,D> |
createTypeMap(S source,
Class<D> destinationType)
Creates a TypeMap for the
source 's type and destinationType using the
ModelMapper's configuration. |
<S,D> TypeMap<S,D> |
createTypeMap(S source,
Class<D> destinationType,
Configuration configuration)
Creates a TypeMap for the
source 's type and destinationType using the
configuration . |
<S,D> TypeMap<S,D> |
createTypeMap(S source,
Class<D> destinationType,
String typeMapName)
Creates a TypeMap for the
source 's type and destinationType identified by the
typeMapName using the ModelMapper's configuration. |
<S,D> TypeMap<S,D> |
createTypeMap(S source,
Class<D> destinationType,
String typeMapName,
Configuration configuration)
Creates a TypeMap for the
source 's type and destinationType identified by the
typeMapName using the configuration . |
<S,D> TypeMap<S,D> |
emptyTypeMap(Class<S> sourceType,
Class<D> destinationType)
Creates an empty TypeMap for the
sourceType , destinationType . |
<S,D> TypeMap<S,D> |
emptyTypeMap(Class<S> sourceType,
Class<D> destinationType,
String typeMapName)
Creates an empty TypeMap for the
sourceType , destinationType . |
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. |
<S,D> TypeMap<S,D> |
getTypeMap(Class<S> sourceType,
Class<D> destinationType,
String typeMapName)
Returns the TypeMap for the
sourceType , destinationType and typeMapName
, 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 . |
<D> D |
map(Object source,
Class<D> destinationType,
String typeMapName)
Maps
source to an instance of destinationType . |
void |
map(Object source,
Object destination)
Maps
source to destination . |
void |
map(Object source,
Object destination,
String typeMapName)
Maps
source to destination . |
<D> D |
map(Object source,
Type destinationType)
Maps
source to an instance of destinationType . |
<D> D |
map(Object source,
Type destinationType,
String typeMapName)
Maps
source to an instance of destinationType . |
ModelMapper |
registerModule(Module module)
Register a module
|
<S,D> TypeMap<S,D> |
typeMap(Class<S> sourceType,
Class<D> destinationType)
Returns the TypeMap for the
sourceType , destinationType , creates TypeMap
automatically if none exists. |
<S,D> TypeMap<S,D> |
typeMap(Class<S> sourceType,
Class<D> destinationType,
String typeMapName)
Returns the TypeMap for the
sourceType , destinationType , and typeMapName
creates TypeMap automatically if none exists. |
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. |
public <S,D> void addConverter(Converter<S,D> converter)
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
.S
- source typeD
- destination typeconverter
- to registerIllegalArgumentException
- if converter
is null or if type arguments S
and D
are not declared for the converter
TypeMap.setConverter(Converter)
public <S,D> void addConverter(Converter<S,D> converter, Class<S> sourceType, Class<D> destinationType)
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
.S
- source typeD
- destination typeconverter
- to registerIllegalArgumentException
- if converter
is null or if type arguments S
and D
are not declared for the converter
TypeMap.setConverter(Converter)
public <S,D> TypeMap<S,D> addMappings(PropertyMap<S,D> propertyMap)
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.S
- source typeD
- destination typepropertyMap
- from which mappings should be loadedpropertyMap
IllegalArgumentException
- if propertyMap
is nullConfigurationException
- if a configuration error occurs while adding mappings for the
propertyMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType)
sourceType
and destinationType
using the
ModelMapper's configuration.S
- source typeD
- destination typesourceType
- destinationType
- IllegalArgumentException
- if sourceType
or destinationType
are nullIllegalStateException
- if a TypeMap already exists for sourceType
and
destinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class)
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration)
sourceType
and destinationType
using the
configuration
.S
- source typeD
- destination typesourceType
- destinationType
- configuration
- to apply to TypeMapIllegalArgumentException
- if sourceType
, destinationType
or
configuration
are nullIllegalStateException
- if a TypeMap already exists for sourceType
and
destinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class)
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
sourceType
and destinationType
identified by the
typeMapName
using the ModelMapper's configuration.S
- source typeD
- destination typesourceType
- destinationType
- typeMapName
- IllegalArgumentException
- if sourceType
, destinationType
or
typeMapName
are nullIllegalStateException
- if a TypeMap already exists for sourceType
,
destinationType
and typeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class, String)
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration)
sourceType
and destinationType
identified by the
typeMapName
using the configuration
.S
- source typeD
- destination typesourceType
- destinationType
- typeMapName
- configuration
- to apply to TypeMapIllegalArgumentException
- if sourceType
, destinationType
,
typeMapName
or configuration
are nullIllegalStateException
- if a TypeMap already exists for sourceType
,
destinationType
and typeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class, String)
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType)
source
's type and destinationType
using the
ModelMapper's configuration. Useful for creating TypeMaps for generic source data structures.S
- source typeD
- destination typesource
- destinationType
- IllegalArgumentException
- if source
or destinationType
are nullIllegalStateException
- if a TypeMap already exists for source
's type and
destinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class)
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, Configuration configuration)
source
's type and destinationType
using the
configuration
. Useful for creating TypeMaps for generic source data structures.S
- source typeD
- destination typesource
- destinationType
- configuration
- to apply to TypeMapIllegalArgumentException
- if source
, destinationType
or
configuration
are nullIllegalStateException
- if a TypeMap already exists for source
's type and
destinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class)
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName)
source
's type and destinationType
identified by the
typeMapName
using the ModelMapper's configuration. Useful for creating TypeMaps for
generic source data structures.S
- source typeD
- destination typesource
- destinationType
- typeMapName
- IllegalArgumentException
- if source
, destinationType
or
typeMapName
are nullIllegalStateException
- if a TypeMap already exists for source
's type,
destinationType
and typeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class, String)
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration)
source
's type and destinationType
identified by the
typeMapName
using the configuration
. Useful for creating TypeMaps for generic
source data structures.S
- source typeD
- destination typesource
- destinationType
- typeMapName
- configuration
- to apply to TypeMapIllegalArgumentException
- if source
, destinationType
,
typeMapName
or configuration
are nullIllegalStateException
- if a TypeMap already exists for source
's type,
destinationType
and typeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMapgetTypeMap(Class, Class, String)
public Configuration getConfiguration()
public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType)
sourceType
and destinationType
, else returns
null
if none exists.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
or destinationType
are nullcreateTypeMap(Class, Class)
public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
sourceType
, destinationType
and typeMapName
, else returns null
if none exists.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
, destinationType
or
typeMapName
are nullcreateTypeMap(Class, Class, String)
public <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType)
sourceType
, destinationType
, creates TypeMap
automatically if none exists.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
, destinationType
are nullpublic <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
sourceType
, destinationType
, and typeMapName
creates TypeMap automatically if none exists.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
, destinationType
or
typeMapName
are nullpublic <S,D> TypeMap<S,D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType)
sourceType
, destinationType
.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
or destinationType
are null, or TypeMap<SourceType, DestinationType
already defined in the TypeMapStorepublic <S,D> TypeMap<S,D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
sourceType
, destinationType
.S
- source typeD
- destination typeIllegalArgumentException
- is sourceType
or destinationType
are null, or TypeMap<Source Type, DestinationType
already defined in the TypeMapStorepublic Collection<TypeMap<?,?>> getTypeMaps()
public <D> D map(Object source, Class<D> destinationType)
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.D
- destination typesource
- object to map fromdestinationType
- type to map todestinationType
IllegalArgumentException
- if source
or destinationType
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the
argumentsMappingException
- if a runtime error occurs while mappingpublic <D> D map(Object source, Class<D> destinationType, String typeMapName)
source
to an instance of destinationType
. Mapping is performed according
to the corresponding TypeMap for the typeMapName
. If no TypeMap exists for the
source.getClass()
, destinationType
and typeMapName
then one is created.D
- destination typesource
- object to map fromdestinationType
- type to map totypeMapName
- name of existing TypeMap to use mappings fromdestinationType
IllegalArgumentException
- if source
, destinationType
or
typeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the
argumentsMappingException
- if a runtime error occurs while mappingpublic void map(Object source, Object destination)
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.source
- object to map fromdestination
- object to map toIllegalArgumentException
- if source
or destination
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the
argumentsMappingException
- if an error occurs while mappingpublic void map(Object source, Object destination, String typeMapName)
source
to destination
. Mapping is performed according to the corresponding
TypeMap for the typeMapName
. If no TypeMap exists for the source.getClass()
,
destination.getClass()
and typeMapName
then one is created.source
- object to map fromdestination
- object to map totypeMapName
- name of existing TypeMap to use mappings fromIllegalArgumentException
- if source
, destination
or typeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the
argumentsMappingException
- if an error occurs while mappingpublic <D> D map(Object source, Type destinationType)
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.
To map a parameterized destination type, subclass TypeToken
and obtain its Type:
Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType);
D
- destination typesource
- object to map fromdestinationType
- type to map todestinationType
IllegalArgumentException
- if source
or destinationType
are nullConfigurationException
- if the ModelMapper cannot find or create the TypeMapMappingException
- if a runtime error occurs while mappingpublic <D> D map(Object source, Type destinationType, String typeMapName)
source
to an instance of destinationType
. Mapping is performed according
to the corresponding TypeMap for the typeMapName
. If no TypeMap exists for the
source.getClass()
, destination.getClass()
and typeMapName
then one is
created.
To map a parameterized destination type, subclass TypeToken
and obtain its Type:
Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType, "string-list");
D
- destination typesource
- object to map fromdestinationType
- type to map totypeMapName
- name of existing TypeMap to use mappings fromdestinationType
IllegalArgumentException
- if source
, destinationType
or
typeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create the TypeMapMappingException
- if a runtime error occurs while mappingpublic void validate()
Converter
was
set
for the TypeMap. If not, a ConfigurationException
is thrown detailing any missing mappings.ValidationException
- if any TypeMaps contain unmapped propertiespublic ModelMapper registerModule(Module module)
module
- a module for extensionCopyright © 2011–2024. All rights reserved.