org.simpleflatmapper.map.mapper.FieldErrorHandlerGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-map Show documentation
Show all versions of sfm-map Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.simpleflatmapper.map.mapper;
import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.map.FieldKey;
import org.simpleflatmapper.map.FieldMapperErrorHandler;
import org.simpleflatmapper.map.getter.BooleanContextualGetter;
import org.simpleflatmapper.map.getter.ByteContextualGetter;
import org.simpleflatmapper.map.getter.CharacterContextualGetter;
import org.simpleflatmapper.map.getter.DoubleContextualGetter;
import org.simpleflatmapper.map.getter.ContextualGetter;
import org.simpleflatmapper.map.getter.FloatContextualGetter;
import org.simpleflatmapper.map.getter.IntContextualGetter;
import org.simpleflatmapper.map.getter.LongContextualGetter;
import org.simpleflatmapper.map.getter.ShortContextualGetter;
public class FieldErrorHandlerGetter implements ContextualGetter {
public final ContextualGetter super S, ? extends T> delegate;
public final FieldMapperErrorHandler super K> errorHandler;
public final K key;
public FieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate,
FieldMapperErrorHandler super K> errorHandler) {
super();
if (key == null) throw new IllegalArgumentException("key is null");
if (delegate == null) throw new IllegalArgumentException("delegate is null");
if (errorHandler == null) throw new IllegalArgumentException("errorHandler is null");
this.key = key;
this.delegate = delegate;
this.errorHandler = errorHandler;
}
@Override
public T get(S source, Context context) {
try {
return delegate.get(source, context);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, context);
return null;
}
}
@Override
public String toString() {
return "FieldErrorHandlerMapper{delegate=" + delegate + '}';
}
public static > ContextualGetter of(K key, ContextualGetter super S, ? extends T> delegate,
FieldMapperErrorHandler super K> errorHandler) {
if (delegate instanceof BooleanContextualGetter) {
return new BooleanFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof ByteContextualGetter) {
return new ByteFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof CharacterContextualGetter) {
return new CharFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof ShortContextualGetter) {
return new ShortFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof IntContextualGetter) {
return new IntFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof LongContextualGetter) {
return new LongFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof FloatContextualGetter) {
return new FloatFieldErrorHandlerGetter(key, delegate, errorHandler);
}
if (delegate instanceof DoubleContextualGetter) {
return new DoubleFieldErrorHandlerGetter(key, delegate, errorHandler);
}
return new FieldErrorHandlerGetter(key, delegate, errorHandler);
}
private static class BooleanFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements BooleanContextualGetter {
private BooleanContextualGetter pGetter;
public BooleanFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (BooleanContextualGetter) delegate;
}
@Override
public boolean getBoolean(T source, Context mappingContext) throws Exception {
try {
return pGetter.getBoolean(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return false;
}
}
}
private static class ByteFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements ByteContextualGetter {
private ByteContextualGetter pGetter;
public ByteFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (ByteContextualGetter) delegate;
}
@Override
public byte getByte(T source, Context mappingContext) throws Exception {
try {
return pGetter.getByte(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class CharFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements CharacterContextualGetter {
private CharacterContextualGetter pGetter;
public CharFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (CharacterContextualGetter) delegate;
}
@Override
public char getCharacter(T source, Context mappingContext) throws Exception {
try {
return pGetter.getCharacter(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class ShortFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements ShortContextualGetter {
private ShortContextualGetter pGetter;
public ShortFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (ShortContextualGetter) delegate;
}
@Override
public short getShort(T source, Context mappingContext) throws Exception {
try {
return pGetter.getShort(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class IntFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements IntContextualGetter {
private IntContextualGetter pGetter;
public IntFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (IntContextualGetter) delegate;
}
@Override
public int getInt(T source, Context mappingContext) throws Exception {
try {
return pGetter.getInt(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class LongFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements LongContextualGetter {
private LongContextualGetter pGetter;
public LongFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (LongContextualGetter) delegate;
}
@Override
public long getLong(T source, Context mappingContext) throws Exception {
try {
return pGetter.getLong(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class FloatFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements FloatContextualGetter {
private FloatContextualGetter pGetter;
public FloatFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (FloatContextualGetter) delegate;
}
@Override
public float getFloat(T source, Context mappingContext) throws Exception {
try {
return pGetter.getFloat(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
private static class DoubleFieldErrorHandlerGetter extends FieldErrorHandlerGetter implements DoubleContextualGetter {
private DoubleContextualGetter pGetter;
public DoubleFieldErrorHandlerGetter(K key, ContextualGetter super S, ? extends T> delegate, FieldMapperErrorHandler super K> errorHandler) {
super(key, delegate, errorHandler);
this.pGetter = (DoubleContextualGetter) delegate;
}
@Override
public double getDouble(T source, Context mappingContext) throws Exception {
try {
return pGetter.getDouble(source, mappingContext);
} catch(Exception e) {
errorHandler.errorMappingField(key, source, null, e, mappingContext);
return 0;
}
}
}
}