com.remondis.remap.ReplaceBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of remap Show documentation
Show all versions of remap Show documentation
A declarative mapping library for converting objects field by field.
package com.remondis.remap;
import static com.remondis.remap.Lang.denyNull;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import com.remondis.propertypath.api.PropertyPath;
/**
* Builds a replace operation.
*
* @param The source type.
* @param The destination type.
* @param The source field type.
* @param The destination field type.
*
*/
public class ReplaceBuilder {
static final String TRANSFORM = "transform";
private TypedPropertyDescriptor sourceProperty;
private TypedPropertyDescriptor destProperty;
private MappingConfiguration mapping;
ReplaceBuilder(TypedPropertyDescriptor sourceProperty, TypedPropertyDescriptor destProperty,
MappingConfiguration mapping) {
super();
this.sourceProperty = sourceProperty;
this.destProperty = destProperty;
this.mapping = mapping;
}
/**
* Maps the source field by evaluating the specified property path. A property path is useful for flattening objects
* and the easy handling of optional values in a Java Bean object graph. The mapping will only be performed if the
* property path
* evaluates to a non-null value. Otherwise the mapping will be skipped.
*
*
* What is a property path
*
* A property path is a chain of get calls. Java Bean compliant get methods
* as well as {@link List#get(int)} and {@link Map#get(Object)} are supported.
* The get call chain evaluation and the null checks between the get calls are performed by the framework to support
* optional fields while avoiding the need of implementing null checks. The property path only evaluates to a non-null
* value, if all get calls return a non-null value.
*
*
*
* The property path will be evaluated during the mapping and every get-call is checked for a
* null
value. If a get call in the chain returns a null
value, the whole evaluation returns
* no value. In this case the mapping will be skipped.
*
*
* @param propertyPath A lambda function performing get calls on the specified object to declare the actual property
* path.
* This is not a function operating on real object. So do not manipulate or calculate here!
* @return Returns the {@link MappingConfiguration} for further mapping configuration.
*/
public MappingConfiguration withPropertyPath(PropertyPath propertyPath) {
denyNull("propertyPath", propertyPath);
PropertyPathTransformation replace = new PropertyPathTransformation(mapping,
sourceProperty.property, destProperty.property, propertyPath);
mapping.addMapping(sourceProperty.property, destProperty.property, replace);
return mapping;
}
/**
* Works exactly like {@link #withPropertyPath(PropertyPath)} but accepts a transformation function, that is to be
* specified on the
* returned builder. The function will be applied to the result of the property path evaluation, but only if the
* property path evaluates to a non-null value. If the specified transform function itself returns
* null
the property path evaluates to no value.
*
* @param propertyPath A lambda function performing get calls on the specified object to declare the actual property
* path.
* This is not a function operating on real object. So do not manipulate or calculate here!
* @return Returns the {@link PropertyPathAndApplyBuilder} to specify the transformation function.
*/
public PropertyPathAndApplyBuilder withPropertyPathAnd(
PropertyPath propertyPath) {
denyNull("propertyPath", propertyPath);
return new PropertyPathAndApplyBuilder(mapping, sourceProperty, destProperty, propertyPath);
}
/**
* Transforms the selected fields with applying the specified transform function on the source value. Note: The
* transform function must check the source value for null
itself. Use {@link
* #withSkipWhenNull(Function)} to skip on null
input values.
*
* @param transformation The transform function.
* @return Returns the {@link MappingConfiguration} for further mapping configuration.
*/
public MappingConfiguration with(Function transformation) {
denyNull("tranformation", transformation);
ReplaceTransformation replace = new ReplaceTransformation<>(mapping, sourceProperty.property,
destProperty.property, transformation, false);
mapping.addMapping(sourceProperty.property, destProperty.property, replace);
return mapping;
}
/**
* Transforms the selected fields with applying the specified transform function on the source value. This method
* skips the execution of the transform function if the source value is null.
*
* @param transformation The transform function.
* @return Returns the {@link MappingConfiguration} for further mapping configuration.
*/
public MappingConfiguration withSkipWhenNull(Function transformation) {
denyNull("tranformation", transformation);
ReplaceTransformation replace = new ReplaceTransformation<>(mapping, sourceProperty.property,
destProperty.property, transformation, true);
mapping.addMapping(sourceProperty.property, destProperty.property, replace);
return mapping;
}
}