@Test public void testValueOf() throws Exception { assertEquals(Integer.valueOf(21), StringUtil.valueOf("21", Integer.class)); assertEquals(Boolean.valueOf(true), StringUtil.valueOf("true", Boolean.class)); assertEquals("Foo", StringUtil.valueOf("Foo", String.class)); assertEquals(Float.valueOf(10.12f), StringUtil.valueOf("10.12", Float.class)); assertEquals(Double.valueOf(121.123), StringUtil.valueOf("121.123", Double.class)); assertEquals(Long.valueOf(12334567L), StringUtil.valueOf("12334567", Long.class)); assertEquals(Short.valueOf((short)21), StringUtil.valueOf("21", Short.class)); List list = StringUtil.valueOf("foo,bar,x,y,z", List.class); assertEquals(5, list.size()); assertTrue(list.contains("foo")); assertTrue(list.contains("x")); int[] values = StringUtil.valueOf("1,2,3,4,5", new int[0].getClass()); assertEquals(5, values.length); assertEquals(5, values[4]); Map m = StringUtil.valueOf("foo=bar,x=,y=z", Map.class); assertEquals(3, m.size()); assertEquals(m.get("foo"), "bar"); assertEquals(m.get("x"), ""); assertEquals(EnumTest.HELLO, StringUtil.valueOf("HELLO", EnumTest.class)); assertEquals(new URL("http: }
@SuppressWarnings("unchecked") public static <T> T valueOf(String value, Class type){ if (value == null) { return null; } if(type == String.class) { return (T) value; } else if(type == Boolean.class || type == Boolean.TYPE) { return (T) Boolean.valueOf(value); } else if (type == Integer.class || type == Integer.TYPE) { return (T) Integer.decode(value); } else if (type == Float.class || type == Float.TYPE) { return (T) Float.valueOf(value); } else if (type == Double.class || type == Double.TYPE) { return (T) Double.valueOf(value); } else if (type == Long.class || type == Long.TYPE) { return (T) Long.decode(value); } else if (type == Short.class || type == Short.TYPE) { return (T) Short.decode(value); } else if (type.isAssignableFrom(List.class)) { return (T)new ArrayList<String>(Arrays.asList(value.split(","))); } else if (type.isAssignableFrom(Set.class)) { return (T)new HashSet<String>(Arrays.asList(value.split(","))); } else if (type.isArray()) { String[] values = value.split(","); Object array = Array.newInstance(type.getComponentType(), values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, valueOf(values[i], type.getComponentType())); } return (T)array; } else if (type == Void.class) { return null; } else if (type.isEnum()) { return (T)Enum.valueOf(type, value); } else if (type == URL.class) { try { return (T)new URL(value); } catch (MalformedURLException e) { } } else if (type.isAssignableFrom(Map.class)) { List<String> l = Arrays.asList(value.split(",")); Map m = new HashMap<String, String>(); for(String key: l) { int index = key.indexOf('='); if (index != -1) { m.put(key.substring(0, index), key.substring(index+1)); } } return (T)m; } throw new IllegalArgumentException("Conversion from String to "+ type.getName() + " is not supported"); }
StringUtil { @SuppressWarnings("unchecked") public static <T> T valueOf(String value, Class type){ if (value == null) { return null; } if(type == String.class) { return (T) value; } else if(type == Boolean.class || type == Boolean.TYPE) { return (T) Boolean.valueOf(value); } else if (type == Integer.class || type == Integer.TYPE) { return (T) Integer.decode(value); } else if (type == Float.class || type == Float.TYPE) { return (T) Float.valueOf(value); } else if (type == Double.class || type == Double.TYPE) { return (T) Double.valueOf(value); } else if (type == Long.class || type == Long.TYPE) { return (T) Long.decode(value); } else if (type == Short.class || type == Short.TYPE) { return (T) Short.decode(value); } else if (type.isAssignableFrom(List.class)) { return (T)new ArrayList<String>(Arrays.asList(value.split(","))); } else if (type.isAssignableFrom(Set.class)) { return (T)new HashSet<String>(Arrays.asList(value.split(","))); } else if (type.isArray()) { String[] values = value.split(","); Object array = Array.newInstance(type.getComponentType(), values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, valueOf(values[i], type.getComponentType())); } return (T)array; } else if (type == Void.class) { return null; } else if (type.isEnum()) { return (T)Enum.valueOf(type, value); } else if (type == URL.class) { try { return (T)new URL(value); } catch (MalformedURLException e) { } } else if (type.isAssignableFrom(Map.class)) { List<String> l = Arrays.asList(value.split(",")); Map m = new HashMap<String, String>(); for(String key: l) { int index = key.indexOf('='); if (index != -1) { m.put(key.substring(0, index), key.substring(index+1)); } } return (T)m; } throw new IllegalArgumentException("Conversion from String to "+ type.getName() + " is not supported"); } }
StringUtil { @SuppressWarnings("unchecked") public static <T> T valueOf(String value, Class type){ if (value == null) { return null; } if(type == String.class) { return (T) value; } else if(type == Boolean.class || type == Boolean.TYPE) { return (T) Boolean.valueOf(value); } else if (type == Integer.class || type == Integer.TYPE) { return (T) Integer.decode(value); } else if (type == Float.class || type == Float.TYPE) { return (T) Float.valueOf(value); } else if (type == Double.class || type == Double.TYPE) { return (T) Double.valueOf(value); } else if (type == Long.class || type == Long.TYPE) { return (T) Long.decode(value); } else if (type == Short.class || type == Short.TYPE) { return (T) Short.decode(value); } else if (type.isAssignableFrom(List.class)) { return (T)new ArrayList<String>(Arrays.asList(value.split(","))); } else if (type.isAssignableFrom(Set.class)) { return (T)new HashSet<String>(Arrays.asList(value.split(","))); } else if (type.isArray()) { String[] values = value.split(","); Object array = Array.newInstance(type.getComponentType(), values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, valueOf(values[i], type.getComponentType())); } return (T)array; } else if (type == Void.class) { return null; } else if (type.isEnum()) { return (T)Enum.valueOf(type, value); } else if (type == URL.class) { try { return (T)new URL(value); } catch (MalformedURLException e) { } } else if (type.isAssignableFrom(Map.class)) { List<String> l = Arrays.asList(value.split(",")); Map m = new HashMap<String, String>(); for(String key: l) { int index = key.indexOf('='); if (index != -1) { m.put(key.substring(0, index), key.substring(index+1)); } } return (T)m; } throw new IllegalArgumentException("Conversion from String to "+ type.getName() + " is not supported"); } private  StringUtil(); }
StringUtil { @SuppressWarnings("unchecked") public static <T> T valueOf(String value, Class type){ if (value == null) { return null; } if(type == String.class) { return (T) value; } else if(type == Boolean.class || type == Boolean.TYPE) { return (T) Boolean.valueOf(value); } else if (type == Integer.class || type == Integer.TYPE) { return (T) Integer.decode(value); } else if (type == Float.class || type == Float.TYPE) { return (T) Float.valueOf(value); } else if (type == Double.class || type == Double.TYPE) { return (T) Double.valueOf(value); } else if (type == Long.class || type == Long.TYPE) { return (T) Long.decode(value); } else if (type == Short.class || type == Short.TYPE) { return (T) Short.decode(value); } else if (type.isAssignableFrom(List.class)) { return (T)new ArrayList<String>(Arrays.asList(value.split(","))); } else if (type.isAssignableFrom(Set.class)) { return (T)new HashSet<String>(Arrays.asList(value.split(","))); } else if (type.isArray()) { String[] values = value.split(","); Object array = Array.newInstance(type.getComponentType(), values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, valueOf(values[i], type.getComponentType())); } return (T)array; } else if (type == Void.class) { return null; } else if (type.isEnum()) { return (T)Enum.valueOf(type, value); } else if (type == URL.class) { try { return (T)new URL(value); } catch (MalformedURLException e) { } } else if (type.isAssignableFrom(Map.class)) { List<String> l = Arrays.asList(value.split(",")); Map m = new HashMap<String, String>(); for(String key: l) { int index = key.indexOf('='); if (index != -1) { m.put(key.substring(0, index), key.substring(index+1)); } } return (T)m; } throw new IllegalArgumentException("Conversion from String to "+ type.getName() + " is not supported"); } private  StringUtil(); static String join(Collection<String> strings, String delimiter); static String toString( final Object[] array, final String delim ); static String toString( final Object[] array, final String delim, boolean includeBrackets); static String toString( final Object[] array ); static List<String> split(String str, String splitter); static String replace(String source, String search, String replace); static String replaceAll(String source, String search, String replace); static List<String> getTokens(String str, String delimiter); static String getLastToken(String str, String delimiter); static String getFirstToken(String str, String delimiter); static String getStackTrace( final Throwable t ); static boolean isEmpty(final String text); static int indexOfIgnoreCase(final String text, final String str); static boolean startsWithIgnoreCase(final String text, final String prefix); static boolean endsWithIgnoreCase(final String text, final String suffix); static boolean isLetter(char c); static boolean isDigit(char c); static boolean isLetterOrDigit(char c); @SuppressWarnings("unchecked") static T valueOf(String value, Class type); static boolean equalsIgnoreCase(String s1, String s2); static T caseInsensitiveValueOf(Class<T> enumType, String name); static List<String> tokenize(String str, char delim); static String unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb); }
StringUtil { @SuppressWarnings("unchecked") public static <T> T valueOf(String value, Class type){ if (value == null) { return null; } if(type == String.class) { return (T) value; } else if(type == Boolean.class || type == Boolean.TYPE) { return (T) Boolean.valueOf(value); } else if (type == Integer.class || type == Integer.TYPE) { return (T) Integer.decode(value); } else if (type == Float.class || type == Float.TYPE) { return (T) Float.valueOf(value); } else if (type == Double.class || type == Double.TYPE) { return (T) Double.valueOf(value); } else if (type == Long.class || type == Long.TYPE) { return (T) Long.decode(value); } else if (type == Short.class || type == Short.TYPE) { return (T) Short.decode(value); } else if (type.isAssignableFrom(List.class)) { return (T)new ArrayList<String>(Arrays.asList(value.split(","))); } else if (type.isAssignableFrom(Set.class)) { return (T)new HashSet<String>(Arrays.asList(value.split(","))); } else if (type.isArray()) { String[] values = value.split(","); Object array = Array.newInstance(type.getComponentType(), values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, valueOf(values[i], type.getComponentType())); } return (T)array; } else if (type == Void.class) { return null; } else if (type.isEnum()) { return (T)Enum.valueOf(type, value); } else if (type == URL.class) { try { return (T)new URL(value); } catch (MalformedURLException e) { } } else if (type.isAssignableFrom(Map.class)) { List<String> l = Arrays.asList(value.split(",")); Map m = new HashMap<String, String>(); for(String key: l) { int index = key.indexOf('='); if (index != -1) { m.put(key.substring(0, index), key.substring(index+1)); } } return (T)m; } throw new IllegalArgumentException("Conversion from String to "+ type.getName() + " is not supported"); } private  StringUtil(); static String join(Collection<String> strings, String delimiter); static String toString( final Object[] array, final String delim ); static String toString( final Object[] array, final String delim, boolean includeBrackets); static String toString( final Object[] array ); static List<String> split(String str, String splitter); static String replace(String source, String search, String replace); static String replaceAll(String source, String search, String replace); static List<String> getTokens(String str, String delimiter); static String getLastToken(String str, String delimiter); static String getFirstToken(String str, String delimiter); static String getStackTrace( final Throwable t ); static boolean isEmpty(final String text); static int indexOfIgnoreCase(final String text, final String str); static boolean startsWithIgnoreCase(final String text, final String prefix); static boolean endsWithIgnoreCase(final String text, final String suffix); static boolean isLetter(char c); static boolean isDigit(char c); static boolean isLetterOrDigit(char c); @SuppressWarnings("unchecked") static T valueOf(String value, Class type); static boolean equalsIgnoreCase(String s1, String s2); static T caseInsensitiveValueOf(Class<T> enumType, String name); static List<String> tokenize(String str, char delim); static String unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb);  }