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

org.simpleflatmapper.map.mapper.AbstractMapper 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.mapper;

import org.simpleflatmapper.map.SourceFieldMapper;
import org.simpleflatmapper.map.context.MappingContextFactory;
import org.simpleflatmapper.reflect.BiInstantiator;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.util.ErrorHelper;

public abstract class AbstractMapper implements SourceFieldMapper {
	
	private final BiInstantiator, T> instantiator;

	public AbstractMapper(final BiInstantiator, T> instantiator) {
		this.instantiator = instantiator;
    }
    
    
    @Override
	public final T map(final S source, final MappingContext mappingContext) throws MappingException {
		try {
			final T target = instantiator.newInstance(source, mappingContext);
			mapFields(source, target, mappingContext);
			return target;
		} catch(Exception e) {
            return ErrorHelper.rethrow(e);
		}
	}

    @Override
    public final void mapTo(final S source, final T target, final MappingContext mappingContext) throws MappingException {
        try {
            mapToFields(source, target, mappingContext);
        } catch(Exception e) {
            ErrorHelper.rethrow(e);
        }
    }

    protected abstract void mapFields(final S source, final T target, final MappingContext mappingContext) throws Exception;

    protected abstract void mapToFields(final S source, final T target, final MappingContext mappingContext) throws Exception;

    protected void appendToStringBuilder(StringBuilder sb) {
        sb.append("instantiator=").append(String.valueOf(instantiator));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy