org.sfm.map.impl.fieldmapper.MapperFieldMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.map.impl.fieldmapper;
import org.sfm.map.Mapper;
import org.sfm.map.FieldMapper;
import org.sfm.map.MappingContext;
import org.sfm.reflect.Getter;
import org.sfm.reflect.Setter;
import org.sfm.utils.BooleanProvider;
import org.sfm.utils.Predicate;
public final class MapperFieldMapper implements FieldMapper {
private final Mapper mapper;
private final Setter propertySetter;
private final Getter propertyGetter;
private final Predicate nullChecker;
private final Getter, BooleanProvider> breakDetectorProvider;
public MapperFieldMapper(Mapper mapper, Setter propertySetter, Getter propertyGetter, Predicate nullChecker, Getter, BooleanProvider> breakDetectorProvider) {
if (mapper == null) {
throw new NullPointerException("mapper is null");
}
if (propertySetter == null) {
throw new NullPointerException("propertySetter is null");
}
if (propertyGetter == null) {
throw new NullPointerException("propertyGetter is null");
}
if (nullChecker == null) {
throw new NullPointerException("nullCheck is null");
}
if (breakDetectorProvider == null) {
throw new NullPointerException("breakDetectorProvider is null");
}
this.mapper = mapper;
this.propertySetter = propertySetter;
this.propertyGetter = propertyGetter;
this.nullChecker = nullChecker;
this.breakDetectorProvider = breakDetectorProvider;
}
@Override
public void mapTo(final S source, final T target, final MappingContext context) throws Exception {
if (nullChecker.test(source)){
return;
}
boolean isBroken = breakDetectorProvider.get(context).getBoolean();
if (isBroken) {
propertySetter.set(target, mapper.map(source, context));
} else {
P value = propertyGetter.get(target);
if (value != null) {
mapper.mapTo(source, value, context);
}
}
}
@Override
public String toString() {
return "MapperFieldMapper{" +
"mapper=" + mapper +
", propertySetter=" + propertySetter +
", propertyGetter=" + propertyGetter +
'}';
}
}