package com.pictc.converts.primitive;

import com.pictc.converts.ConvertException;
import com.pictc.converts.TypeHandler;

public class FloatHandler implements TypeHandler<Float>{
	
	private Class<?>[] converts=new Class<?>[] {Float.class,float.class};

	@Override
	public boolean isType(Class<?> type) {
		return Float.class.equals(type)|| float.class.equals(type);
	}

	@Override
	public Class<?>[] getConverts() {
		return converts;
	}

	@Override
	public Object convert(Float value) {
		return value;
	}

	@Override
	public Float valueToObject(Object val) {
		if(val==null) {
			return null;
		}
		Class<?> vcs=val.getClass();
		if(isType(vcs)) {
			return Float.class.cast(val);
		}else if(Number.class.equals(vcs)) {
			return Number.class.cast(val).floatValue();
		}else {
			try {
				return Float.valueOf(val.toString());
			} catch (Exception e) {
				throw new ConvertException(vcs,val,Float.class);
			}
		}
	}

}
