package com.das.common.util;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* @Author liangmy
* @Date 2018/3/8
*/
public class EnumUtil {
private static final Integer doublePrecision = 1000;
public static <T> List<EnumObject> getEnum(Class<T> enumClazz) {
return getEnum(enumClazz, null, null);
}
public static <T> List<EnumObject> getEnum(Class<T> enumClazz, String value, String label) {
Set<T> set = new TreeSet<>((o1, o2) -> {
try {
String typeName = o1.getClass().getMethod("getCode").getReturnType().getSimpleName();
if (Double.class.getSimpleName().equalsIgnoreCase(typeName)) {
return (int) (((double) (o1.getClass().getMethod("getCode").invoke(o1))) * doublePrecision - ((double) (o2.getClass().getMethod("getCode").invoke(o2))) * doublePrecision);
} else {
return (int) o1.getClass().getMethod("getCode").invoke(o1) - (int) o2.getClass().getMethod("getCode").invoke(o2);
}
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return 0;
});
set.addAll(Arrays.asList(enumClazz.getEnumConstants()));
List<EnumObject> list = new LinkedList<>();
String getValueMethodName = null == value ? "name" : "get" + value.substring(0, 1).toUpperCase() + value.substring(1);
String getLabelMethodName = null == label ? "getValue" : "get" + label.substring(0, 1).toUpperCase() + label.substring(1);
for (T e : set) {
try {
list.add(new EnumObject(enumClazz.getMethod(getValueMethodName).invoke(e).toString(), enumClazz.getMethod(getLabelMethodName).invoke(e).toString()));
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
return list;
}
public static <T> List<EnumObject> getEnum(List<T> inputList) {
if (CollectionUtils.isEmpty(inputList)) {
return Collections.EMPTY_LIST;
}
if (inputList.get(0) instanceof Enum) {
return getEnum(inputList.toArray(new Enum[0]));
}
return getEnum(inputList, "name");
}
public static <T> List<EnumObject> getEnum(List<T> inputList, String label) {
if (CollectionUtils.isEmpty(inputList)) {
return Collections.EMPTY_LIST;
}
if (inputList.get(0) instanceof Enum) {
return getEnum(inputList.toArray(new Enum[0]), new String[]{label});
}
return getEnum(inputList, "id", label);
}
public static <T> List<EnumObject> getEnum(List<T> inputList, String value, String label) {
if (CollectionUtils.isEmpty(inputList)) {
return Collections.EMPTY_LIST;
}
if (inputList.get(0) instanceof Enum) {
return getEnum(inputList.toArray(new Enum[0]), new String[]{value, label});
}
return getEnum(inputList, value, label, null);
}
public static <T> List<EnumObject> getEnum(List<T> inputList, String[] kwParam) {
if (CollectionUtils.isEmpty(inputList)) {
return Collections.EMPTY_LIST;
}
if (inputList.get(0) instanceof Enum) {
return getEnum(inputList.toArray(new Enum[0]), kwParam);
}
return getEnum(inputList, "id", "name", kwParam);
}
public static <T> List<EnumObject> getEnum(List<T> inputList, String value, String label, String[] kwParam) {
if (CollectionUtils.isEmpty(inputList)) {
return Collections.EMPTY_LIST;
}
Iterator<T> iterator = inputList.iterator();
while (iterator.hasNext()) {
if (iterator.next() == null) {
iterator.remove();
}
}
Integer notNullIndex = 0;
String getValueMethodName = inputList.get(notNullIndex) instanceof String ? "toString" :
inputList.get(notNullIndex) instanceof Integer ? "intValue" :
inputList.get(notNullIndex) instanceof Long ? "longValue" :
inputList.get(notNullIndex) instanceof Double ? "doubleValue" :
"get" + value.substring(0, 1).toUpperCase() + value.substring(1);
String getLabelMethodName = inputList.get(notNullIndex) instanceof String ? "toString" :
inputList.get(notNullIndex) instanceof Integer ? "intValue" :
inputList.get(notNullIndex) instanceof Long ? "longValue" :
inputList.get(notNullIndex) instanceof Double ? "doubleValue" :
"get" + label.substring(0, 1).toUpperCase() + label.substring(1);
Set<T> set = new TreeSet<>();
if (!(inputList.get(notNullIndex) instanceof String)) {
set = new TreeSet<>((T o1, T o2) -> {
try {
Class<?> returnType = o1.getClass().getMethod(getValueMethodName).getReturnType();
if (Integer.class.isAssignableFrom(returnType) || Integer.TYPE.isAssignableFrom(returnType)) {
return (int) ((int) (o1.getClass().getMethod(getValueMethodName).invoke(o1)) - (int) (o2.getClass().getMethod(getValueMethodName).invoke(o2)));
} else if (Double.class.isAssignableFrom(returnType) || Double.TYPE.isAssignableFrom(returnType)) {
return (int) (((double) (o1.getClass().getMethod(getValueMethodName).invoke(o1))) * doublePrecision - ((double) (o2.getClass().getMethod(getValueMethodName).invoke(o2))) * doublePrecision);
} else if (Long.class.isAssignableFrom(returnType) || Long.TYPE.isAssignableFrom(returnType)) {
return (int) ((long) (o1.getClass().getMethod(getValueMethodName).invoke(o1)) - (long) (o2.getClass().getMethod(getValueMethodName).invoke(o2)));
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.getStackTrace();
}
return -1;
});
}
set.addAll(inputList);
List<EnumObject> result = new LinkedList<>();
for (T e : set) {
if (null == e) {
continue;
}
try {
EnumObject enumObject = new EnumObject(e.getClass().getMethod(getValueMethodName).invoke(e).toString(), e.getClass().getMethod(getLabelMethodName).invoke(e).toString());
if (null != kwParam && kwParam.length > 0) {
Map<String, String> kwMap = new HashMap<>(kwParam.length);
for (String key : kwParam) {
String word = e.getClass().getMethod("get" + key.substring(0, 1).toUpperCase() + key.substring(1)).invoke(e).toString();
kwMap.put(key, word);
}
enumObject.setKwParam(kwMap);
}
result.add(enumObject);
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
return result;
}
public static List<EnumObject> getEnum(Enum... enums) {
return getEnum(enums, null);
}
private static List<EnumObject> getEnum(Enum[] enums, String[] kwParam) {
Set<Enum> set = new TreeSet<>((Enum o1, Enum o2) -> {
try {
String getValueMethodName = "getCode";
Class<?> returnType = o1.getClass().getMethod(getValueMethodName).getReturnType();
if (Integer.class.isAssignableFrom(returnType) || Integer.TYPE.isAssignableFrom(returnType)) {
return ((Integer) o1.getClass().getMethod(getValueMethodName).invoke(o1)).compareTo(((Integer) o2.getClass().getMethod(getValueMethodName).invoke(o2)));
} else if (Double.class.isAssignableFrom(returnType) || Double.TYPE.isAssignableFrom(returnType)) {
return ((Double) o1.getClass().getMethod(getValueMethodName).invoke(o1)).compareTo(((Double) o2.getClass().getMethod(getValueMethodName).invoke(o2)));
} else if (Long.class.isAssignableFrom(returnType) || Long.TYPE.isAssignableFrom(returnType)) {
return ((Long) o1.getClass().getMethod(getValueMethodName).invoke(o1)).compareTo((Long) o2.getClass().getMethod(getValueMethodName).invoke(o2));
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.getStackTrace();
}
return -1;
});
set.addAll(Arrays.asList(enums));
List<EnumObject> result = new LinkedList<>();
for (Enum e : set) {
try {
EnumObject enumObject = new EnumObject(e.name(), e.getClass().getMethod("getValue").invoke(e).toString());
if (null != kwParam && kwParam.length > 0) {
Map<String, String> kwMap = new HashMap<>(kwParam.length);
for (String key : kwParam) {
String word = e.getClass().getMethod("get" + key.substring(0, 1).toUpperCase() + key.substring(1)).invoke(e).toString();
kwMap.put(key, word);
}
enumObject.setKwParam(kwMap);
}
result.add(enumObject);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
return result;
}
public static class EnumObject {
private String value;
private String label;
private Map<String, String> kwParam;
public EnumObject(String value, String label) {
this.value = value;
this.label = label;
}
public EnumObject(String value, String label, Map<String, String> kwParam) {
this.value = value;
this.label = label;
this.kwParam = kwParam;
}
public String getValue() {
return value;
}
public String getLabel() {
return label;
}
public Map<String, String> getKwParam() {
return kwParam;
}
public void setKwParam(Map<String, String> kwParam) {
this.kwParam = kwParam;
}
@Override
public String toString() {
return "EnumObject{" +
"value='" + value + '\'' +
", label='" + label + '\'' +
'}';
}
}
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(EnumUtil.getEnum(list));
}
}