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. 可以直接用的“ html转字符串string”方法

    //html转字符串 -(NSString *)filterHTMLString:(NSString *)html { NSScanner * scanner = [NSScanner scanner ...

  2. Maven实战(七)——常用Maven插件介绍(上)

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的.进一步说,每个任务对应了 ...

  3. Servlet(4)—一个简单的Servlet实例

    简单实例 页面请求登陆,提交表单数据 <body> <form action="loginServlet" method="get"> ...

  4. Deepin 15.4 编译安装 LNMP(PHP 5.6.31 + Nginx 1.12.1 + MySQL 5.6.36)

    先查看先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 编译 Nginx #安装依赖库 sudo apt-get -y i ...

  5. [Web 前端] qs.parse()、qs.stringify()使用方法

    cp from : https://blog.csdn.net/suwu150/article/details/78333452 qs是一个npm仓库所管理的包,可通过npm install qs命令 ...

  6. Asp.Net Core 404处理

    在使用Asp.Net Core Mvc时 404处理整理如下 一.自带404状态处理 1.控制器视图子弹404视图 NotFoundResult,NotFoundObjectResult // // ...

  7. 根据javabean转换为mysql建表语句与mapper内容

    原文地址:  https://www.cnblogs.com/Jeffscnblog/p/10072483.html 一般上,我们会使用数据库表转换为javabean.dao.或是mapper,就叫逆 ...

  8. HRMS(人力资源管理系统)-SaaS架构设计-概要设计实践

    一.开篇 前期我们针对架构准备阶段及需求分析这块我们写了2篇内容<HRMS(人力资源管理系统)-从单机应用到SaaS应用-架构分析(功能性.非功能性.关键约束)-上篇><HRMS(人 ...

  9. jquery 多级联动下拉列表含(数据模型)

    方法 /** * 级联 * 联动 * @param url:访问json数据的地址 * @param param:参数 * @param levelIds:页面下拉标签数组,为联动级数 * @priv ...

  10. Django根据现有数据库建立/更新model

    Django引入外部数据库还是比较方便的,步骤如下: 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自动 ...