TypeConverterImpl.java :  » Database-ORM » ODAL » com » completex » objective » components » persistency » type » Java Open Source

Java Open Source » Database ORM » ODAL 
ODAL » com » completex » objective » components » persistency » type » TypeConverterImpl.java
package com.completex.objective.components.persistency.type;

import com.completex.objective.util.TypeUtil;
import com.completex.objective.components.OdalRuntimeException;

import java.sql.SQLException;
import java.sql.Clob;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author Gennady Krizhevsky
 */
public class TypeConverterImpl implements TypeConverter {

    private int booleanFormat = TypeUtil.Y_N;

    public int getBooleanFormat() {
        return booleanFormat;
    }

    public void setBooleanFormat(int booleanFormat) {
        this.booleanFormat = booleanFormat;
    }

    public long objectToLongPrimitive(Object value) {
        if (value == null) {
            return 0;
        }
        if (value instanceof Number) {
            return TypeUtil.Number2long((Number) value);
        } else if (value instanceof String) {
            return TypeUtil.S2L((String) value).longValue();
        } else {
            throw new IllegalArgumentException("Cannot convert value " + value + " to long");
        }
    }

    public int objectToIntPrimitive(Object value) {
        return (int) objectToLongPrimitive(value);
    }

    public short objectToShortPrimitive(Object value) {
        return (short) objectToLongPrimitive(value);
    }

    public double objectToDoublePrimitive(Object value) {
        if (value == null) {
            return 0;
        }
        if (value instanceof Number) {
            return TypeUtil.Number2double((Number) value);
        } else if (value instanceof String) {
            return TypeUtil.S2BigDecimal((String) value).doubleValue();
        } else {
            throw new IllegalArgumentException("Cannot convert value " + value + " to long");
        }
    }

    public float objectToFloatPrimitive(Object value) {
        return (float) objectToDoublePrimitive(value);
    }

    public boolean objectToBooleanPrimitive(Object value) {
        if (value instanceof String) {
            return TypeUtil.S2b((String) value, TypeUtil.Y_N);
        } else {
            throw new IllegalArgumentException("Cannot convert value " + value + " to boolean");
        }
    }

    public Long objectToLong(Object value) {
        if (value == null) {
            return null;
        }
        return new Long(objectToLongPrimitive(value));
    }

    public Integer objectToInt(Object value) {
        if (value == null) {
            return null;
        }
        return new Integer(objectToIntPrimitive(value));
    }

    public Short objectToShort(Object value) {
        if (value == null) {
            return null;
        }
        return new Short(objectToShortPrimitive(value));
    }

    public BigDecimal objectToBigDecimal(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof String) {
            return TypeUtil.S2BigDecimal((String) value);
        } else if (value instanceof Number) {
            return TypeUtil.number2BigDecimal((Number) value);
        } else {
            throw new OdalRuntimeException("Cannot convert value " + value + " to BigDecimal");
        }
    }

    public BigInteger objectToBigInteger(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof String) {
            return TypeUtil.S2BigDecimal((String) value).toBigInteger();
        } else if (value instanceof Number) {
            return TypeUtil.number2BigDecimal((Number) value).toBigInteger();
        } else {
            throw new OdalRuntimeException("Cannot convert value " + value + " to BigDecimal");
        }
    }

    public Double objectToDouble(Object value) {
        if (value == null) {
            return null;
        }
        return new Double(objectToDoublePrimitive(value));
    }

    public Float objectToFloat(Object value) {
        if (value == null) {
            return null;
        }
        return new Float(objectToFloatPrimitive(value));
    }

    public byte [] objectToByteArray(Object value) {
        if (value == null) {
            return null;
        }
        try {
            if (value instanceof byte[]) {
                return (byte[]) value;
            } else {
                return (byte[]) new ByteArrayTypeHandler().transformRead(value);
            }
        } catch (SQLException e) {
            throw new OdalRuntimeException(e);
        }
    }

    public Boolean objectToBoolean(Object value) {
        if (value instanceof Boolean) {
            return (Boolean) value;
        } else if (value instanceof String) {
            return TypeUtil.S2B((String) value, TypeUtil.Y_N);
        } else {
            throw new IllegalArgumentException("Cannot convert value " + value + " to boolean");
        }
    }

    public Object convertBeanValueToPoValue (Object beanValue, Class  beanClass, Class poClass) {
        return beanValue;
    }

    public Object convertPoValueToBeanValue(Object poValue,
                                            Class beanClass,
                                            Class poClass) {
        if (poValue == null) {
            if (beanClass.isPrimitive()) {
                poValue = ridOfNullsForPrimitives(beanClass, poValue);
            } else {
                return null;
            }
        }

        assertBeanClassNotNull(beanClass);

        beanClass = toNonPrimitiveClass(beanClass);

        return convert(beanClass, poValue);
    }

    protected Object convert(Class beanClass, Object poValue) {
        if (Number.class.isAssignableFrom(beanClass)) {
            return convertToNumber(beanClass, poValue);
        } else if (String.class.isAssignableFrom(beanClass)) {
            return convertToString(beanClass, poValue);
        } else if (Character.class.isAssignableFrom(beanClass)) {
            return convertToString(beanClass, poValue);
        } else if (Boolean.class.isAssignableFrom(beanClass)) {
            return convertToBoolean(beanClass, poValue);
        } else if (byte[].class.isAssignableFrom(beanClass)) {
            return convertToByteArray(beanClass, poValue);
        } else {
            return convertToOther(beanClass, poValue);
        }
    }

    private Object convertToByteArray(Class beanClass, Object poValue) {
        return objectToByteArray(poValue);
    }

    private void assertBeanClassNotNull(Class beanClass) {
        if (beanClass == null) {
            throw new IllegalArgumentException("Bean class is not set");
        }
    }

    protected Object ridOfNullsForPrimitives(Class beanClass, Object poValue) {
        if (beanClass == boolean.class) {
            poValue = Boolean.FALSE;
        } else if (beanClass == char.class) {
        } else if (beanClass == void.class) {
        } else {
            poValue = new BigDecimal("0");
        }
        return poValue;
    }

    private Object convertToOther(Class beanClass, Object poValue) {
        return poValue;
    }

    private Boolean convertToBoolean(Class beanClass, Object poValue) {
        return objectToBoolean(poValue);
    }

    private String convertToString(Class beanClass, Object poValue) {
        if (poValue instanceof Clob) {
           ClobStringHandler handler = new ClobStringHandler();
            try {
                return (String) handler.transformRead(poValue);
            } catch (SQLException e) {
                throw new OdalRuntimeException("Cannot convert clob value to string: " + e);
            }
        } else {
           return poValue.toString();         
        }
        
    }

    protected Object convertToNumber(Class beanClass, Object poValue) {
        if (beanClass == Double.class) {
            return objectToDouble(poValue);
        } else if (beanClass == Float.class) {
            return objectToFloat(poValue);
        } else if (beanClass == Long.class) {
            return objectToLong(poValue);
        } else if (beanClass == Integer.class || beanClass == Byte.class) {
            return objectToInt(poValue);
        } else if (beanClass == Short.class) {
            return objectToShort(poValue);
        } else if (beanClass == BigDecimal.class) {
            return objectToBigDecimal(poValue);
        } else if (beanClass == BigInteger.class) {
            return objectToBigInteger(poValue);
        } else {
            throw new OdalRuntimeException("Cannot convert value " + poValue + " to " + beanClass);
        }
    }

    protected Class toNonPrimitiveClass(Class primitive) {
        if (primitive == null || !primitive.isPrimitive()) {
            return primitive;
        } else if (boolean.class == primitive) {
            return Boolean.class;
        } else if (char.class == primitive) {
            return Character.class;
        } else if (byte.class == primitive) {
            return Byte.class;
        } else if (byte.class == primitive) {
            return Byte.class;
        } else if (short.class == primitive) {
            return Short.class;
        } else if (int.class == primitive) {
            return Integer.class;
        } else if (long.class == primitive) {
            return Long.class;
        } else if (double.class == primitive) {
            return Double.class;
        } else if (float.class == primitive) {
            return Float.class;
        } else if (void.class == primitive) {
            return Void.class;
        } else {
            throw new IllegalArgumentException("Unsupporter primitive class " + primitive);
        }
    }


}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.