com.remondis.remap.SetTransformation 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.Properties.asString;
import java.beans.PropertyDescriptor;
import java.util.function.Function;
import javax.xml.crypto.dsig.Transform;
/**
* A replace transformation converts a source object into a destination object by applying the specified {@link
* Transform} function on the source.
*
* @param The input type
* @param The output type
* @author schuettec
*/
class SetTransformation extends Transformation {
private static final String MSG = "Set %s with a custom value supplier.";
private Function transformation;
SetTransformation(MappingConfiguration mapping, PropertyDescriptor destProperty,
Function transformation) {
super(mapping, null, destProperty);
this.transformation = transformation;
}
@Override
protected void performTransformation(PropertyDescriptor sourceProperty, Object source,
PropertyDescriptor destinationProperty, Object destination) throws MappingException {
MappedResult result = performValueTransformation(source, destination);
if (result.hasValue()) {
writeOrFail(destinationProperty, destination, result.getValue());
}
}
@Override
@SuppressWarnings("unchecked")
protected MappedResult performValueTransformation(Object source, Object destination) throws MappingException {
Object destinationValue = transformation.apply((S) source);
return MappedResult.value(destinationValue);
}
@Override
protected void validateTransformation() throws MappingException {
}
@Override
public String toString(boolean detailed) {
return String.format(MSG, asString(destinationProperty, detailed));
}
}