package com.pictc.converts.primitive;

import java.math.BigInteger;

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

public class BigIntegerHandler implements TypeHandler<BigInteger>{
	
	private Class<?>[] converts=new Class<?>[] {BigInteger.class};

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

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

	@Override
	public Object convert(BigInteger value) {
		if(value==null) {
			return null;
		}
		return value.toString();
	}

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

}
