com.remondis.remap.ReplaceAssertBuilder 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.function.Function;
import com.remondis.propertypath.api.PropertyPath;
/**
* Builder to assert a replace operation on a {@link Mapper} object using {@link AssertConfiguration}.
*
* @param The source type
* @param The destination type
* @param The type of the source field
* @param The type of the destination field
* @author schuettec
*/
public class ReplaceAssertBuilder {
private TypedPropertyDescriptor sourceProperty;
private TypedPropertyDescriptor destProperty;
private AssertConfiguration asserts;
ReplaceAssertBuilder(TypedPropertyDescriptor sourceProperty, TypedPropertyDescriptor destProperty,
AssertConfiguration asserts) {
super();
this.sourceProperty = sourceProperty;
this.destProperty = destProperty;
this.asserts = asserts;
}
/**
* Expects the mapping to evaluate the exact property path that was specified.
*
* @param propertyPath The expected property path.
* @return Returns the {@link AssertConfiguration} for further configuration.
*/
public AssertConfiguration withPropertyPath(PropertyPath propertyPath) {
denyNull("propertyPath", propertyPath);
PropertyPathTransformation replace = new PropertyPathTransformation(asserts.getMapping(),
sourceProperty.property, destProperty.property, propertyPath);
asserts.addAssertion(replace);
return asserts;
}
/**
* Expects the mapping to evaluate the exact property path that was specified.
*
* @param propertyPath The expected property path.
* @return Returns the {@link AssertConfiguration} for further configuration.
*/
public AssertConfiguration withPropertyPathAndTransformation(PropertyPath, RS, ?> propertyPath) {
denyNull("propertyPath", propertyPath);
PropertyPathTransformation replace = new PropertyPathTransformation<>(asserts.getMapping(),
sourceProperty.property, destProperty.property, propertyPath, Function.identity());
asserts.addAssertion(replace);
return asserts;
}
/**
* Expects the mapping to not skip the transform function on null
input. The specified transform
* function will be checked against null
input.
*
*
* Note: This method cannot reliably check that the specified function is actually the function that was configured on
* the mapping. This method only verifies the skip-on-null behaviour and performs a null
check on the
* specified function.
*
*
* @param transformation The transformation to test.
* @return Returns the {@link AssertConfiguration} for further configuration.
*/
public AssertConfiguration andTest(Function transformation) {
denyNull("tranfromation", transformation);
ReplaceTransformation replace = new ReplaceTransformation<>(asserts.getMapping(), sourceProperty.property,
destProperty.property, transformation, false);
asserts.addAssertion(replace);
return asserts;
}
/**
* Expects the mapping to skip the transform function on null
input.
*
* @return Returns the {@link AssertConfiguration} for further configuration.
*/
public AssertConfiguration andSkipWhenNull() {
ReplaceTransformation replace = new ReplaceTransformation(asserts.getMapping(),
sourceProperty.property, destProperty.property, null, true);
asserts.addAssertion(replace);
return asserts;
}
}