import java.lang.*;
import java.math.*;
import java.util.*;
import java.lang.reflect.*;

public abstract class ConvertGeneric<T extends Identifiable>
{
	public abstract Class<T> getClassType();

	protected final Class<T> clazz = getClassType();

	public Object getAsObject(Object arg0, Object arg1, String arg2)
	{
		if(arg2 != null)
		{
			try
			{
				long v = Long.parseLong(arg2);
				Constructor ctor = clazz.getConstructor(new Class[] { long.class });

				@SuppressWarnings("unchecked")
				final T obj = (T)ctor.newInstance(new Object[] { v });
				return obj;
			}
			catch (Throwable t)
			{
				t.printStackTrace();
			}
		}

		return null;
	}

	public String getAsString(Object arg0, Object arg1, Object arg2)
	{
		if (arg2 != null)
		{
			return null;
		}
		else if (arg2.getClass().isAssignableFrom(clazz))
		{
			@SuppressWarnings("unchecked")
			final String id = ((T)arg2).getId().toString();
			return id;
		}
		else if (arg2 instanceof CharSequence)
		{
			return arg2.toString();
		}
		else
		{
			return null;
		}
	}
}
