TypeConverter { public static <T> Object tryToConvertTo( final String value, final Class<T> type ) throws ParseException, NumberFormatException { if ( value == null || "".equals(value) ) { return null; } if ( java.util.Date.class.isAssignableFrom( type ) ) { return (java.util.Date)( new java.text.SimpleDateFormat( dateFormat, java.util.Locale.getDefault() ).parse( value ) ); } else if ( String.class.isAssignableFrom( type ) ) { return type.cast( value ); } else if ( Integer.class.isAssignableFrom( type ) || Integer.TYPE.equals( type )) { return Integer.valueOf( value ); } else if ( Double.class.isAssignableFrom( type ) || Double.TYPE.equals( type ) ) { return Double.valueOf( value ); } else if ( Long.class.isAssignableFrom( type ) || Long.TYPE.equals( type ) ) { return Long.valueOf( value ); } else if ( Boolean.class.isAssignableFrom( type ) || Boolean.TYPE.equals( type ) ) { return (BooleanHelper.isTrue(value) ? Boolean.TRUE : (Boolean.valueOf( value ))); } else if ( Byte.class.isAssignableFrom( type ) || Byte.TYPE.equals( type ) ) { return Byte.valueOf(value); } else if ( type.isEnum() ) { for ( T enumValue : type.getEnumConstants() ) { if ( enumValue.toString().equals( value ) ) { return enumValue; } } return null; } else { return value; } } private  TypeConverter(); static Object tryToConvertTo( final String value, final Class<T> type ); static Object tryToConvertToSilent( final String value, final Class<T> type ); static T convertTo( final String value, final Class<T> type ); static T convertToSilent( final String value, final Class<T> type ); static String dateFormat; }
@Test public void testTryToConvertToNull() throws Exception { assertNull(TypeConverter.tryToConvertTo(null, Object.class)); assertNull(TypeConverter.tryToConvertTo("", Object.class)); }
@Test public void testTryToConvertToInteger() throws Exception { assertEquals(Integer.valueOf(0), TypeConverter.tryToConvertTo("0", Integer.class)); assertEquals(Integer.valueOf(1), TypeConverter.tryToConvertTo("1", int.class)); assertEquals(Integer.valueOf(123456), TypeConverter.tryToConvertTo("123456", Integer.class)); assertEquals(Integer.valueOf(-123456), TypeConverter.tryToConvertTo("-123456", Integer.class)); try { TypeConverter.tryToConvertTo("nan", Integer.class); fail(); } catch (NumberFormatException expected) { } }
@Test public void testTryToConvertToLong() throws Exception { assertEquals(Long.valueOf(0), TypeConverter.tryToConvertTo("0", Long.class)); assertEquals(Long.valueOf(1), TypeConverter.tryToConvertTo("1", long.class)); assertEquals(Long.valueOf(123456), TypeConverter.tryToConvertTo("123456", Long.class)); assertEquals(Long.valueOf(-123456), TypeConverter.tryToConvertTo("-123456", Long.class)); try { TypeConverter.tryToConvertTo("nan", Long.class); fail(); } catch (NumberFormatException expected) { } }
@Test public void testTryToConvertToDouble() throws Exception { assertEquals(Double.valueOf(0), TypeConverter.tryToConvertTo("0", Double.class)); assertEquals(Double.valueOf(1), TypeConverter.tryToConvertTo("1", double.class)); assertEquals(Double.valueOf(123456), TypeConverter.tryToConvertTo("123456", Double.class)); assertEquals(Double.valueOf(-123456), TypeConverter.tryToConvertTo("-123456", Double.class)); assertEquals(Double.valueOf(3.14), TypeConverter.tryToConvertTo("3.14", Double.class)); try { TypeConverter.tryToConvertTo("nan", Double.class); fail(); } catch (NumberFormatException expected) { } }
@Test public void testTryToConvertToString() throws Exception { assertEquals("0", TypeConverter.tryToConvertTo("0", String.class)); assertEquals("1", TypeConverter.tryToConvertTo("1", String.class)); assertEquals("123456", TypeConverter.tryToConvertTo("123456", String.class)); assertEquals("abcd e", TypeConverter.tryToConvertTo("abcd e", String.class)); }
@Test public void testTryToConvertToByte() throws Exception { assertEquals(Byte.valueOf((byte) 0), TypeConverter.tryToConvertTo("0", Byte.class)); assertEquals(Byte.valueOf((byte) 1), TypeConverter.tryToConvertTo("1", byte.class)); assertEquals(Byte.MAX_VALUE, TypeConverter.tryToConvertTo("127", Byte.class)); assertEquals(Byte.MIN_VALUE, TypeConverter.tryToConvertTo("-128", Byte.class)); try { TypeConverter.tryToConvertTo("nan", Byte.class); fail(); } catch (NumberFormatException expected) { } try { TypeConverter.tryToConvertTo("129", Byte.class); fail(); } catch (NumberFormatException expected) { } }