package com.pictc.converts.primitive;

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

public class ByteHandler implements TypeHandler<Byte>{
	
	private Class<?>[] converts=new Class<?>[] {Byte.class,byte.class};

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

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

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

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

}
