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

org.simpleflatmapper.map.setter.ContextualSetterAdapter 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.setter;

import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.primitive.BooleanSetter;
import org.simpleflatmapper.reflect.primitive.ByteSetter;
import org.simpleflatmapper.reflect.primitive.CharacterSetter;
import org.simpleflatmapper.reflect.primitive.DoubleSetter;
import org.simpleflatmapper.reflect.primitive.FloatSetter;
import org.simpleflatmapper.reflect.primitive.IntSetter;
import org.simpleflatmapper.reflect.primitive.LongSetter;
import org.simpleflatmapper.reflect.primitive.ShortSetter;

public class ContextualSetterAdapter implements ContextualSetter {
    private final Setter setter;

    private ContextualSetterAdapter(Setter setter) {
        this.setter = setter;
    }

    public static  ContextualSetter of(Setter setter) {
        if (setter == null) return null;
        
        if (setter instanceof BooleanSetter) {
            return (ContextualSetter) new BooleanContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof ByteSetter) {
            return (ContextualSetter) new ByteContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof CharacterSetter) {
            return (ContextualSetter) new CharacterContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof ShortSetter) {
            return (ContextualSetter) new ShortContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof IntSetter) {
            return (ContextualSetter) new IntegerContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof LongSetter) {
            return (ContextualSetter) new LongContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof FloatSetter) {
            return (ContextualSetter) new FloatContextualSetterAdapter((Setter)setter);
        }
        if (setter instanceof DoubleSetter) {
            return (ContextualSetter) new DoubleContextualSetterAdapter((Setter)setter);
        }
        return new ContextualSetterAdapter(setter);
    }
    @Override
    public void set(T target, P value, Context context) throws Exception {
        setter.set(target, value);
    }
    
    private static class BooleanContextualSetterAdapter implements ContextualSetter, BooleanContextualSetter {
        private final Setter setter;
        private final BooleanSetter psetter;
        
        private BooleanContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (BooleanSetter) setter;
        }


        @Override
        public void setBoolean(T target, boolean value, Context context) throws Exception {
            psetter.setBoolean(target, value);
        }

        @Override
        public void set(T target, Boolean value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class ByteContextualSetterAdapter implements ContextualSetter, ByteContextualSetter {
        private final Setter setter;
        private final ByteSetter psetter;

        private ByteContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (ByteSetter) setter;
        }


        @Override
        public void setByte(T target, byte value, Context context) throws Exception {
            psetter.setByte(target, value);
        }

        @Override
        public void set(T target, Byte value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class CharacterContextualSetterAdapter implements ContextualSetter, CharacterContextualSetter {
        private final Setter setter;
        private final CharacterSetter psetter;

        private CharacterContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (CharacterSetter) setter;
        }


        @Override
        public void setCharacter(T target, char value, Context context) throws Exception {
            psetter.setCharacter(target, value);
        }

        @Override
        public void set(T target, Character value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class ShortContextualSetterAdapter implements ContextualSetter, ShortContextualSetter {
        private final Setter setter;
        private final ShortSetter psetter;

        private ShortContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (ShortSetter) setter;
        }


        @Override
        public void setShort(T target, short value, Context context) throws Exception {
            psetter.setShort(target, value);
        }

        @Override
        public void set(T target, Short value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class IntegerContextualSetterAdapter implements ContextualSetter, IntContextualSetter {
        private final Setter setter;
        private final IntSetter psetter;

        private IntegerContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (IntSetter) setter;
        }


        @Override
        public void setInt(T target, int value, Context context) throws Exception {
            psetter.setInt(target, value);
        }

        @Override
        public void set(T target, Integer value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class LongContextualSetterAdapter implements ContextualSetter, LongContextualSetter {
        private final Setter setter;
        private final LongSetter psetter;

        private LongContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (LongSetter) setter;
        }


        @Override
        public void setLong(T target, long value, Context context) throws Exception {
            psetter.setLong(target, value);
        }

        @Override
        public void set(T target, Long value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class FloatContextualSetterAdapter implements ContextualSetter, FloatContextualSetter {
        private final Setter setter;
        private final FloatSetter psetter;

        private FloatContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (FloatSetter) setter;
        }


        @Override
        public void setFloat(T target, float value, Context context) throws Exception {
            psetter.setFloat(target, value);
        }

        @Override
        public void set(T target, Float value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    private static class DoubleContextualSetterAdapter implements ContextualSetter, DoubleContextualSetter {
        private final Setter setter;
        private final DoubleSetter psetter;

        private DoubleContextualSetterAdapter(Setter setter) {
            this.setter = setter;
            this.psetter = (DoubleSetter) setter;
        }


        @Override
        public void setDouble(T target, double value, Context context) throws Exception {
            psetter.setDouble(target, value);
        }

        @Override
        public void set(T target, Double value, Context context) throws Exception {
            setter.set(target, value);
        }
    }
    

    public static  BooleanContextualSetter of(final BooleanSetter setter) {
        return new PrimitiveBooleanContextualSetterAdapter(setter);
    }

    private static class PrimitiveBooleanContextualSetterAdapter implements BooleanContextualSetter {
        private final BooleanSetter setter;

        public PrimitiveBooleanContextualSetterAdapter(BooleanSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setBoolean(T target, boolean value, Context context) throws Exception {
            setter.setBoolean(target, value);
        }
    }

    public static  ByteContextualSetter of(final ByteSetter setter) {
        return new PrimitiveByteContextualSetterAdapter(setter);
    }

    private static class PrimitiveByteContextualSetterAdapter implements ByteContextualSetter {
        private final ByteSetter setter;

        public PrimitiveByteContextualSetterAdapter(ByteSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setByte(T target, byte value, Context context) throws Exception {
            setter.setByte(target, value);
        }
    }

    public static  ShortContextualSetter of(final ShortSetter setter) {
        return new PrimitiveShortContextualSetterAdapter(setter);
    }

    private static class PrimitiveShortContextualSetterAdapter implements ShortContextualSetter {
        private final ShortSetter setter;

        public PrimitiveShortContextualSetterAdapter(ShortSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setShort(T target, short value, Context context) throws Exception {
            setter.setShort(target, value);
        }
    }

    public static  IntContextualSetter of(final IntSetter setter) {
        return new PrimitiveIntContextualSetterAdapter(setter);
    }

    private static class PrimitiveIntContextualSetterAdapter implements IntContextualSetter {
        private final IntSetter setter;

        public PrimitiveIntContextualSetterAdapter(IntSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setInt(T target, int value, Context context) throws Exception {
            setter.setInt(target, value);
        }
    }


    public static  LongContextualSetter of(final LongSetter setter) {
        return new PrimitiveLongContextualSetterAdapter(setter);
    }

    private static class PrimitiveLongContextualSetterAdapter implements LongContextualSetter {
        private final LongSetter setter;

        public PrimitiveLongContextualSetterAdapter(LongSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setLong(T target, long value, Context context) throws Exception {
            setter.setLong(target, value);
        }
    }


    public static  FloatContextualSetter of(final FloatSetter setter) {
        return new PrimitiveFloatContextualSetterAdapter(setter);
    }

    private static class PrimitiveFloatContextualSetterAdapter implements FloatContextualSetter {
        private final FloatSetter setter;

        public PrimitiveFloatContextualSetterAdapter(FloatSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setFloat(T target, float value, Context context) throws Exception {
            setter.setFloat(target, value);
        }
    }


    public static  DoubleContextualSetter of(final DoubleSetter setter) {
        return new PrimitiveDoubleContextualSetterAdapter(setter);
    }

    private static class PrimitiveDoubleContextualSetterAdapter implements DoubleContextualSetter {
        private final DoubleSetter setter;

        public PrimitiveDoubleContextualSetterAdapter(DoubleSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setDouble(T target, double value, Context context) throws Exception {
            setter.setDouble(target, value);
        }
    }


    public static  CharacterContextualSetter of(final CharacterSetter setter) {
        return new PrimitiveCharacterContextualSetterAdapter(setter);
    }

    private static class PrimitiveCharacterContextualSetterAdapter implements CharacterContextualSetter {
        private final CharacterSetter setter;

        public PrimitiveCharacterContextualSetterAdapter(CharacterSetter setter) {
            this.setter = setter;
        }

        @Override
        public void setCharacter(T target, char value, Context context) throws Exception {
            setter.setCharacter(target, value);
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy