/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import java.util.Date;

import com.crunii.micro.common.converter.ConvertUtilBean;

/**
 * @author 田平 create 2020年2月22日下午2:11:54
 */
public class ConvertUtil {

	public static final String TYPE_STRING = "String";
	public static final String TYPE_INTEGER = "Integer";
	public static final String TYPE_LONG = "Long";
	public static final String TYPE_DATE = "Date";
	public static final String TYPE_FLOAT = "Float";
	public static final String TYPE_BOOLEAN = "Boolean";

	private static ThreadLocal<ConvertUtilBean> convert = new ThreadLocal<ConvertUtilBean>() {
		protected ConvertUtilBean initialValue() {
			return new ConvertUtilBean();
		}
	};

	public static String convert(final Object value) {
		return convert.get().convert(value);
	}

	@SuppressWarnings("unchecked")
	public static <T> T convert(final Object value, final Class<T> clazz) {

		return (T) convert.get().convert(value, clazz);
	}

	@SuppressWarnings("unchecked")
	public static <T> T convert(final String value, final Class<T> clazz) {
		return (T) convert.get().convert(value, clazz);
	}

	public static Object convert(final String value, String type) {
		switch (type) {
		case TYPE_STRING:
			return convert(value, String.class);
		case TYPE_INTEGER:
			return convert(value, Integer.class);
		case TYPE_LONG:
			return convert(value, Long.class);
		case TYPE_DATE:
			return convert(value, Date.class);
		case TYPE_FLOAT:
			return convert(value, Float.class);
		case TYPE_BOOLEAN:
			return convert(value, Boolean.class);
		default:
			throw new RuntimeException("unsupport this type:" + type);
		}
	}

	public static void main(String[] args) {
		System.out.println(convert("abd", TYPE_STRING).getClass());
		System.out.println(convert("12312", TYPE_INTEGER).getClass());
		System.out.println(convert("1111111113", TYPE_LONG).getClass());
		System.out.println(convert("2010-01-01 12:00:11", TYPE_DATE).getClass());
		System.out.println(convert("3.14", TYPE_FLOAT).getClass());
		System.out.println(convert("true", TYPE_BOOLEAN).getClass());
		System.out.println(convert("TRUE", TYPE_BOOLEAN).getClass());
		System.out.println(convert("True", TYPE_BOOLEAN).getClass());
		System.out.println(convert("false", TYPE_BOOLEAN).getClass());

		Long id = 111L;
		System.out.println(convert(id, Float.class));
		System.out.println(convert(id, Integer.class));
		System.out.println(convert(id, Long.class));
	}

}
