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));
} }

EnumUtil 链表转换工具类的更多相关文章

  1. Json与javaBean之间的转换工具类

    /**  * Json与javaBean之间的转换工具类  *  * {@code 现使用json-lib组件实现  *    需要  *     json-lib-2.4-jdk15.jar  * ...

  2. 日期转换工具类 CommUtil.java

    package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...

  3. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  4. java 二进制数字符串转换工具类

    java 二进制数字符串转换工具类 将二进制转换成八进制 将二进制转换成十进制 将二进制转换成十六进制 将十进制转换成二进制 package com.iteye.injavawetrust.ad; i ...

  5. DensityUtil【尺寸转换工具类(px、dp互相转换)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于项目中dp.px.sp之间的转换以及指定缩放值下的转换. 效果图 暂不需要 代码分析 常用的方法是px2dip.dip2px: ...

  6. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  7. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

  8. 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)

    Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换) 有时候要做出如下所示的展示文件大小的效果时候,需要对文件大小进行转换,然后再进行相关的代码逻辑编写. 下面是一个Java ...

  9. (转)Android中px与dip,sp与dip等的转换工具类

    功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...

随机推荐

  1. Cocos Creator_发布到微信小游戏平台

    观看官方教程,地址 传送门: http://docs.cocos.com/creator/manual/zh/publish/publish-wechatgame.html CocosCreator接 ...

  2. 如何修改CCS 7.2 代码和注释的颜色以及折叠代码

    默认的字体总是让人感觉很难受,作为一个热爱生活的人,工作的时候也得尽力创造一个舒适的环境----程序猿们,对自己好一点. 首先废话一下--------------------------------- ...

  3. json文件读写函数

    老师代码: import json def op_data(filename,dic=None): if dic:#写入进去 with open(filename,'w',encoding='utf- ...

  4. Ubuntu或linux 运行后台进程运行不挂断的办法

    nohup python ChatReq.py 20000 >>log_cronjob.txt 2>&1 & 之前把nohup去掉,发现就算运行python Chat ...

  5. spring接入swagger后单元测试报错

    2017-05-16 11:58:05.276 INFO 11268 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing ...

  6. ajax冲刺03

    ---恢复内容开始--- 1.jq中ajax封装 简单的$.ajax方法使用示例:请关注 传参类型及数据 <!DOCTYPE html> <html lang="en&qu ...

  7. JS自学笔记03

    JS自学笔记03 1.函数练习: 如果函数所需参数为数组,在声明和定义时按照普通变量名书写参数列表,在编写函数体内容时体现其为一个数组即可,再传参时可以直接将具体的数组传进去 即 var max=ge ...

  8. django之ORM补充

    本篇导航: QuerySet 中介模型 查询优化 extra 一.QuerySet 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句 ...

  9. Wpf 之Canvas介绍

    从这篇文章开始是对WPF中的界面如何布局做一个较简单的介绍,大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感 ...

  10. delphi TTBXToolBar 增加外部控件

    这样可以引用外部控件,还是比较方便