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

org.nomin.util.Converters Maven / Gradle / Ivy

Go to download

Nomin is a mapping engine for the Java platform. It provides abilities to transform object trees according to declarative mapping rules. Main features of Nomin are no XML configuration, intuitively looking mapping, using arbitrary expressions and method invocations in mappings, pre and postprocessing right in a mapping listing, customizations. It's applicable for any Java compatible classes, not only JavaBeans.

There is a newer version: 1.2.2
Show newest version
package org.nomin.util;

import org.nomin.core.NominException;
import org.slf4j.*;
import java.io.File;
import java.math.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.*;
import java.util.Date;
import static java.lang.String.format;
import static java.util.Arrays.asList;

/**
 * Supplies scalar converters.
 * @author Dmitry Dobrynin
 *         Created at 26.04.2014 21:25.
 */
@SuppressWarnings("unchecked")
public class Converters {
    private static Logger logger = LoggerFactory.getLogger(Converters.class);
    private static final Map, Map, ScalarConverter>> converters = new HashMap, Map, ScalarConverter>>();

    public static final ScalarConverter asIs = new ScalarConverter() {
        public Object convert(Object source) {
            return source;
        }
    };
    public static final ScalarConverter obj2str = new ScalarConverter() {
        public String convert(Object source) {
            return source.toString();
        }
    };
    public static final ScalarConverter number2byte = new ScalarConverter() {
        public Byte convert(Number source) {
            return source.byteValue();
        }
    };
    public static final ScalarConverter number2short = new ScalarConverter() {
        public Short convert(Number source) {
            return source.shortValue();
        }
    };
    public static final ScalarConverter number2int = new ScalarConverter() {
        public Integer convert(Number source) {
            return source.intValue();
        }
    };
    public static final ScalarConverter number2long = new ScalarConverter() {
        public Long convert(Number source) {
            return source.longValue();
        }
    };
    public static final ScalarConverter number2float = new ScalarConverter() {
        public Float convert(Number source) {
            return source.floatValue();
        }
    };
    public static final ScalarConverter number2dbl = new ScalarConverter() {
        public Double convert(Number source) {
            return source.doubleValue();
        }
    };
    public static final ScalarConverter number2bigint = new ScalarConverter() {
        public BigInteger convert(Number source) {
            return BigInteger.valueOf(source.longValue());
        }
    };
    public static final ScalarConverter number2bigdec = new ScalarConverter() {
        public BigDecimal convert(Number source) {
            return BigDecimal.valueOf(source.doubleValue());
        }
    };
    public static final ScalarConverter> str2class = new ScalarConverter>() {
        public Class convert(String source) {
            try { return Class.forName(source); } catch (ClassNotFoundException e) {
                throw new NominException("Could not find specified class!", e);
            }
        }
    };
    public static final ScalarConverter str2url = new ScalarConverter() {
        public URL convert(String source) {
            try { return new URL(source); } catch (MalformedURLException e) {
                throw new NominException(source + " is invalid URL!", e);
            }
        }
    };
    public static final ScalarConverter str2uri = new ScalarConverter() {
        public URI convert(String source) {
            try { return new URI(source); } catch (URISyntaxException e) {
                throw new NominException(source + " is invalid URI!", e);
            }
        }
    };
    public static final ScalarConverter str2file = new ScalarConverter() {
        public File convert(String source) {
            return new File(source);
        }
    };
    public static final ScalarConverter str2byte = new ScalarConverter() {
        public Byte convert(String source) {
            return Byte.valueOf(source);
        }
    };
    public static final ScalarConverter str2short = new ScalarConverter() {
        public Short convert(String source) {
            return Short.valueOf(source);
        }
    };
    public static final ScalarConverter str2int = new ScalarConverter() {
        public Integer convert(String source) {
            return Integer.valueOf(source);
        }
    };
    public static final ScalarConverter str2long = new ScalarConverter() {
        public Long convert(String source) {
            return Long.valueOf(source);
        }
    };
    public static final ScalarConverter str2float = new ScalarConverter() {
        public Float convert(String source) {
            return Float.valueOf(source);
        }
    };
    public static final ScalarConverter str2dbl = new ScalarConverter() {
        public Double convert(String source) {
            return Double.valueOf(source);
        }
    };
    public static final ScalarConverter str2bigint = new ScalarConverter() {
        public BigInteger convert(String source) {
            return new BigInteger(source);
        }
    };
    public static final ScalarConverter str2bigdec = new ScalarConverter() {
        public BigDecimal convert(String source) {
            return new BigDecimal(source);
        }
    };
    public static final ScalarConverter udate2sdate = new ScalarConverter() {
        public java.sql.Date convert(Date source) {
            return new java.sql.Date(source.getTime());
        }
    };
    public static final ScalarConverter udate2timestamp = new ScalarConverter() {
        public Timestamp convert(Date source) {
            return new Timestamp(source.getTime());
        }
    };
    public static final ScalarConverter udate2time = new ScalarConverter() {
        public Time convert(Date source) {
            return new Time(source.getTime());
        }
    };
    private static final ScalarConverter str2bool = new ScalarConverter() {
        public Boolean convert(String source) {
            return Boolean.valueOf(source);
        }
    };

    public static  void register(Class source, Class target, ScalarConverter converter) {
        logger.debug("Registering converter from {} to {}", source, target);
        Map, ScalarConverter> registered = converters.get(source);
        if (registered == null) converters.put(source, registered = new HashMap, ScalarConverter>());
        registered.put(target, converter);
    }

    public static  void register(List> sources, List> targets, ScalarConverter converter) {
        for (Class s : sources) for (Class t : targets) register(s, t, converter);
    }

    public static boolean isRegistered(Class source, Class target) {
        return findConverter(source, target) != null;
    }

    public static ScalarConverter findConverter(Class source, Class target) {
        Map, ScalarConverter> registered = converters.get(source);
        return (ScalarConverter) (registered != null && registered.containsKey(target) ?
                        registered.get(target) : target == String.class ? obj2str : null);
    }

    public static Object convert(Object source, Class target) {
        ScalarConverter converter = findConverter(source.getClass(), target);
        if (converter == null)
            throw new NominException(format("Scalar converter between %s and %s has not been registered!", source.getClass(), target));
        return converter.convert(source);
    }

    static {
        register(Byte.class, Byte.TYPE, (ScalarConverter) asIs);
        register(Byte.TYPE, Byte.class, (ScalarConverter) asIs);
        register(asList(Byte.class, Byte.TYPE), asList(Short.class, Short.TYPE), number2short);
        register(asList(Byte.class, Byte.TYPE), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(Byte.class, Byte.TYPE), asList(Long.class, Long.TYPE), number2long);
        register(asList(Byte.class, Byte.TYPE), asList(Float.class, Float.TYPE), number2float);
        register(asList(Byte.class, Byte.TYPE), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(Byte.class, Byte.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Byte.class, Byte.TYPE), asList(BigDecimal.class), number2bigdec);

        register(Short.class, Short.TYPE, (ScalarConverter) asIs);
        register(Short.TYPE, Short.class, (ScalarConverter) asIs);
        register(asList(Short.class, Short.TYPE), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(Short.class, Short.TYPE), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(Short.class, Short.TYPE), asList(Long.class, Long.TYPE), number2long);
        register(asList(Short.class, Short.TYPE), asList(Float.class, Float.TYPE), number2float);
        register(asList(Short.class, Short.TYPE), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(Short.class, Short.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Short.class, Short.TYPE), asList(BigDecimal.class), number2bigdec);

        register(Integer.class, Integer.TYPE, (ScalarConverter) asIs);
        register(Integer.TYPE, Integer.class, (ScalarConverter) asIs);
        register(asList(Integer.class, Integer.TYPE), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(Integer.class, Integer.TYPE), asList(Short.class, Short.TYPE), number2short);
        register(asList(Integer.class, Integer.TYPE), asList(Long.class, Long.TYPE), number2long);
        register(asList(Integer.class, Integer.TYPE), asList(Float.class, Float.TYPE), number2float);
        register(asList(Integer.class, Integer.TYPE), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(Integer.class, Integer.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Integer.class, Integer.TYPE), asList(BigDecimal.class), number2bigdec);

        register(Long.class, Long.TYPE, (ScalarConverter) asIs);
        register(Long.TYPE, Long.class, (ScalarConverter) asIs);
        register(asList(Long.class, Long.TYPE), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(Long.class, Long.TYPE), asList(Short.class, Short.TYPE), number2short);
        register(asList(Long.class, Long.TYPE), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(Long.class, Long.TYPE), asList(Float.class, Float.TYPE), number2float);
        register(asList(Long.class, Long.TYPE), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(Long.class, Long.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Long.class, Long.TYPE), asList(BigDecimal.class), number2bigdec);

        register(Float.class, Float.TYPE, (ScalarConverter) asIs);
        register(Float.TYPE, Float.class, (ScalarConverter) asIs);
        register(asList(Float.class, Float.TYPE), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(Float.class, Float.TYPE), asList(Short.class, Short.TYPE), number2short);
        register(asList(Float.class, Float.TYPE), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(Float.class, Float.TYPE), asList(Long.class, Long.TYPE), number2long);
        register(asList(Float.class, Float.TYPE), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(Float.class, Float.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Float.class, Float.TYPE), asList(BigDecimal.class), number2bigdec);

        register(Double.class, Double.TYPE, (ScalarConverter) asIs);
        register(Double.TYPE, Double.class, (ScalarConverter) asIs);
        register(asList(Double.class, Double.TYPE), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(Double.class, Double.TYPE), asList(Short.class, Short.TYPE), number2short);
        register(asList(Double.class, Double.TYPE), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(Double.class, Double.TYPE), asList(Long.class, Long.TYPE), number2long);
        register(asList(Double.class, Double.TYPE), asList(Float.class, Float.TYPE), number2float);
        register(asList(Double.class, Double.TYPE), asList(BigInteger.class), number2bigint);
        register(asList(Double.class, Double.TYPE), asList(BigDecimal.class), number2bigdec);

        register(asList(BigInteger.class), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(BigInteger.class), asList(Short.class, Short.TYPE), number2short);
        register(asList(BigInteger.class), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(BigInteger.class), asList(Long.class, Long.TYPE), number2long);
        register(asList(BigInteger.class), asList(Float.class, Float.TYPE), number2float);
        register(asList(BigInteger.class), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(BigInteger.class), asList(BigDecimal.class), number2bigdec);

        register(asList(BigDecimal.class), asList(Byte.class, Byte.TYPE), number2byte);
        register(asList(BigDecimal.class), asList(Short.class, Short.TYPE), number2short);
        register(asList(BigDecimal.class), asList(Integer.class, Integer.TYPE), number2int);
        register(asList(BigDecimal.class), asList(Long.class, Long.TYPE), number2long);
        register(asList(BigDecimal.class), asList(Float.class, Float.TYPE), number2float);
        register(asList(BigDecimal.class), asList(Double.class, Double.TYPE), number2dbl);
        register(asList(BigDecimal.class), asList(BigInteger.class), number2bigint);

        register(asList(String.class), asList(Boolean.class, Boolean.TYPE), str2bool);
        register(asList(String.class), asList(Byte.class, Byte.TYPE), str2byte);
        register(asList(String.class), asList(Short.class, Short.TYPE), str2short);
        register(asList(String.class), asList(Integer.class, Integer.TYPE), str2int);
        register(asList(String.class), asList(Long.class, Long.TYPE), str2long);
        register(asList(String.class), asList(Float.class, Float.TYPE), str2float);
        register(asList(String.class), asList(Double.class, Double.TYPE), str2dbl);
        register(asList(String.class), asList(BigInteger.class), str2bigint);
        register(asList(String.class), asList(BigDecimal.class), str2bigdec);
        register(String.class, URI.class, str2uri);
        register(String.class, Class.class, str2class);
        register(String.class, File.class, str2file);
        register(String.class, URL.class, str2url);
        register(String.class, URI.class, str2uri);

        register(Date.class, java.sql.Date.class, udate2sdate);
        register(Date.class, Time.class, udate2time);
        register(Date.class, Timestamp.class, udate2timestamp);

        register(Boolean.class, Boolean.TYPE, (ScalarConverter) asIs);
        register(Boolean.TYPE, Boolean.class, (ScalarConverter) asIs);
    }
}