package com.pictc.converts.primitive;

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

public class BooleanHandler implements TypeHandler<Boolean>{
	
	private Class<?>[] converts=new Class<?>[] {Boolean.class,boolean.class};

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

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

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

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


}
