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

com.wizarius.orm.database.handlers.ReadableHandlers Maven / Gradle / Ivy

There is a newer version: 0.0.27.6
Show newest version
package com.wizarius.orm.database.handlers;

import com.wizarius.orm.database.entityreader.DBParsedField;
import com.wizarius.orm.database.entityreader.DBSupportedTypes;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.HashMap;

/**
 * Created by Vladyslav Shyshkin on 29.12.2017.
 * Hash map where key its type of object and value its handler
 */
public class ReadableHandlers extends HashMap {
    /**
     * Constructs an empty HashMap with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public ReadableHandlers() {
        initializeDefaults();
    }

    /**
     * Initialize default handlers
     */
    @SuppressWarnings("unchecked")
    public void initializeDefaults() {
        this.put(DBSupportedTypes.BYTE, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setByte(instance, rs.getByte(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setByte(instance, rs.getByte(columnIndex));
            }
        });
        this.put(DBSupportedTypes.SHORT, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setShort(instance, rs.getShort(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setShort(instance, rs.getShort(columnIndex));
            }
        });
        this.put(DBSupportedTypes.INTEGER, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setInt(instance, rs.getInt(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setInt(instance, rs.getInt(columnIndex));
            }
        });
        this.put(DBSupportedTypes.LONG, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setLong(instance, rs.getLong(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setLong(instance, rs.getLong(columnIndex));
            }
        });
        this.put(DBSupportedTypes.FLOAT, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setFloat(instance, rs.getFloat(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setFloat(instance, rs.getFloat(columnIndex));
            }
        });
        this.put(DBSupportedTypes.DOUBLE, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setDouble(instance, rs.getDouble(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setDouble(instance, rs.getDouble(columnIndex));
            }
        });
        this.put(DBSupportedTypes.BIGDECIMAL, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                BigDecimal value = rs.getBigDecimal(resultSetFieldName);
                field.set(instance, value);
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                BigDecimal value = rs.getBigDecimal(columnIndex);
                field.set(instance, value);
            }
        });
        this.put(DBSupportedTypes.CHAR, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                String string = rs.getString(resultSetFieldName);
                if (string != null && string.length() != 0) {
                    field.setChar(instance, string.charAt(0));
                }
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                String string = rs.getString(columnIndex);
                if (string != null && string.length() != 0) {
                    field.setChar(instance, string.charAt(0));
                }
            }
        });
        this.put(DBSupportedTypes.STRING, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.set(instance, rs.getString(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.set(instance, rs.getString(columnIndex));
            }
        });
        this.put(DBSupportedTypes.BOOLEAN, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.setBoolean(instance, rs.getBoolean(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.setBoolean(instance, rs.getBoolean(columnIndex));
            }
        });
        this.put(DBSupportedTypes.BYTE_ARRAY, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                field.set(instance, rs.getBytes(resultSetFieldName));
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                field.set(instance, rs.getBytes(columnIndex));
            }
        });
        this.put(DBSupportedTypes.ENUM, new ReadableHandler() {
            @Override
            public void set(Field field, Object instance, ResultSet rs, String resultSetFieldName, DBParsedField parsedField) throws Exception {
                String value = rs.getString(resultSetFieldName);
                if (value != null) {
                    Enum enumValue = Enum.valueOf((Class) field.getType(), value);
                    field.set(instance, enumValue);
                }
            }

            @Override
            public void set(Field field, Object instance, ResultSet rs, int columnIndex, DBParsedField parsedField) throws Exception {
                String value = rs.getString(columnIndex);
                if (value != null) {
                    Enum enumValue = Enum.valueOf((Class) field.getType(), value);
                    field.set(instance, enumValue);
                }
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy