package com.pictc.converts.primitive;

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

public class ShortHandler implements TypeHandler<Short>{
	
	private Class<?>[] converts=new Class<?>[] {Short.class,short.class};

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

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

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

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

	
}
