package com.pictc.converts.primitive;

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

public class IntegerHandler implements TypeHandler<Integer>{
	
	private Class<?>[] converts=new Class<?>[] {Integer.class,int.class};
	

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

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

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

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

	

}
