Skip to content

Instantly share code, notes, and snippets.

@budhash
Created July 26, 2016 01:06
Show Gist options
  • Save budhash/6e27cd50e21b05f94b0362f49ee5dd7c to your computer and use it in GitHub Desktop.
Save budhash/6e27cd50e21b05f94b0362f49ee5dd7c to your computer and use it in GitHub Desktop.
package com.budhash.common.utils;
//import com.github.drapostolos.typeparser.TypeParser;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Converter {
private static final char DISABLED_DELIMITER = '\0';
private static final String DEFAULT_LIST_DILIMER = ",";
//private static TypeParser TYPEPARSER = TypeParser.newBuilder().build();
public static <T extends Enum<?>> List<T> toEnumList(final List<String> collection, final Class<T> type) {
if (null != collection && !collection.isEmpty()) {
return collection
.stream()
.filter(s -> StringUtils.isNotBlank(s))
.map(String::trim)
.map(e -> Utilities.searchEnum(type, e))
.filter(Objects::nonNull)
.collect(Collectors.toList());
} else {
return Arrays.asList();
}
}
public static <T> List<T> toList(final List<String> collection, final Class<T> type) {
if (null != collection && !collection.isEmpty() && PrimitiveConverter.isSupported(type)) {
return (List<T>)collection
.stream()
.filter(s -> StringUtils.isNotBlank(s))
.map(String::trim)
//.map(e -> TYPEPARSER.parse(e, type))
.map(e -> PrimitiveConverter.getConverter(type))
.filter(Objects::nonNull)
.collect(Collectors.toList());
} else {
return Arrays.asList();
}
}
public static List<String> toStringList(final String[] array) {
if (null != array && array.length > 0) {
return Arrays.stream(array)
.filter(StringUtils::isNotBlank)
.map(String::trim)
.collect(Collectors.toList());
} else {
return Arrays.asList();
}
}
public static <T> Optional<T> parseValue(final Object value, final Class<T> type, final Function<String, Object> converter) {
if (null != value) {
if (type.isInstance(value)) {
return Optional.ofNullable(type.cast(value));
} else {
if (type.isEnum()) {
final Enum<?> anEnum = Utilities.searchEnum((Class<? extends Enum<?>>) type, value.toString());
return Optional.ofNullable(type.cast(anEnum));
} else if (type.equals(String.class)) {
return Optional.ofNullable(type.cast(value.toString()));
} else {
if(value.getClass().equals(String.class) && null !=converter){
T processedValue = null;
try {
processedValue = type.cast(converter.apply(value.toString()));
return (null == processedValue) ? Optional.empty() : Optional.ofNullable(processedValue);
}catch(final Exception ex){
return Optional.empty();
}
}else {
//throw new PairingException(String.format("unable to cast %s to type %s", key, type));
return Optional.empty();
}
}
}
} else {
return Optional.empty();
}
}
public static <T> Optional<T> parseValue(final Object value, final Class<T> type) {
final Function<String, Object> converter = PrimitiveConverter.isSupported(type) ? PrimitiveConverter.getConverter(type) : null;
return parseValue(value, type, converter);
}
public static <T> List<T> parseList(final Object value, final Class<T> type) {
if (null != value) {
//collection
if (value instanceof Collection<?>) {
return (List<T>) value;
}
//array
else if (value.getClass().isArray()) {
Class<?> arrayType = value.getClass().getComponentType();
if (arrayType.equals(type)) {
return (List<T>) Arrays.asList((T[]) value);
}
//string array
if (arrayType.equals(String.class)) {
return (List<T>) Converter.toList(Arrays.asList((String[]) value), type);
}
//enum array
if (arrayType.isEnum()) {
return (List<T>) Converter
.toEnumList(Arrays.asList((String[]) value), (Class<? extends Enum<?>>) type);
}
return Arrays.asList();
}
//value is of type string
else if (value.getClass().equals(String.class)) {
final List<String> csvList = Arrays.asList(((String) value).split(DEFAULT_LIST_DILIMER));
if (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) {
return Arrays.asList();
}
if (type.isEnum()) {
return (List<T>) Converter.toEnumList(csvList, (Class<? extends Enum<?>>) type);
}
return (List<T>) Converter.toList(csvList, type);
} else {
//if expected type is string - try to do a to string and write to array
if (type.equals(String.class)) {
return (List<T>) Arrays.asList(value.toString());
}
//if expected type matches the value type - create list of that type
else if (type.isInstance(value)) {
return (List<T>) Arrays.asList(type.cast(value));
}
//everything else fails / return blank array
else {
return Arrays.asList();
}
}
} else {
return Arrays.asList();
}
}
public static class PrimitiveConverter{
private static Map<Class<?>, Function<String, Object>> PRIMITIVECONVERTER = new HashMap<>();
static {
PRIMITIVECONVERTER.put(Boolean.class, s -> Boolean.parseBoolean(s));
PRIMITIVECONVERTER.put(Boolean.TYPE, s -> Boolean.parseBoolean(s));
PRIMITIVECONVERTER.put(Byte.class, s -> Byte.parseByte(s));
PRIMITIVECONVERTER.put(Byte.TYPE, s -> Byte.parseByte(s));
PRIMITIVECONVERTER.put(Short.class, s -> Short.parseShort(s));
PRIMITIVECONVERTER.put(Short.TYPE, s -> Short.parseShort(s));
PRIMITIVECONVERTER.put(Integer.class, s -> Integer.parseInt(s));
PRIMITIVECONVERTER.put(Integer.TYPE, s -> Integer.parseInt(s));
PRIMITIVECONVERTER.put(Long.class, s -> Long.parseLong(s));
PRIMITIVECONVERTER.put(Long.TYPE, s -> Long.parseLong(s));
PRIMITIVECONVERTER.put(Float.class, s -> Float.parseFloat(s));
PRIMITIVECONVERTER.put(Float.TYPE, s -> Float.parseFloat(s));
PRIMITIVECONVERTER.put(Double.class, s -> Double.parseDouble(s));
PRIMITIVECONVERTER.put(Double.TYPE, s -> Double.parseDouble(s));
PRIMITIVECONVERTER.put(String.class, s -> s);
}
public static boolean isSupported(final Class type){
return PRIMITIVECONVERTER.keySet().contains(type);
}
public static Function<String, Object> getConverter(final Class<?> paramTypeToCreate) {
return PRIMITIVECONVERTER.get(paramTypeToCreate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment