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

org.simpleflatmapper.map.mapper.FieldErrorHandlerGetter 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.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 delegate;
	public final FieldMapperErrorHandler errorHandler;
	public final K key;
	
	public FieldErrorHandlerGetter(K key, ContextualGetter delegate,
                                   FieldMapperErrorHandler 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 delegate,
																		   FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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 delegate, FieldMapperErrorHandler 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;
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy