package com.pictc.converts.date;

import java.sql.Time;

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

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

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

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

	@Override
	public Object convert(Time value) {
		return value.toString();
	}

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