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

org.simpleflatmapper.map.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: 9.0.2
Show newest version
package org.simpleflatmapper.map.fieldmapper;

import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.util.Predicate;

import static org.simpleflatmapper.util.Asserts.requireNonNull;

public final class MapperFieldMapper & FieldMapper> implements FieldMapper {

	public final M mapper;
    public final Setter propertySetter;
    public final Predicate nullChecker;

    public final int currentValueIndex;

    public MapperFieldMapper(M mapper, Setter propertySetter, Predicate nullChecker, int currentValueIndex) {
        this.mapper = requireNonNull("jdbcMapper", mapper);
        this.propertySetter = requireNonNull("propertySetter", propertySetter);
        this.nullChecker = requireNonNull("nullChecker", nullChecker);
        this.currentValueIndex = currentValueIndex;
    }

    @SuppressWarnings("unchecked")
    @Override
	public void mapTo(final S source, final T target, final MappingContext context) throws Exception {
        if (nullChecker.test(source)){
            return;
        }

        P value = null;

        if (context != null) {
            value = (P) context.getCurrentValue(currentValueIndex);
        }

        if (value == null) {
            value = mapper.map(source, context);
            if (context != null) {
                context.setCurrentValue(currentValueIndex, value);
            }
            propertySetter.set(target, value);
        } else {
            mapper.mapTo(source, value, context);
        }
	}

    @Override
    public String toString() {
        return "MapperFieldMapper{" +
                "jdbcMapper=" + mapper +
                ", propertySetter=" + propertySetter +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy