Package org.modelmapper
Interface TypeMap<S,D>
- Type Parameters:
S
- source typeD
- destination type
public interface TypeMap<S,D>
Encapsulates mapping configuration for a source and destination type pair.
-
Method Summary
Modifier and TypeMethodDescriptionaddMapping
(SourceGetter<S> sourceGetter, DestinationSetter<D, V> destinationSetter) Adds a mapping intoTypeMap
by definingsourceGetter
->destinationSetter
addMappings
(ExpressionMap<S, D> mapper) Add a mapping intoTypeMap
by defining amapper
action You can chainaddMappings
contains only one property mapping likeaddMappings
(PropertyMap<S, D> propertyMap) Loads mappings from thepropertyMap
into the TypeMap.Condition<?,
?> Returns the Condition that must apply for the source and destination in order for mapping to take place, elsenull
if no condition has been configured.Returns the Converter configured for this TypeMap, elsenull
if no Converter has been configured.Returns the destination type for the TypeMap.Returns a snapshot of the TypeMap's mappings.getName()
Returns the name of the TypeMap, elsenull
if the TypeMap has no name.Returns the converter to be used after mapping between the source and destination types, elsenull
if no post-Converter has been configured.Returns the converter to be used before mapping between the source and destination types, elsenull
if no post-Converter has been configured.Condition<?,
?> Returns the Condition that must apply in properties in this TypeMap to be mapped, elsenull
if no condition has been configured.Converter<?,
?> Returns the Converter used for converting properties in the TypeMap, elsenull
if no Converter has been configured.Provider<?>
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.Returns the source type for the TypeMap.Returns a snapshot list of destination properties that do not have mappings defined, else empty list if all destination properties are mapped.Performs implicit mapping.Makes this TypeMap is also supporting on the mapping fromsourceType
tobaseDestinationType
.Constructs a new TypeMap derived fromthis
.include
(TypeSafeSourceGetter<S, P> sourceGetter, Class<P> propertyType) Includesmappings
from property's TypeMap.includeBase
(Class<? super S> sourceType, Class<? super D> destinationType) Includesmappings
from a baseTypeMap
.Mapssource
to an instance of typeD
.void
Mapssource
todestination
.setCondition
(Condition<?, ?> condition) Sets thecondition
that must apply for the source and destination in order for mapping to take place.setConverter
(Converter<S, D> converter) Sets theconverter
to be used for any conversion requests for the TypeMap's source to destination type.setPostConverter
(Converter<S, D> converter) Sets theconverter
to be used after mapping between the source and destination types.setPreConverter
(Converter<S, D> converter) Sets theconverter
to be used before mapping between the source and destination types.setPropertyCondition
(Condition<?, ?> condition) Sets thecondition
that must apply in order for properties in this TypeMap to be mapped.setPropertyConverter
(Converter<?, ?> converter) Sets theconverter
to be used for converting properties in the TypeMap.setPropertyProvider
(Provider<?> provider) Sets theprovider
to be used for providing instances of properties during mapping.setProvider
(Provider<D> provider) Sets theprovider
to be used for providing instances of destination typeD
during mapping.void
validate()
Validates that every top level destination property is mapped to one and only one source property, or that aConverter
wasset
.
-
Method Details
-
addMappings
Loads mappings from thepropertyMap
into the TypeMap. Mappings defined in thepropertyMap
will override any implicit mappings for the same properties.- Parameters:
propertyMap
- from which mappings should be loaded- Returns:
- this type map
- Throws:
IllegalArgumentException
- ifpropertyMap
is nullConfigurationException
- if a configuration error occurs while adding mappings for thepropertyMap
-
getCondition
Condition<?,?> getCondition()Returns the Condition that must apply for the source and destination in order for mapping to take place, elsenull
if no condition has been configured.- See Also:
-
getConverter
Returns the Converter configured for this TypeMap, elsenull
if no Converter has been configured.- See Also:
-
getDestinationType
Returns the destination type for the TypeMap. -
getMappings
Returns a snapshot of the TypeMap's mappings.This method is part of the ModelMapper SPI.
-
getName
String getName()Returns the name of the TypeMap, elsenull
if the TypeMap has no name. -
getPostConverter
Returns the converter to be used after mapping between the source and destination types, elsenull
if no post-Converter has been configured.- See Also:
-
getPreConverter
Returns the converter to be used before mapping between the source and destination types, elsenull
if no post-Converter has been configured.- See Also:
-
getPropertyCondition
Condition<?,?> getPropertyCondition()Returns the Condition that must apply in properties in this TypeMap to be mapped, elsenull
if no condition has been configured.- See Also:
-
getPropertyConverter
Converter<?,?> getPropertyConverter()Returns the Converter used for converting properties in the TypeMap, elsenull
if no Converter has been configured.- See Also:
-
getPropertyProvider
Provider<?> getPropertyProvider()Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.- See Also:
-
getProvider
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.- See Also:
-
getSourceType
Returns the source type for the TypeMap. -
getUnmappedProperties
List<PropertyInfo> getUnmappedProperties()Returns a snapshot list of destination properties that do not have mappings defined, else empty list if all destination properties are mapped.This method is part of the ModelMapper SPI.
-
map
Mapssource
to an instance of typeD
.- Parameters:
source
- object to map from- Returns:
- fully mapped instance of type
D
- Throws:
IllegalArgumentException
- ifsource
is nullMappingException
- if an error occurs while mapping
-
map
Mapssource
todestination
.- Parameters:
source
- object to map fromdestination
- object to map to- Throws:
IllegalArgumentException
- ifsource
ordestination
are nullMappingException
- if an error occurs while mapping
-
setCondition
Sets thecondition
that must apply for the source and destination in order for mapping to take place.- Throws:
IllegalArgumentException
- ifcondition
is null
-
setConverter
Sets theconverter
to be used for any conversion requests for the TypeMap's source to destination type. This converter will be used in place of any mappings that have been added to the TypeMap.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPostConverter
Sets theconverter
to be used after mapping between the source and destination types.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPreConverter
Sets theconverter
to be used before mapping between the source and destination types.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPropertyCondition
Sets thecondition
that must apply in order for properties in this TypeMap to be mapped. This is overridden by any conditions defined in a PropertyMap.- Throws:
IllegalArgumentException
- ifcondition
is null
-
setPropertyConverter
Sets theconverter
to be used for converting properties in the TypeMap. This is overridden by any converters defined in a PropertyMap.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPropertyProvider
Sets theprovider
to be used for providing instances of properties during mapping. This is overriden by any providers defined in a PropertyMap.- Throws:
IllegalArgumentException
- ifprovider
is null
-
setProvider
Sets theprovider
to be used for providing instances of destination typeD
during mapping.- Throws:
IllegalArgumentException
- ifprovider
is null
-
validate
void validate()Validates that every top level destination property is mapped to one and only one source property, or that aConverter
wasset
. If not, a ConfigurationException is thrown detailing any missing mappings.- Throws:
ValidationException
- if any TypeMaps contain unmapped properties
-
addMapping
Adds a mapping intoTypeMap
by definingsourceGetter
->destinationSetter
typeMap.addMapping(Src::getA, Dest::setB); typeMap.<String>addMapping(src -> src.getC().getD(), (dest, value) -> dest.getE().setF(value))
- Type Parameters:
V
- type of destination property wants to be set- Parameters:
sourceGetter
- source property getterdestinationSetter
- destination property setter
-
addMappings
Add a mapping intoTypeMap
by defining amapper
action You can chainaddMappings
contains only one property mapping liketypeMap.addMappings(mapper -> mapper.<String>map(Src::getA, Dest::setB)) .addMappings(mapper -> mapper.<String>skip(Dest::setB)) .addMappings(mapper -> mapper.when(condition).<String>map(Src::getA, Dest::setB)) .addMappings(mapper -> mapper.when(condition).<String>skip(Dest::setB)) .addMappings(mapper -> mapper.using(converter).<String>map(Src::getA, Dest::setB)) .addMappings(mapper -> mapper.with(provider).<String>map(Src::getA, Dest::setB));
addMappings
liketypeMap.addMappings(mapper -> { mapper.<String>map(Src::getA, Dest::setB); mapper.<String>skip(Dest::setB); mapper.when(condition).<String>map(Src::getA, Dest::setB); });
- Parameters:
mapper
- a mapper defined a mapping action- Returns:
- this typeMap
-
include
Constructs a new TypeMap derived fromthis
. The derived TypeMap will include allmappings
from the baseTypeMap
, but will NOT includeconverter
,condition
, andprovider
from the baseTypeMap
.- Type Parameters:
DS
- derived type of source classDD
- derived type of destination class- Parameters:
sourceType
- source typedestinationType
- destination type- Returns:
- this type map
- Throws:
IllegalArgumentException
- ifTypePair.of(sourceType, destinationType)
already defined inmodelMapper.getTypeMaps()
-
include
Makes this TypeMap is also supporting on the mapping fromsourceType
tobaseDestinationType
.- Parameters:
baseDestinationType
- the base destination type- Returns:
- this type map.
- Throws:
IllegalArgumentException
- ifTypePair.of(sourceType, baseDestinationType)
already defined inmodelMapper.getTypeMaps()
-
includeBase
Includesmappings
from a baseTypeMap
.- Parameters:
sourceType
- source typedestinationType
- destination type- Returns:
- this type map
- Throws:
IllegalArgumentException
- ifTypePair.of(sourceType, destinationType)
already defined inmodelMapper.getTypeMaps()
-
include
Includesmappings
from property's TypeMap. If we want to map Source to Destination and we already have a TypeMap map a property of the source to destination, then we can include this property's TypeMap byinclude(Source::getProperty, Property.class)
.- Parameters:
sourceGetter
- the source getterpropertyType
- the property type- Returns:
- this type map
-
implicitMappings
Performs implicit mapping.- Returns:
- this type map
-