Interface TypeMap<S,​D>

  • Type Parameters:
    S - source type
    D - destination type

    public interface TypeMap<S,​D>
    Encapsulates mapping configuration for a source and destination type pair.
    • Method Detail

      • addMappings

        TypeMap<S,​D> addMappings​(PropertyMap<S,​D> propertyMap)
        Loads mappings from the propertyMap into the TypeMap. Mappings defined in the propertyMap will override any implicit mappings for the same properties.
        Parameters:
        propertyMap - from which mappings should be loaded
        Returns:
        this type map
        Throws:
        IllegalArgumentException - if propertyMap is null
        ConfigurationException - if a configuration error occurs while adding mappings for the propertyMap
      • getCondition

        Condition<?,​?> getCondition()
        Returns the Condition that must apply for the source and destination in order for mapping to take place, else null if no condition has been configured.
        See Also:
        setCondition(Condition)
      • getConverter

        Converter<S,​D> getConverter()
        Returns the Converter configured for this TypeMap, else null if no Converter has been configured.
        See Also:
        setConverter(Converter)
      • getDestinationType

        Class<D> getDestinationType()
        Returns the destination type for the TypeMap.
      • 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, else null 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, else null 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, else null 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, else null if no condition has been configured.
        See Also:
        setCondition(Condition)
      • getPropertyConverter

        Converter<?,​?> getPropertyConverter()
        Returns the Converter used for converting properties in the TypeMap, else null if no Converter has been configured.
        See Also:
        setPropertyConverter(Converter)
      • getPropertyProvider

        Provider<?> getPropertyProvider()
        Returns the Provider configured for this TypeMap, else null if no Provider has been configured.
        See Also:
        setPropertyProvider(Provider)
      • getProvider

        Provider<D> getProvider()
        Returns the Provider configured for this TypeMap, else null if no Provider has been configured.
        See Also:
        setProvider(Provider)
      • getSourceType

        Class<S> 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

        D map​(S source)
        Maps source to an instance of type D.
        Parameters:
        source - object to map from
        Returns:
        fully mapped instance of type D
        Throws:
        IllegalArgumentException - if source is null
        MappingException - if an error occurs while mapping
      • map

        void map​(S source,
                 D destination)
        Maps source to destination.
        Parameters:
        source - object to map from
        destination - object to map to
        Throws:
        IllegalArgumentException - if source or destination are null
        MappingException - if an error occurs while mapping
      • setCondition

        TypeMap<S,​D> setCondition​(Condition<?,​?> condition)
        Sets the condition that must apply for the source and destination in order for mapping to take place.
        Throws:
        IllegalArgumentException - if condition is null
      • setConverter

        TypeMap<S,​D> setConverter​(Converter<S,​D> converter)
        Sets the converter 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 - if converter is null
      • setPostConverter

        TypeMap<S,​D> setPostConverter​(Converter<S,​D> converter)
        Sets the converter to be used after mapping between the source and destination types.
        Throws:
        IllegalArgumentException - if converter is null
      • setPreConverter

        TypeMap<S,​D> setPreConverter​(Converter<S,​D> converter)
        Sets the converter to be used before mapping between the source and destination types.
        Throws:
        IllegalArgumentException - if converter is null
      • setPropertyCondition

        TypeMap<S,​D> setPropertyCondition​(Condition<?,​?> condition)
        Sets the condition 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 - if condition is null
      • setPropertyConverter

        TypeMap<S,​D> setPropertyConverter​(Converter<?,​?> converter)
        Sets the converter to be used for converting properties in the TypeMap. This is overridden by any converters defined in a PropertyMap.
        Throws:
        IllegalArgumentException - if converter is null
      • setPropertyProvider

        TypeMap<S,​D> setPropertyProvider​(Provider<?> provider)
        Sets the provider to be used for providing instances of properties during mapping. This is overriden by any providers defined in a PropertyMap.
        Throws:
        IllegalArgumentException - if provider is null
      • setProvider

        TypeMap<S,​D> setProvider​(Provider<D> provider)
        Sets the provider to be used for providing instances of destination type D during mapping.
        Throws:
        IllegalArgumentException - if provider is null
      • validate

        void validate()
        Validates that every top level destination property is mapped to one and only one source property, or that a Converter was set. 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 into TypeMap by defining sourceGetter -> 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 getter
        destinationSetter - destination property setter
      • addMappings

        TypeMap<S,​D> addMappings​(ExpressionMap<S,​D> mapper)
        Add a mapping into TypeMap by defining a mapper action You can chain addMappings contains only one property mapping like
         
           typeMap.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));
         
         
        Or you can define all property mappings in one addMappings like
         
           typeMap.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 DTypeMap<S,​D> include​(Class<DS> sourceType,
                                                                     Class<DD> destinationType)
        Constructs a new TypeMap derived from this. The derived TypeMap will includes all mappings from the base TypeMap, but will NOT include converter, condition, and provider from the base TypeMap.
        Type Parameters:
        DS - derived type of source class
        DD - derived type of destination class
        Parameters:
        sourceType - source type
        destinationType - destination type
        Returns:
        this type map
        Throws:
        IllegalArgumentException - if TypePair.of(sourceType, destinationType) already defined in modelMapper.getTypeMaps()
      • include

        TypeMap<S,​D> include​(Class<? super D> baseDestinationType)
        Makes this TypeMap is also supporting on the mapping from sourceType to baseDestinationType.
        Parameters:
        baseDestinationType - the base destination type
        Returns:
        this type map.
        Throws:
        IllegalArgumentException - if TypePair.of(sourceType, baseDestinationType) already defined in modelMapper.getTypeMaps()
      • includeBase

        TypeMap<S,​D> includeBase​(Class<? super S> sourceType,
                                       Class<? super D> destinationType)
        Includes mappings from a base TypeMap.
        Parameters:
        sourceType - source type
        destinationType - destination type
        Returns:
        this type map
        Throws:
        IllegalArgumentException - if TypePair.of(sourceType, destinationType) already defined in modelMapper.getTypeMaps()
      • include

        <P> TypeMap<S,​D> include​(TypeSafeSourceGetter<S,​P> sourceGetter,
                                       Class<P> propertyType)
        Includes mappings 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 by include(Source::getProperty, Property.class).
        Parameters:
        sourceGetter - the source getter
        propertyType - the property type
        Returns:
        this type map
      • implicitMappings

        TypeMap<S,​D> implicitMappings()
        Performs implicit mapping.
        Returns:
        this type map