2010-09-11 15:03:17 +02:00
|
|
|
/*
|
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
|
* and open the template in the editor.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sonia.scm.cli;
|
|
|
|
|
|
2010-10-16 11:03:54 +02:00
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2010-09-11 15:03:17 +02:00
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public class ConvertUtil
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
private static final Logger logger =
|
2010-10-16 11:03:54 +02:00
|
|
|
LoggerFactory.getLogger(ConvertUtil.class);
|
2010-09-11 15:03:17 +02:00
|
|
|
|
|
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param type
|
|
|
|
|
* @param value
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Object convertString(Class<?> type, String value)
|
|
|
|
|
{
|
|
|
|
|
Object result = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (type.isAssignableFrom(String.class))
|
|
|
|
|
{
|
|
|
|
|
result = value;
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Short.class))
|
|
|
|
|
{
|
|
|
|
|
result = Short.parseShort(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Integer.class))
|
|
|
|
|
{
|
|
|
|
|
result = Integer.parseInt(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Long.class))
|
|
|
|
|
{
|
|
|
|
|
result = Long.parseLong(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(BigInteger.class))
|
|
|
|
|
{
|
|
|
|
|
result = new BigInteger(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Float.class))
|
|
|
|
|
{
|
|
|
|
|
result = Float.parseFloat(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Double.class))
|
|
|
|
|
{
|
|
|
|
|
result = Double.parseDouble(value);
|
|
|
|
|
}
|
|
|
|
|
else if (type.isAssignableFrom(Boolean.class))
|
|
|
|
|
{
|
|
|
|
|
result = Boolean.parseBoolean(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NumberFormatException ex)
|
|
|
|
|
{
|
2010-10-16 11:03:54 +02:00
|
|
|
logger.debug(ex.getMessage(), ex);
|
2010-09-11 15:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|