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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <V> TypeMap<S,D>
addMapping(SourceGetter<S> sourceGetter, DestinationSetter<D,V> destinationSetter)
Adds a mapping intoTypeMap
by definingsourceGetter
->destinationSetter
TypeMap<S,D>
addMappings(ExpressionMap<S,D> mapper)
Add a mapping intoTypeMap
by defining amapper
action You can chainaddMappings
contains only one property mapping likeTypeMap<S,D>
addMappings(PropertyMap<S,D> propertyMap)
Loads mappings from thepropertyMap
into the TypeMap.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.Converter<S,D>
getConverter()
Returns the Converter configured for this TypeMap, elsenull
if no Converter has been configured.Class<D>
getDestinationType()
Returns the destination type for the TypeMap.List<Mapping>
getMappings()
Returns a snapshot of the TypeMap's mappings.String
getName()
Returns the name of the TypeMap, elsenull
if the TypeMap has no name.Converter<S,D>
getPostConverter()
Returns the converter to be used after mapping between the source and destination types, elsenull
if no post-Converter has been configured.Converter<S,D>
getPreConverter()
Returns the converter to be used before mapping between the source and destination types, elsenull
if no post-Converter has been configured.Condition<?,?>
getPropertyCondition()
Returns the Condition that must apply in properties in this TypeMap to be mapped, elsenull
if no condition has been configured.Converter<?,?>
getPropertyConverter()
Returns the Converter used for converting properties in the TypeMap, elsenull
if no Converter has been configured.Provider<?>
getPropertyProvider()
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.Provider<D>
getProvider()
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.Class<S>
getSourceType()
Returns the source type for the TypeMap.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.TypeMap<S,D>
implicitMappings()
Performs implicit mapping.TypeMap<S,D>
include(Class<? super D> baseDestinationType)
Makes this TypeMap is also supporting on the mapping fromsourceType
tobaseDestinationType
.<DS extends S,DD extends D>
TypeMap<S,D>include(Class<DS> sourceType, Class<DD> destinationType)
Constructs a new TypeMap derived fromthis
.<P> TypeMap<S,D>
include(TypeSafeSourceGetter<S,P> sourceGetter, Class<P> propertyType)
Includesmappings
from property's TypeMap.TypeMap<S,D>
includeBase(Class<? super S> sourceType, Class<? super D> destinationType)
Includesmappings
from a baseTypeMap
.D
map(S source)
Mapssource
to an instance of typeD
.void
map(S source, D destination)
Mapssource
todestination
.TypeMap<S,D>
setCondition(Condition<?,?> condition)
Sets thecondition
that must apply for the source and destination in order for mapping to take place.TypeMap<S,D>
setConverter(Converter<S,D> converter)
Sets theconverter
to be used for any conversion requests for the TypeMap's source to destination type.TypeMap<S,D>
setPostConverter(Converter<S,D> converter)
Sets theconverter
to be used after mapping between the source and destination types.TypeMap<S,D>
setPreConverter(Converter<S,D> converter)
Sets theconverter
to be used before mapping between the source and destination types.TypeMap<S,D>
setPropertyCondition(Condition<?,?> condition)
Sets thecondition
that must apply in order for properties in this TypeMap to be mapped.TypeMap<S,D>
setPropertyConverter(Converter<?,?> converter)
Sets theconverter
to be used for converting properties in the TypeMap.TypeMap<S,D>
setPropertyProvider(Provider<?> provider)
Sets theprovider
to be used for providing instances of properties during mapping.TypeMap<S,D>
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 Detail
-
addMappings
TypeMap<S,D> addMappings(PropertyMap<S,D> propertyMap)
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:
setCondition(Condition)
-
getConverter
Converter<S,D> getConverter()
Returns the Converter configured for this TypeMap, elsenull
if no Converter has been configured.- See Also:
setConverter(Converter)
-
getMappings
List<Mapping> 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
Converter<S,D> 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:
setPostConverter(Converter)
-
getPreConverter
Converter<S,D> 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:
setPreConverter(Converter)
-
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:
setCondition(Condition)
-
getPropertyConverter
Converter<?,?> getPropertyConverter()
Returns the Converter used for converting properties in the TypeMap, elsenull
if no Converter has been configured.- See Also:
setPropertyConverter(Converter)
-
getPropertyProvider
Provider<?> getPropertyProvider()
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.- See Also:
setPropertyProvider(Provider)
-
getProvider
Provider<D> getProvider()
Returns the Provider configured for this TypeMap, elsenull
if no Provider has been configured.- See Also:
setProvider(Provider)
-
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
D map(S source)
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
void map(S source, D destination)
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
TypeMap<S,D> setCondition(Condition<?,?> condition)
Sets thecondition
that must apply for the source and destination in order for mapping to take place.- Throws:
IllegalArgumentException
- ifcondition
is null
-
setConverter
TypeMap<S,D> setConverter(Converter<S,D> converter)
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
TypeMap<S,D> setPostConverter(Converter<S,D> converter)
Sets theconverter
to be used after mapping between the source and destination types.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPreConverter
TypeMap<S,D> setPreConverter(Converter<S,D> converter)
Sets theconverter
to be used before mapping between the source and destination types.- Throws:
IllegalArgumentException
- ifconverter
is null
-
setPropertyCondition
TypeMap<S,D> setPropertyCondition(Condition<?,?> condition)
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
TypeMap<S,D> setPropertyConverter(Converter<?,?> converter)
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
TypeMap<S,D> setPropertyProvider(Provider<?> provider)
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
TypeMap<S,D> setProvider(Provider<D> provider)
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
<V> TypeMap<S,D> addMapping(SourceGetter<S> sourceGetter, DestinationSetter<D,V> destinationSetter)
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
TypeMap<S,D> addMappings(ExpressionMap<S,D> mapper)
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
<DS extends S,DD extends D> TypeMap<S,D> include(Class<DS> sourceType, Class<DD> destinationType)
Constructs a new TypeMap derived fromthis
. The derived TypeMap will includes 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
TypeMap<S,D> include(Class<? super D> baseDestinationType)
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
TypeMap<S,D> includeBase(Class<? super S> sourceType, Class<? super D> destinationType)
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
<P> TypeMap<S,D> include(TypeSafeSourceGetter<S,P> sourceGetter, Class<P> propertyType)
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
-
-