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 intoTypeMapby definingsourceGetter->destinationSetteraddMappings(ExpressionMap<S, D> mapper) Add a mapping intoTypeMapby defining amapperaction You can chainaddMappingscontains only one property mapping likeaddMappings(PropertyMap<S, D> propertyMap) Loads mappings from thepropertyMapinto the TypeMap.Condition<?,?> Returns the Condition that must apply for the source and destination in order for mapping to take place, elsenullif no condition has been configured.Returns the Converter configured for this TypeMap, elsenullif 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, elsenullif the TypeMap has no name.Returns the converter to be used after mapping between the source and destination types, elsenullif no post-Converter has been configured.Returns the converter to be used before mapping between the source and destination types, elsenullif no post-Converter has been configured.Condition<?,?> Returns the Condition that must apply in properties in this TypeMap to be mapped, elsenullif no condition has been configured.Converter<?,?> Returns the Converter used for converting properties in the TypeMap, elsenullif no Converter has been configured.Provider<?>Returns the Provider configured for this TypeMap, elsenullif no Provider has been configured.Returns the Provider configured for this TypeMap, elsenullif 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 fromsourceTypetobaseDestinationType.Constructs a new TypeMap derived fromthis.include(TypeSafeSourceGetter<S, P> sourceGetter, Class<P> propertyType) Includesmappingsfrom property's TypeMap.includeBase(Class<? super S> sourceType, Class<? super D> destinationType) Includesmappingsfrom a baseTypeMap.Mapssourceto an instance of typeD.voidMapssourcetodestination.setCondition(Condition<?, ?> condition) Sets theconditionthat must apply for the source and destination in order for mapping to take place.setConverter(Converter<S, D> converter) Sets theconverterto be used for any conversion requests for the TypeMap's source to destination type.setPostConverter(Converter<S, D> converter) Sets theconverterto be used after mapping between the source and destination types.setPreConverter(Converter<S, D> converter) Sets theconverterto be used before mapping between the source and destination types.setPropertyCondition(Condition<?, ?> condition) Sets theconditionthat must apply in order for properties in this TypeMap to be mapped.setPropertyConverter(Converter<?, ?> converter) Sets theconverterto be used for converting properties in the TypeMap.setPropertyProvider(Provider<?> provider) Sets theproviderto be used for providing instances of properties during mapping.setProvider(Provider<D> provider) Sets theproviderto be used for providing instances of destination typeDduring mapping.voidvalidate()Validates that every top level destination property is mapped to one and only one source property, or that aConverterwasset.
-
Method Details
-
addMappings
Loads mappings from thepropertyMapinto the TypeMap. Mappings defined in thepropertyMapwill override any implicit mappings for the same properties.- Parameters:
propertyMap- from which mappings should be loaded- Returns:
- this type map
- Throws:
IllegalArgumentException- ifpropertyMapis 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, elsenullif no condition has been configured.- See Also:
-
getConverter
Returns the Converter configured for this TypeMap, elsenullif 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, elsenullif the TypeMap has no name. -
getPostConverter
Returns the converter to be used after mapping between the source and destination types, elsenullif no post-Converter has been configured.- See Also:
-
getPreConverter
Returns the converter to be used before mapping between the source and destination types, elsenullif 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, elsenullif no condition has been configured.- See Also:
-
getPropertyConverter
Converter<?,?> getPropertyConverter()Returns the Converter used for converting properties in the TypeMap, elsenullif no Converter has been configured.- See Also:
-
getPropertyProvider
Provider<?> getPropertyProvider()Returns the Provider configured for this TypeMap, elsenullif no Provider has been configured.- See Also:
-
getProvider
Returns the Provider configured for this TypeMap, elsenullif 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
Mapssourceto an instance of typeD.- Parameters:
source- object to map from- Returns:
- fully mapped instance of type
D - Throws:
IllegalArgumentException- ifsourceis nullMappingException- if an error occurs while mapping
-
map
Mapssourcetodestination.- Parameters:
source- object to map fromdestination- object to map to- Throws:
IllegalArgumentException- ifsourceordestinationare nullMappingException- if an error occurs while mapping
-
setCondition
Sets theconditionthat must apply for the source and destination in order for mapping to take place.- Throws:
IllegalArgumentException- ifconditionis null
-
setConverter
Sets theconverterto 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- ifconverteris null
-
setPostConverter
Sets theconverterto be used after mapping between the source and destination types.- Throws:
IllegalArgumentException- ifconverteris null
-
setPreConverter
Sets theconverterto be used before mapping between the source and destination types.- Throws:
IllegalArgumentException- ifconverteris null
-
setPropertyCondition
Sets theconditionthat must apply in order for properties in this TypeMap to be mapped. This is overridden by any conditions defined in a PropertyMap.- Throws:
IllegalArgumentException- ifconditionis null
-
setPropertyConverter
Sets theconverterto be used for converting properties in the TypeMap. This is overridden by any converters defined in a PropertyMap.- Throws:
IllegalArgumentException- ifconverteris null
-
setPropertyProvider
Sets theproviderto be used for providing instances of properties during mapping. This is overriden by any providers defined in a PropertyMap.- Throws:
IllegalArgumentException- ifprovideris null
-
setProvider
Sets theproviderto be used for providing instances of destination typeDduring mapping.- Throws:
IllegalArgumentException- ifprovideris null
-
validate
void validate()Validates that every top level destination property is mapped to one and only one source property, or that aConverterwasset. If not, a ConfigurationException is thrown detailing any missing mappings.- Throws:
ValidationException- if any TypeMaps contain unmapped properties
-
addMapping
Adds a mapping intoTypeMapby definingsourceGetter->destinationSettertypeMap.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 intoTypeMapby defining amapperaction You can chainaddMappingscontains only one property mapping like
Or you can define all property mappings in onetypeMap.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));addMappingsliketypeMap.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 allmappingsfrom the baseTypeMap, but will NOT includeconverter,condition, andproviderfrom 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 fromsourceTypetobaseDestinationType.- Parameters:
baseDestinationType- the base destination type- Returns:
- this type map.
- Throws:
IllegalArgumentException- ifTypePair.of(sourceType, baseDestinationType)already defined inmodelMapper.getTypeMaps()
-
includeBase
Includesmappingsfrom a baseTypeMap.- Parameters:
sourceType- source typedestinationType- destination type- Returns:
- this type map
- Throws:
IllegalArgumentException- ifTypePair.of(sourceType, destinationType)already defined inmodelMapper.getTypeMaps()
-
include
Includesmappingsfrom 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
-