- java.lang.Object
-
- com.remondis.remap.MappingConfiguration<S,D>
-
- Type Parameters:
S- source type of the mappingD- destination type of the mapping
public class MappingConfiguration<S,D> extends Object
TheMappingConfigurationobject 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.
transientdo 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 usinguseMapper(Mapper), otherweise aMappingExceptionis thrown.- Author:
- schuettec
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddDestinationMapping(PropertyDescriptor destProperty, com.remondis.remap.Transformation setTransformation)protected voidaddMapping(PropertyDescriptor sourceProperty, PropertyDescriptor destProperty, com.remondis.remap.Transformation transformation)protected Map<com.remondis.remap.Projection<?,?>,com.remondis.remap.InternalMapper<?,?>>getMappers()<S1,D1>
booleanhasMapperFor(Class<S1> sourceType, Class<D1> destinationType)Checks if the specified mapper is registered.booleanisWriteNull()Mapper<S,D>mapper()Returns the mapper configured with this builder.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.MappingConfiguration<S,D>omitInDestination(FieldSelector<D> destinationSelector)Marks a destination field as omitted.MappingConfiguration<S,D>omitInSource(FieldSelector<S> sourceSelector)Marks a source field as omitted.MappingConfiguration<S,D>omitOtherDestinationProperties()Omits all unmapped destination fields.MappingConfiguration<S,D>omitOthers()Omits all unmapped source and destination fields.MappingConfiguration<S,D>omitOtherSourceProperties()Omits all unmapped source fields.<RS> ReassignBuilder<S,D>reassign(FieldSelector<S> sourceSelector)Reassigns a property from the source to the specified property of the destination object.<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.<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.<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.<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.StringtoString()StringtoString(boolean detailed)Returns a string representation of this mapper.protected voiduseInternalMapper(com.remondis.remap.InternalMapper<?,?> interalMapper)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.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.MappingConfiguration<S,D>writeNullIfSourceIsNull()Configures the mapper to writenullvalue if the source value is null.
-
-
-
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- TheFieldSelectorlambda 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 necessaryomitInDestination(FieldSelector)andomitInSource(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 useomitOthers()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 necessaryomitInDestination(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 useomitOtherDestinationProperties()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 necessaryomitInSource(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 useomitOtherSourceProperties()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- TheFieldSelectorlambda 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- TheFieldSelectors selecting the source property with get-method invocation.- Returns:
- Returns a
ReassignBuilderto 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- TheFieldSelectors selecting the source property with get-method invocation.destinationSelector- TheFieldSelectors selecting the destination property with get-method invocation.- Returns:
- Returns
ReplaceBuilderto 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- TheFieldSelectors selecting the destination property with get-method invocation.- Returns:
- Returns
ReplaceBuilderto 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- TheFieldSelectors selecting the destination property with get-method invocation.- Returns:
- Returns a
RestructureBuilderfor 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- TheFieldSelectors selecting the source property holding aCollection.destinationSelector- TheFieldSelectors selecting the destination property holding aCollection.- Returns:
- Returns
ReplaceBuilderto specify the transform function and null-strategy.
-
addMapping
protected void addMapping(PropertyDescriptor sourceProperty, PropertyDescriptor destProperty, com.remondis.remap.Transformation transformation)
-
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
MappingConfigurationobject 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- ATypeMapping.- Returns:
- Returns this
MappingConfigurationobject 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 usingreassign(FieldSelector)or any other mapping operation.- Returns:
- Returns this
MappingConfigurationobject for further configuration.
-
writeNullIfSourceIsNull
public MappingConfiguration<S,D> writeNullIfSourceIsNull()
Configures the mapper to writenullvalue if the source value is null. If not set the mapper skips mapping if the source value isnull.- Returns:
- Returns this
MappingConfigurationobject 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 typedestinationType- the destination type- Returns:
- Returns
trueif a mapper was registered for this type of conversion. Otherwisefalseis returned.
-
toString
public String toString(boolean detailed)
Returns a string representation of this mapper.- Parameters:
detailed- Iftruethe 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()
-
-