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

org.simpleflatmapper.map.getter.ContextualGetterBiFunction 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.getter;

import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.ErrorHelper;

public class ContextualGetterBiFunction implements BiFunction {
    private final ContextualGetter fieldMapperGetter;

    public ContextualGetterBiFunction(ContextualGetter fieldMapperGetter) {
        this.fieldMapperGetter = fieldMapperGetter;
    }

    public static  BiFunction, T> of(Class type, ContextualGetter getter) {
        if (type.isPrimitive()) {
            if (boolean.class.equals(type) && getter instanceof BooleanContextualGetter) {
                final BooleanContextualGetter pGetter = (BooleanContextualGetter) getter;
                return (BiFunction, T>) new BooleanBiFunction(pGetter);
            }

            if (byte.class.equals(type) && getter instanceof ByteContextualGetter) {
                final ByteContextualGetter pGetter = (ByteContextualGetter) getter;
                return (BiFunction, T>) new ByteBiFunction(pGetter);
            }

            if (char.class.equals(type) && getter instanceof CharacterContextualGetter) {
                final CharacterContextualGetter pGetter = (CharacterContextualGetter) getter;
                return (BiFunction, T>) new CharacterBiFunction(pGetter);
            }

            if (short.class.equals(type) && getter instanceof ShortContextualGetter) {
                final ShortContextualGetter pGetter = (ShortContextualGetter) getter;
                return (BiFunction, T>) new ShortBiFunction(pGetter);
            }

            if (int.class.equals(type) && getter instanceof IntContextualGetter) {
                final IntContextualGetter pGetter = (IntContextualGetter) getter;
                return (BiFunction, T>) new IntegerBiFunction(pGetter);
            }

            if (long.class.equals(type) && getter instanceof LongContextualGetter) {
                final LongContextualGetter pGetter = (LongContextualGetter) getter;
                return (BiFunction, T>) new LongBiFunction(pGetter);
            }

            if (float.class.equals(type) && getter instanceof FloatContextualGetter) {
                final FloatContextualGetter pGetter = (FloatContextualGetter) getter;
                return (BiFunction, T>) new FloatBiFunction(pGetter);
            }

            if (double.class.equals(type) && getter instanceof DoubleContextualGetter) {
                final DoubleContextualGetter pGetter = (DoubleContextualGetter) getter;
                return (BiFunction, T>) new DoubleBiFunction(pGetter);
            }
            
        } 
        
        return new ContextualGetterBiFunction(getter);
    }

    @Override
    public T apply(S s, Context mappingContext) {
        try {
            return fieldMapperGetter.get(s, mappingContext);
        } catch (Exception e) {
            return ErrorHelper.rethrow(e);
        }
    }

    public static final class BooleanBiFunction implements BiFunction, Boolean> {
        private final BooleanContextualGetter pGetter;

        public BooleanBiFunction(BooleanContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Boolean apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getBoolean(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class ByteBiFunction implements BiFunction, Byte> {
        private final ByteContextualGetter pGetter;

        public ByteBiFunction(ByteContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Byte apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getByte(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class CharacterBiFunction implements BiFunction, Character> {
        private final CharacterContextualGetter pGetter;

        public CharacterBiFunction(CharacterContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Character apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getCharacter(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class ShortBiFunction implements BiFunction, Short> {
        private final ShortContextualGetter pGetter;

        public ShortBiFunction(ShortContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Short apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getShort(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class IntegerBiFunction implements BiFunction, Integer> {
        private final IntContextualGetter pGetter;

        public IntegerBiFunction(IntContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Integer apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getInt(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class LongBiFunction implements BiFunction, Long> {
        private final LongContextualGetter pGetter;

        public LongBiFunction(LongContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Long apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getLong(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class FloatBiFunction implements BiFunction, Float> {
        private final FloatContextualGetter pGetter;

        public FloatBiFunction(FloatContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Float apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getFloat(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }

    public static final class DoubleBiFunction implements BiFunction, Double> {
        private final DoubleContextualGetter pGetter;

        public DoubleBiFunction(DoubleContextualGetter pGetter) {
            this.pGetter = pGetter;
        }

        @Override
        public Double apply(S s, MappingContext mappingContext) {
            try {
                return pGetter.getDouble(s, mappingContext);
            } catch (Exception e) {
                return ErrorHelper.rethrow(e);
            }
        }
    }
}