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

com.mycila.testing.plugin.db.Converters Maven / Gradle / Ivy

/**
 * Copyright (C) 2008 Mathieu Carbou 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mycila.testing.plugin.db;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Arrays;

/**
 * @author Mathieu Carbou ([email protected])
 */
final class Converters {

    private Converters() {
    }


    @SuppressWarnings({"unchecked"})
    static  Converter unconvertible() {
        return (Converter) NOT_CONVERTIBLE;
    }

    static final Converter NOT_CONVERTIBLE = new Converter() {
        public Object convert(Class type, Object object) {
            throw new IllegalArgumentException("Cannot convert object of type " + object.getClass().getName() + " to type " + type.getName());
        }
    };

    static final Converter BYTE_CONVERTER = new Converter() {
        public Byte convert(Class type, Object object) {
            return toNumber(object).byteValue();
        }
    };

    static final Converter DOUBLE_CONVERTER = new Converter() {
        public Double convert(Class type, Object object) {
            return toNumber(object).doubleValue();
        }
    };

    static final Converter SHORT_CONVERTER = new Converter() {
        public Short convert(Class type, Object object) {
            return toNumber(object).shortValue();
        }
    };

    static final Converter INTEGER_CONVERTER = new Converter() {
        public Integer convert(Class type, Object object) {
            return toNumber(object).intValue();
        }
    };

    static final Converter LONG_CONVERTER = new Converter() {
        public Long convert(Class type, Object object) {
            return toNumber(object).longValue();
        }
    };

    static final Converter FLOAT_CONVERTER = new Converter() {
        public Float convert(Class type, Object object) {
            return toNumber(object).floatValue();
        }
    };

    static final Converter BIGDECIMAL_CONVERTER = new Converter() {
        public BigDecimal convert(Class type, Object object) {
            return Float.class.equals(object.getClass()) || Double.class.equals(object.getClass()) ?
                    BigDecimal.valueOf(toNumber(object).doubleValue()) :
                    BigDecimal.valueOf(toNumber(object).longValue());
        }
    };

    static final Converter BYTES_CONVERTER = new Converter() {
        public byte[] convert(Class type, Object object) {
            if (object instanceof InputStream) {
                return JdbcUtils.readFully((InputStream) object);
            } else if (object instanceof Reader) {
                return JdbcUtils.readFully((Reader) object).getBytes();
            } else if (object instanceof String) {
                return ((String) object).getBytes();
            } else if (object instanceof Blob) {
                return JdbcUtils.readBlob((Blob) object);
            } else if (object instanceof Clob) {
                return JdbcUtils.readClob((Clob) object).getBytes();
            } else {
                return Converters.unconvertible().convert(type, object);
            }
        }
    };

    static final Converter STRING_CONVERTER = new Converter() {
        public String convert(Class type, Object object) {
            if (object instanceof InputStream) {
                return new String(JdbcUtils.readFully((InputStream) object));
            } else if (object instanceof Reader) {
                return JdbcUtils.readFully((Reader) object);
            } else if (object instanceof Blob) {
                return new String(JdbcUtils.readBlob((Blob) object));
            } else if (object instanceof Clob) {
                return JdbcUtils.readClob((Clob) object);
            } else if (byte[].class.isInstance(object)) {
                return new String((byte[]) object);
            } else if (Object[].class.isInstance(object)) {
                return Arrays.toString((Object[]) object);
            } else {
                return String.valueOf(object);
            }
        }
    };

    static final Converter BOOL_CONVERTER = new Converter() {
        public Boolean convert(Class type, Object object) {
            return Boolean.valueOf(String.valueOf(object));
        }
    };

    static final Converter DATE_CONVERTER = new Converter() {
        public Date convert(Class type, Object object) {
            if (object instanceof Timestamp) {
                return new Date(((Timestamp) object).getTime());
            } else {
                return Converters.unconvertible().convert(type, object);
            }
        }
    };

    static final Converter