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

com.github.rkmk.mapper.FieldMapperFactories Maven / Gradle / Ivy

package com.github.rkmk.mapper;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.util.Objects.nonNull;

public class FieldMapperFactories {

    private List factories = new ArrayList<>();

    public FieldMapperFactories() {
        factories.add(new BooleanMapperFactory());
        factories.add(new ByteMapperFactory());
        factories.add(new ShortMapperFactory());
        factories.add(new IntegerMapperFactory());
        factories.add(new IntegerArrayMapperFactory());
        factories.add(new IntArrayMapperFactory());
        factories.add(new LongMapperFactory());
        factories.add(new LongObjectArrayMapperFactory());
        factories.add(new LongArrayMapperFactory());
        factories.add(new FloatMapperFactory());
        factories.add(new FloatObjectArrayMapperFactory());
        factories.add(new FloatArrayMapperFactory());
        factories.add(new DoubleMapperFactory());
        factories.add(new DoubleObjectArrayMapperFactory());
        factories.add(new DoubleArrayMapperFactory());
        factories.add(new BigDecimalMapperFactory());
        factories.add(new BigDecimalArrayMapperFactory());
        factories.add(new TimestampMapperFactory());
        factories.add(new TimeMapperFactory());
        factories.add(new DateMapperFactory());
        factories.add(new StringMapperFactory());
        factories.add(new StringArrayMapperFactory());
        factories.add(new EnumMapperFactory());
        factories.add(new ObjectMapperFactory());
    }

    public List getValues() {
        return factories;
    }


    public static class BooleanMapperFactory implements FieldMapperFactory{
        @Override
        public Boolean getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getBoolean(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class);
        }
    }

    public static class ByteMapperFactory implements FieldMapperFactory{
        @Override
        public Byte getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getByte(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Byte.class) || type.isAssignableFrom(byte.class);
        }
    }

    public static class ShortMapperFactory implements FieldMapperFactory{
        @Override
        public Short getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getShort(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Short.class) || type.isAssignableFrom(short.class);
        }
    }

    public static class IntegerMapperFactory implements FieldMapperFactory{
        @Override
        public Integer getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getInt(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Integer.class) || type.isAssignableFrom(int.class);
        }
    }

    public static class LongMapperFactory implements FieldMapperFactory{
        @Override
        public Long getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getLong(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Long.class) || type.isAssignableFrom(long.class);
        }
    }

    public static class FloatMapperFactory implements FieldMapperFactory{
        @Override
        public Float getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getFloat(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Float.class) || type.isAssignableFrom(float.class);
        }
    }

    public static class DoubleMapperFactory implements FieldMapperFactory{
        @Override
        public Double getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getDouble(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Double.class) || type.isAssignableFrom(double.class);
        }
    }

    public static class BigDecimalMapperFactory implements FieldMapperFactory{
        @Override
        public BigDecimal getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getBigDecimal(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(BigDecimal.class);
        }
    }

    public static class TimestampMapperFactory implements FieldMapperFactory{
        @Override
        public Timestamp getValue(ResultSet rs, int index, Class type) throws SQLException {
            return getNullOrValue(type,rs, rs.getTimestamp(index));
        }

        @Override
        public Boolean accepts(Class type) {
            return type.isAssignableFrom(Timestamp.class);
        }
    }

    public static class TimeMapperFactory implements FieldMapperFactory