package com.pictc.converts.primitive;

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

public class CharacterHandler implements TypeHandler<Character>{
	
	private Class<?>[] converts=new Class<?>[] {Character.class,char.class};

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

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

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

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

}
