StateTypeConverter { public static <T> T convert(String value, Class<T> to) { if (value == null && !(String.class.equals(to) || String[].class.equals(to))) { return null; } if (String.class.equals(to)) { return to.cast(value); } else if (Integer.class.equals(to)) { return to.cast(Integer.valueOf(value)); } else if (int.class.equals(to)) { return (T) Integer.valueOf(value); } else if (Double.class.equals(to)) { return to.cast(Double.valueOf(value)); } else if (double.class.equals(to)) { return (T) Double.valueOf(value); } else if (Long.class.equals(to)) { return to.cast(Long.valueOf(value)); } else if (long.class.equals(value)) { return (T) Long.valueOf(value); } else if (Boolean.class.equals(to)) { return to.cast(Boolean.valueOf(value)); } else if (boolean.class.equals(to)) { return (T) Boolean.valueOf(value); } else if (URL.class.equals(to)) { try { return to.cast(new URI(value).toURL()); } catch (MalformedURLException e) { throw new IllegalArgumentException("Unable to convert value " + value + " to URL", e); } catch (URISyntaxException e) { throw new IllegalArgumentException("Unable to convert value " + value + " to URL", e); } } else if (URI.class.equals(to)) { try { return to.cast(new URI(value)); } catch (URISyntaxException e) { throw new IllegalArgumentException("Unable to convert value " + value + " to URL", e); } } else { String trimmedValue = extractEnumName(value); if (to.isEnum()) { return (T) Enum.valueOf((Class<Enum>) to, trimmedValue.toUpperCase()); } else if (String[].class.equals(to)) { final String[] convertedArray = value.split(","); if (convertedArray.length == 0) { return to.cast(new String[0]); } trimElements(convertedArray); if (convertedArray.length == 1 && hasOnlyBlanks(convertedArray)) { return to.cast(new String[0]); } return to.cast(convertedArray); } else if (Collection.class.isAssignableFrom(to)) { final String[] convertedArray = value.split(","); if (convertedArray.length == 0) { return to.cast(new ArrayList<>()); } trimElements(convertedArray); if (convertedArray.length == 1 && hasOnlyBlanks(convertedArray)) { return to.cast(new ArrayList<>()); } return to.cast(Arrays.asList(convertedArray)); } else if (Charset.class.equals(to)) { return to.cast(Charset.forName(trimmedValue.toUpperCase())); } else if (Class.class.equals(to)) { try { Object clazz = Class.forName(value); return to.cast(clazz); } catch (Exception e) { throw new IllegalArgumentException("Unable to find class [" + value + "].", e); } } else { try { Object instance = Class.forName(value).newInstance(); return to.cast(instance); } catch (Exception e) { throw new IllegalArgumentException( "Unable to convert value [" + value + "] to a class [" + to.getName() + "].", e); } } } }  static T convert(String value, Class<T> to);  }
@Test public void should_convert_empty_string_to_empty_string_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] convertedStringArray = typeConverter.convert("", String[].class); assertThat(convertedStringArray).isEmpty(); }
@Test public void should_convert_blank_string_to_empty_string_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] convertedStringArray = typeConverter.convert(" ", String[].class); assertThat(convertedStringArray).isEmpty(); }
@Test public void should_convert_sequence_of_blank_strings_to_empty_string_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] arrayOfEmptyStrings = Arrays.array("", "", "", "", ""); String[] convertedStringArray = typeConverter.convert(" , , , , ", String[].class); assertThat(convertedStringArray).isEqualTo(arrayOfEmptyStrings); }
@Test public void should_convert_single_element_to_one_element_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] singleElementArray = Arrays.array("one element"); String[] convertedStringArray = typeConverter.convert("one element", String[].class); assertThat(convertedStringArray).isEqualTo(singleElementArray); }
@Test public void should_convert_single_element_with_delimiter_to_one_element_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] singleElementArray = Arrays.array("one element"); String[] convertedStringArray = typeConverter.convert("one element,", String[].class); assertThat(convertedStringArray).isEqualTo(singleElementArray); }
@Test public void should_convert_single_element_with_delimiters_to_one_element_array() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] singleElementArray = Arrays.array("one element"); String[] convertedStringArray = typeConverter.convert("one element,,,,,,,", String[].class); assertThat(convertedStringArray).isEqualTo(singleElementArray); }
@Test public void should_convert_blank_to_empty_string_when_appear_in_sequence_with_non_blanks() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); String[] expectedArray = Arrays.array("a", "", "test", "", "", "b"); String[] convertedStringArray = typeConverter.convert("a, , test , , , b ", String[].class); assertThat(convertedStringArray).isEqualTo(expectedArray); }
@Test public void should_convert_string() throws Exception { String expectedString = "Hello"; StateTypeConverter typeConverter = new StateTypeConverter(); String convertedString = typeConverter.convert("Hello", String.class); assertThat(convertedString).isEqualTo(expectedString); }
@Test public void should_convert_string_to_integer() throws Exception { Integer expectedInteger = Integer.valueOf(15); StateTypeConverter typeConverter = new StateTypeConverter(); Integer convertedInteger = typeConverter.convert("15", Integer.class); assertThat(convertedInteger).isEqualTo(expectedInteger); }
@Test public void should_convert_string_to_double() throws Exception { Double expecteDouble = Double.valueOf("123"); StateTypeConverter typeConverter = new StateTypeConverter(); Double convertedDouble = typeConverter.convert("123", Double.class); assertThat(convertedDouble).isEqualTo(expecteDouble); }
@Test public void should_convert_string_to_long() throws Exception { Long expectedLong = Long.valueOf(-456); StateTypeConverter typeConverter = new StateTypeConverter(); Long convertedLong = typeConverter.convert("-456", Long.class); assertThat(convertedLong).isEqualTo(expectedLong); }
@Test public void should_convert_string_to_boolean() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); Boolean convertedBoolen = typeConverter.convert("True", Boolean.class); assertThat(convertedBoolen).isTrue(); }
@Test public void should_convert_string_to_URL() throws Exception { URL expectedUrl = new URI("http: StateTypeConverter typeConverter = new StateTypeConverter(); URL convertedUrl = typeConverter.convert("http: assertThat(convertedUrl).isEqualTo(expectedUrl); }
@Test public void should_convert_string_to_URI() throws Exception { URI expectedUri = new URI("http: StateTypeConverter typeConverter = new StateTypeConverter(); URI convertedUri = typeConverter.convert("http: assertThat(convertedUri).isEqualTo(expectedUri); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_unsupported_type() throws Exception { StateTypeConverter typeConverter = new StateTypeConverter(); typeConverter.convert("typeConverter", StateTypeConverter.class); }