org.sfm.map.impl.AbstractMapper 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;
import org.sfm.map.*;
import org.sfm.reflect.Instantiator;
import org.sfm.utils.ErrorHelper;
public abstract class AbstractMapper implements Mapper {
private final Instantiator instantiator;
private final MappingContextFactory mappingContextFactory;
public AbstractMapper(final Instantiator instantiator, MappingContextFactory mappingContextFactory) {
this.instantiator = instantiator;
this.mappingContextFactory = mappingContextFactory;
}
@Override
public final T map(final S source) throws MappingException {
return map(source, null);
}
@Override
public final T map(final S source, final MappingContext mappingContext) throws MappingException {
try {
final T target = instantiator.newInstance(source);
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);
}
}
@Override
public final MappingContext newMappingContext(S source) {
return mappingContextFactory.newContext();
}
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));
}
}