EnumUtil 链表转换工具类
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 链表转换工具类的更多相关文章
- Json与javaBean之间的转换工具类
/** * Json与javaBean之间的转换工具类 * * {@code 现使用json-lib组件实现 * 需要 * json-lib-2.4-jdk15.jar * ...
- 日期转换工具类 CommUtil.java
package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- java 二进制数字符串转换工具类
java 二进制数字符串转换工具类 将二进制转换成八进制 将二进制转换成十进制 将二进制转换成十六进制 将十进制转换成二进制 package com.iteye.injavawetrust.ad; i ...
- DensityUtil【尺寸转换工具类(px、dp互相转换)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于项目中dp.px.sp之间的转换以及指定缩放值下的转换. 效果图 暂不需要 代码分析 常用的方法是px2dip.dip2px: ...
- 【Java】字节数组转换工具类
import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...
- Json转换工具类(基于google的Gson和阿里的fastjson)
在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...
- 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)
Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换) 有时候要做出如下所示的展示文件大小的效果时候,需要对文件大小进行转换,然后再进行相关的代码逻辑编写. 下面是一个Java ...
- (转)Android中px与dip,sp与dip等的转换工具类
功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...
随机推荐
- 去除input默认带的上下按钮与修改placeholder的默认颜色、背景、placeholder内容的大小
有时候需要用input元素中type属性值为number时,会出现默认的上下按钮, 去掉input默认的上下按钮:兼容性写法如下 input[type='number']::-webkit-outer ...
- html页面布局之table布局:
table布局: table来做整体页面的布局,布局技巧归纳如下: (1)按照设计图的尺寸设置表格的宽高以及单元格的宽高 (2)将表格的border.cellpadding.cellspacing全部 ...
- Updating and Publishing a NuGet Package - Plus making NuGet packages smarter and avoiding source edits with WebActivator
I wrote a post a few days ago called "Creating a NuGet Package in 7 easy steps - Plus using NuG ...
- springboot配置双数据源 MySQL和SqlServer
1. pom文件的驱动jar包加上去, compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8' 2. application.yml sprin ...
- SpringBoot项目接口第一次访问慢的问题
SpringBoot的接口第一次访问都很慢,通过日志可以发现,dispatcherServlet不是一开始就加载的,有访问才开始加载的,即懒加载. 2019-01-25 15:23:46.264 IN ...
- 需要看源码的java类
1.数据结构相关的类,如String.ArrayList,LinkedList,HashMap和ConcurrentHashMap等等.2.线程并发相关的类,如Synchronized.Reentra ...
- go微服务框架go-micro深度学习(一) 整体架构介绍
产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂,变成一个大项目,如果前期项目架构没设计的不好,代码会越来越臃肿,难以维护,后期的每次产品迭代上线都会牵一发而动全身.项目 ...
- Unix环境高级编程-阻塞访问原理——等待队列
有些时候,一个系统调用可能无法马上取到或者送出数据:一个温度采集器如果没有采用中断或者轮询的策略,而是在用户发出请求时才进行采集,并在一定的时间后返回结果.如果用户程序希望调用read或write并且 ...
- MATLAB 条形图添加多个图例
MATLAB 条形图添加多个图例: 1)只有一个图例: 2)两个图例:
- 一个会学习(观察->活学->求变)的人,在任何领域都能变得强大无比
开始今天的话题之前,我说个小故事. 很早以前有一部美剧,叫<Hero>. 大概讲的是正反两派都是一群有超能力的人,彼此为了某个巨大的阴谋互相撕逼了十多集.虽然剧情很老套,但是让 ...