package com.pictc.converts.primitive;

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

public class LongHandler implements TypeHandler<Long>{
	
	private Class<?>[] converts=new Class<?>[] {Long.class,long.class};

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

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

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

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

}
