package org.bench4q.agent.plugin;

import org.springframework.stereotype.Component;

@Component
public class TypeConverter {
	public Object convert(Object source, String targetClassName) {
		if (targetClassName.equals("boolean")) {
			return Boolean.parseBoolean(source.toString());
		}
		if (targetClassName.equals("char")) {
			return source.toString().toCharArray()[0];
		}
		if (targetClassName.equals("byte")) {
			return Byte.parseByte(source.toString());
		}
		if (targetClassName.equals("short")) {
			return Short.parseShort(source.toString());
		}
		if (targetClassName.equals("int")) {
			return Integer.parseInt(source.toString());
		}
		if (targetClassName.equals("long")) {
			return Long.parseLong(source.toString());
		}
		if (targetClassName.equals("float")) {
			return Float.parseFloat(source.toString());
		}
		if (targetClassName.equals("double")) {
			return Double.parseDouble(source.toString());
		}

		try {
			Class<?> targetClass = Class.forName(targetClassName);
			if (targetClass.isAssignableFrom(String.class)) {
				return source.toString();
			}
			return targetClass.cast(source);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
