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

org.simpleflatmapper.map.mapper.MapperFieldMapperGetterAdapter Maven / Gradle / Ivy

Go to download

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

The newest version!
package org.simpleflatmapper.map.mapper;

import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.getter.ContextualGetter;
import org.simpleflatmapper.util.Predicate;

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

public final class MapperFieldMapperGetterAdapter implements ContextualGetter {

	public final SourceMapper mapper;
    public final Predicate nullChecker;
    public final int valueIndex;
	
	public MapperFieldMapperGetterAdapter(SourceMapper mapper, Predicate nullChecker, int valueIndex) {
		this.mapper = requireNonNull("jdbcMapper", mapper);
        this.nullChecker = requireNonNull("nullChecker", nullChecker);
        this.valueIndex = valueIndex;
    }

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

    @Override
    public P get(S s, Context context) {
        if (nullChecker.test(s)){
            return null;
        }
        MappingContext mappingContext = (MappingContext) context;
        P value = mapper.map(s, mappingContext);
        if (mappingContext != null) {
            mappingContext.setCurrentValue(valueIndex, value);
        }
        return value;
    }
}