All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.map.impl.fieldmapper.MapperFieldMapper Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
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 +
                '}';
    }
}