Class MappingConfiguration<S,​D>

  • Type Parameters:
    S - source type of the mapping
    D - destination type of the mapping

    public class MappingConfiguration<S,​D>
    extends Object
    The MappingConfiguration object is used to specify the mapping of the fields from a source object/type to a destination object/type. Only properties can be mapped. Properties are defined with the Java Bean Convention:
    • A property is a field with any visibility
    • A property has a public getter/setter pair exactly named as the field
    • Boolean values have is/setter methods.
    • A bean has a default zero-args constructor.
    For the mapping, keywords like transient do not have any effect.

    The mapper always tries to map all properties with name from the source to the destination object. To retrieve a valid mapper, all properties must be either mapped/reassigned or omitted. If there are unmapped properties in source/destination type, the mapper() throws an error. If the mapping contains nested mappings of other complex types, a delegation mapper must be registered using useMapper(Mapper), otherweise a MappingException is thrown.

    Author:
    schuettec
    • Method Detail

      • omitInDestination

        public MappingConfiguration<S,​D> omitInDestination​(FieldSelector<D> destinationSelector)
        Marks a destination field as omitted. The mapping will not touch this field in the destination object.
        Parameters:
        destinationSelector - The FieldSelector lambda that selects the field with invoking the corresponding getter method.
        Returns:
        Returns this object for method chaining.
      • omitOthers

        public MappingConfiguration<S,​D> omitOthers()
        Omits all unmapped source and destination fields. This method adds the necessary omitInDestination(FieldSelector) and omitInSource(FieldSelector) declarations to the mapping as if they were called specifically.

        Note: The use of omitOthers() carries the risk of erroneously excluding fields from mapping. For example: If a field is added on the source type, a mapping configuration that does not use omitOthers() will complain about a new unmapped field. This normally gives the developer a hint, to either specify a mapping or omit this field intentionally. If this method is used, any unmapped field will be omitted without notification!

        Returns:
        Returns this object for method chaining.
      • omitOtherDestinationProperties

        public MappingConfiguration<S,​D> omitOtherDestinationProperties()
        Omits all unmapped destination fields. This method adds the necessary omitInDestination(FieldSelector) declarations to the mapping as if they were called specifically.

        Note: The use of omitOtherDestinationProperties() carries the risk of erroneously excluding fields from mapping. For example: If a field is added on the destination type, a mapping configuration that does not use omitOtherDestinationProperties() will complain about a new unmapped field. This normally gives the developer a hint, to either specify a mapping or omit this field intentionally. If this method is used, any unmapped field will be omitted without notification!

        Returns:
        Returns this object for method chaining.
      • omitOtherSourceProperties

        public MappingConfiguration<S,​D> omitOtherSourceProperties()
        Omits all unmapped source fields. This method adds the necessary omitInSource(FieldSelector) declarations to the mapping as if they were called specifically.

        Note: The use of omitOtherSourceProperties() carries the risk of erroneously excluding fields from mapping. For example: If a field is added on the source type, a mapping configuration that does not use omitOtherSourceProperties() will complain about a new unmapped field. This normally gives the developer a hint, to either specify a mapping or omit this field intentionally. If this method is used, any unmapped field will be omitted without notification!

        Returns:
        Returns this object for method chaining.
      • omitInSource

        public MappingConfiguration<S,​D> omitInSource​(FieldSelector<S> sourceSelector)
        Marks a source field as omitted. The mapping will not touch this field in the source object.
        Parameters:
        sourceSelector - The FieldSelector lambda that selects the field with invoking the corresponding getter method.
        Returns:
        Returns this object for method chaining.
      • reassign

        public <RS> ReassignBuilder<S,​D> reassign​(FieldSelector<S> sourceSelector)
        Reassigns a property from the source to the specified property of the destination object.
        Parameters:
        sourceSelector - The FieldSelectors selecting the source property with get-method invocation.
        Returns:
        Returns a ReassignBuilder to specify the destination field.
      • replace

        public <RD,​RS> ReplaceBuilder<S,​D,​RD,​RS> replace​(TypedSelector<RS,​S> sourceSelector,
                                                                                 TypedSelector<RD,​D> destinationSelector)
        Maps a property from the source to the specified property of the destination object with transforming the source value using the specified transform lambda. Note: The mapping library is designed to reduce the required client tests. Using this method requires the client to test the transformation function!
        Parameters:
        sourceSelector - The FieldSelectors selecting the source property with get-method invocation.
        destinationSelector - The FieldSelectors selecting the destination property with get-method invocation.
        Returns:
        Returns ReplaceBuilder to specify the transform function and null-strategy.
      • set

        public <RD> SetBuilder<S,​D,​RD> set​(TypedSelector<RD,​D> destinationSelector)
        Defines a custom source of a value that should be mapped to the specified property of the destination object. The custom source value is provided by a supplier lambda function. Note: The mapping library is designed to reduce the required client tests. Using this method requires the client to test the supplier lambda function!
        Parameters:
        destinationSelector - The FieldSelectors selecting the destination property with get-method invocation.
        Returns:
        Returns ReplaceBuilder to specify the transform function and null-strategy.
      • restructure

        public <RD> RestructureBuilder<S,​D,​RD> restructure​(TypedSelector<RD,​D> destinationSelector)
        Defines a restructuring operation to build an complex object to be assigned to the specified destination field by mapping from fields of the source type.
        Type Parameters:
        RD - The destination type.
        Parameters:
        destinationSelector - The FieldSelectors selecting the destination property with get-method invocation.
        Returns:
        Returns a RestructureBuilder for further configuration.
      • replaceCollection

        public <RD,​RS> ReplaceCollectionBuilder<S,​D,​RD,​RS> replaceCollection​(TypedSelector<Collection<RS>,​S> sourceSelector,
                                                                                                     TypedSelector<Collection<RD>,​D> destinationSelector)
        Maps a property holding a collection from the source to the specified property holding a collection of the destination object. The specified transform function will be applied on every item in the source value to convert to the specified destination type. Note: The mapping library is designed to reduce the required client tests. Using this method requires the client to test the transformation function!
        Parameters:
        sourceSelector - The FieldSelectors selecting the source property holding a Collection.
        destinationSelector - The FieldSelectors selecting the destination property holding a Collection.
        Returns:
        Returns ReplaceBuilder to specify the transform function and null-strategy.
      • addDestinationMapping

        protected void addDestinationMapping​(PropertyDescriptor destProperty,
                                             com.remondis.remap.Transformation setTransformation)
      • mapper

        public Mapper<S,​D> mapper()
        Returns the mapper configured with this builder.
        Returns:
        The mapper instance.
      • useMapper

        public MappingConfiguration<S,​D> useMapper​(Mapper<?,​?> mapper)
        Registers a configured mapper to this object that is to be used whenever a hierarchical mapping tries to map the specified types. Note: Only one mapper can be added for a combination of source and destination type!
        Parameters:
        mapper - A mapper
        Returns:
        Returns this MappingConfiguration object for further configuration.
      • useMapper

        public MappingConfiguration<S,​D> useMapper​(TypeMapping<?,​?> typeMapping)
        Registers a custom type conversion to this mapping that is to be used whenever a type mapping is required that is not defined by a replace operation. Note: Only one mapper/type converter can be added for a combination of source and destination type!
        Parameters:
        typeMapping - A TypeMapping.
        Returns:
        Returns this MappingConfiguration object for further configuration.
      • noImplicitMappings

        public MappingConfiguration<S,​D> noImplicitMappings()
        Disables the creation of implicit mappings, so that fields with the same name and same type are not mapped automatically any more. This requires the user to define the mappings explicitly using reassign(FieldSelector) or any other mapping operation.
        Returns:
        Returns this MappingConfiguration object for further configuration.
      • writeNullIfSourceIsNull

        public MappingConfiguration<S,​D> writeNullIfSourceIsNull()
        Configures the mapper to write null value if the source value is null. If not set the mapper skips mapping if the source value is null.
        Returns:
        Returns this MappingConfiguration object for further configuration.
      • useInternalMapper

        protected void useInternalMapper​(com.remondis.remap.InternalMapper<?,​?> interalMapper)
      • hasMapperFor

        public <S1,​D1> boolean hasMapperFor​(Class<S1> sourceType,
                                                  Class<D1> destinationType)
        Checks if the specified mapper is registered.
        Parameters:
        sourceType - The source type
        destinationType - the destination type
        Returns:
        Returns true if a mapper was registered for this type of conversion. Otherwise false is returned.
      • toString

        public String toString​(boolean detailed)
        Returns a string representation of this mapper.
        Parameters:
        detailed - If true the string will be more detailed.
        Returns:
        Returns a string representation of this mapper.
      • getMappers

        protected Map<com.remondis.remap.Projection<?,​?>,​com.remondis.remap.InternalMapper<?,​?>> getMappers()
        Returns:
        Returns the registered Mappers.
      • isWriteNull

        public boolean isWriteNull()