/** * Converts comma separated string to Set * * @param <T> * type of set * @param value * comma separated values. * @return The converted set with type <T>. */ public static <T> Set<T> stringToSet(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toSet()); }
/** * Converts comma separated string to List * * @param <T> * type of list * @param value * comma separated values. * @return The converted list with type <T>. */ public static <T> List<T> stringToList(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toList()); }
/** * Converts value as per appropriate DataType of the field. * * @param strValue * : string value of the object * @param field * : field of the attribute * @return */ public static Object value(String strValue, Field field) { checkNotNull(field); // if field is not primitive type if (field.getGenericType() instanceof ParameterizedType) { Class<?> clazz = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; // convert to list if (field.getType().equals(List.class)) return stringToList(strValue, clazz); // convert to set else if (field.getType().equals(Set.class)) return stringToSet(strValue, clazz); else throw new IllegalArgumentException( format("unsupported field-type %s for %s", field.getType(), field.getName())); } else { return convert(strValue, field.getType()); } }
/** * Converts comma separated string to List * * @param <T> * type of list * @param value * comma separated values. * @return The converted list with type <T>. */ public static <T> List<T> stringToList(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toList()); }
/** * Converts comma separated string to Set * * @param <T> * type of set * @param value * comma separated values. * @return The converted set with type <T>. */ public static <T> Set<T> stringToSet(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toSet()); }
/** * Converts value as per appropriate DataType of the field. * * @param strValue * : string value of the object * @param field * : field of the attribute * @return */ public static Object value(String strValue, Field field) { checkNotNull(field); // if field is not primitive type if (field.getGenericType() instanceof ParameterizedType) { Class<?> clazz = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; // convert to list if (field.getType().equals(List.class)) return stringToList(strValue, clazz); // convert to set else if (field.getType().equals(Set.class)) return stringToSet(strValue, clazz); else throw new IllegalArgumentException( format("unsupported field-type %s for %s", field.getType(), field.getName())); } else { return convert(strValue, field.getType()); } }