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

org.simpleflatmapper.map.getter.ContextualGetterAdapter Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

The newest version!
package org.simpleflatmapper.map.getter;

import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.primitive.BooleanGetter;
import org.simpleflatmapper.reflect.primitive.ByteGetter;
import org.simpleflatmapper.reflect.primitive.CharacterGetter;
import org.simpleflatmapper.reflect.primitive.DoubleGetter;
import org.simpleflatmapper.reflect.primitive.FloatGetter;
import org.simpleflatmapper.reflect.primitive.IntGetter;
import org.simpleflatmapper.reflect.primitive.LongGetter;
import org.simpleflatmapper.reflect.primitive.ShortGetter;

public final class ContextualGetterAdapter implements ContextualGetter {
    
    private final Getter getter;

    public ContextualGetterAdapter(Getter getter) {
        this.getter = getter;
    }


    @Override
    public P get(S s, Context context) throws Exception {
        return getter.get(s);
    }


    public static  ContextualGetter of(Getter getter) {
        if (getter == null) return null;

        if (getter instanceof BooleanGetter) {
            return (ContextualGetter) new FieldMapperBooleanGetterAdapter((Getter) getter);
        }
        if (getter instanceof ByteGetter) {
            return (ContextualGetter) new FieldMapperByteGetterAdapter((Getter) getter);
        }
        if (getter instanceof CharacterGetter) {
            return (ContextualGetter) new FieldMapperCharacterGetterAdapter((Getter) getter);
        }
        if (getter instanceof ShortGetter) {
            return (ContextualGetter) new FieldMapperShortGetterAdapter((Getter) getter);
        }
        if (getter instanceof IntGetter) {
            return (ContextualGetter) new FieldMapperIntGetterAdapter((Getter) getter);
        }
        if (getter instanceof LongGetter) {
            return (ContextualGetter) new FieldMapperLongGetterAdapter((Getter) getter);
        }
        if (getter instanceof FloatGetter) {
            return (ContextualGetter) new FieldMapperFloatGetterAdapter((Getter) getter);
        }
        if (getter instanceof DoubleGetter) {
            return (ContextualGetter) new FieldMapperDoubleGetterAdapter((Getter) getter);
        }
        
        return new ContextualGetterAdapter(getter);
    }

    private static class FieldMapperBooleanGetterAdapter implements ContextualGetter, BooleanContextualGetter {
        private final Getter getter;
        private final BooleanGetter pGetter;

        public  FieldMapperBooleanGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (BooleanGetter) getter;
        }

        @Override
        public Boolean get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public boolean getBoolean(S s, Context mappingContext) throws Exception {
            return pGetter.getBoolean(s);
        }
    }

    private static class FieldMapperByteGetterAdapter implements ContextualGetter, ByteContextualGetter {
        private final Getter getter;
        private final ByteGetter pGetter;

        public  FieldMapperByteGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (ByteGetter) getter;
        }

        @Override
        public Byte get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public byte getByte(S s, Context mappingContext) throws Exception {
            return pGetter.getByte(s);
        }
    }

    private static class FieldMapperCharacterGetterAdapter implements ContextualGetter, CharacterContextualGetter {
        private final Getter getter;
        private final CharacterGetter pGetter;

        public  FieldMapperCharacterGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (CharacterGetter) getter;
        }

        @Override
        public Character get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public char getCharacter(S s, Context mappingContext) throws Exception {
            return pGetter.getCharacter(s);
        }
    }

    private static class FieldMapperShortGetterAdapter implements ContextualGetter, ShortContextualGetter {
        private final Getter getter;
        private final ShortGetter pGetter;

        public  FieldMapperShortGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (ShortGetter) getter;
        }

        @Override
        public Short get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public short getShort(S s, Context mappingContext) throws Exception {
            return pGetter.getShort(s);
        }
    }

    private static class FieldMapperIntGetterAdapter implements ContextualGetter, IntContextualGetter {
        private final Getter getter;
        private final IntGetter pGetter;

        public  FieldMapperIntGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (IntGetter) getter;
        }

        @Override
        public Integer get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public int getInt(S s, Context mappingContext) throws Exception {
            return pGetter.getInt(s);
        }
    }
    private static class FieldMapperLongGetterAdapter implements ContextualGetter, LongContextualGetter {
        private final Getter getter;
        private final LongGetter pGetter;

        public  FieldMapperLongGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (LongGetter) getter;
        }

        @Override
        public Long get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public long getLong(S s, Context mappingContext) throws Exception {
            return pGetter.getLong(s);
        }
    }

    private static class FieldMapperFloatGetterAdapter implements ContextualGetter, FloatContextualGetter {
        private final Getter getter;
        private final FloatGetter pGetter;

        public  FieldMapperFloatGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (FloatGetter) getter;
        }

        @Override
        public Float get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public float getFloat(S s, Context mappingContext) throws Exception {
            return pGetter.getFloat(s);
        }
    }
    private static class FieldMapperDoubleGetterAdapter implements ContextualGetter, DoubleContextualGetter {
        private final Getter getter;
        private final DoubleGetter pGetter;

        public  FieldMapperDoubleGetterAdapter(Getter getter) {
            this.getter = getter;
            this.pGetter = (DoubleGetter) getter;
        }

        @Override
        public Double get(S s, Context context) throws Exception {
            return getter.get(s);
        }

        @Override
        public double getDouble(S s, Context mappingContext) throws Exception {
            return pGetter.getDouble(s);
        }
    }



    public static  BooleanContextualGetter of(final BooleanGetter getter) {
        return new BooleanContextualGetterAdapter(getter);
    }
    
    private static class BooleanContextualGetterAdapter implements BooleanContextualGetter {
        private final BooleanGetter getter;

        public BooleanContextualGetterAdapter(BooleanGetter getter) {
            this.getter = getter;
        }

        @Override
        public boolean getBoolean(S s, Context mappingContext) throws Exception {
            return getter.getBoolean(s);
        }
    }
    public static  ByteContextualGetter of(final ByteGetter getter) {
        return new ByteContextualGetterAdapter(getter);
    }

    private static class ByteContextualGetterAdapter implements ByteContextualGetter {
        private final ByteGetter getter;

        public ByteContextualGetterAdapter(ByteGetter getter) {
            this.getter = getter;
        }

        @Override
        public byte getByte(S s, Context mappingContext) throws Exception {
            return getter.getByte(s);
        }
    }
    public static  CharacterContextualGetter of(final CharacterGetter getter) {
        return new CharacterContextualGetterAdapter(getter);
    }

    private static class CharacterContextualGetterAdapter implements CharacterContextualGetter {
        private final CharacterGetter getter;

        public CharacterContextualGetterAdapter(CharacterGetter getter) {
            this.getter = getter;
        }

        @Override
        public char getCharacter(S s, Context mappingContext) throws Exception {
            return getter.getCharacter(s);
        }
    }
    public static  ShortContextualGetter of(final ShortGetter getter) {
        return new ShortContextualGetterAdapter(getter);
    }

    private static class ShortContextualGetterAdapter implements ShortContextualGetter {
        private final ShortGetter getter;

        public ShortContextualGetterAdapter(ShortGetter getter) {
            this.getter = getter;
        }

        @Override
        public short getShort(S s, Context mappingContext) throws Exception {
            return getter.getShort(s);
        }
    }
    public static  IntContextualGetter of(final IntGetter getter) {
        return new IntContextualGetterAdapter(getter);
    }

    private static class IntContextualGetterAdapter implements IntContextualGetter {
        private final IntGetter getter;

        public IntContextualGetterAdapter(IntGetter getter) {
            this.getter = getter;
        }

        @Override
        public int getInt(S s, Context mappingContext) throws Exception {
            return getter.getInt(s);
        }
    }
    public static  LongContextualGetter of(final LongGetter getter) {
        return new LongContextualGetterAdapter(getter);
    }

    private static class LongContextualGetterAdapter implements LongContextualGetter {
        private final LongGetter getter;

        public LongContextualGetterAdapter(LongGetter getter) {
            this.getter = getter;
        }

        @Override
        public long getLong(S s, Context mappingContext) throws Exception {
            return getter.getLong(s);
        }
    }
    public static  FloatContextualGetter of(final FloatGetter getter) {
        return new FloatContextualGetterAdapter(getter);
    }

    private static class FloatContextualGetterAdapter implements FloatContextualGetter {
        private final FloatGetter getter;

        public FloatContextualGetterAdapter(FloatGetter getter) {
            this.getter = getter;
        }

        @Override
        public float getFloat(S s, Context mappingContext) throws Exception {
            return getter.getFloat(s);
        }
    }
    public static  DoubleContextualGetter of(final DoubleGetter getter) {
        return new DoubleContextualGetterAdapter(getter);
    }

    private static class DoubleContextualGetterAdapter implements DoubleContextualGetter {
        private final DoubleGetter getter;

        public DoubleContextualGetterAdapter(DoubleGetter getter) {
            this.getter = getter;
        }

        @Override
        public double getDouble(S s, Context mappingContext) throws Exception {
            return getter.getDouble(s);
        }
    }
}