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. python:什么是单例?一个简单的单例

    单例:即一个类只能生成唯一的一个实例,python中的类如果没有被实例化,则cls._instance为None 如下: class Singleton(object): def __new__(cl ...

  2. NodeJS 模块&函数

    NodeJS 模块&函数 nodejs的多文件操作通过模块系统实现,模块和文件一一对应.文件本身可以是javascript代码.JSON或编译过的C/C++扩展 基本用法 nodeJS通过ex ...

  3. git 一些常见问题 总结

    问题1: Auto packing the repository in background for optimum performance. See "git help gc" ...

  4. 通过TopShelf快速开发服务程序

    我之前在文章中介绍过使用NSSM将exe封装为服务,这种方式我个人是比较喜欢的,一来原始文件不受服务的开发约束,二来也可以提供简单的日志系统.线程守护等功能,是我个人比较倾向的行为.但是,有的场景下, ...

  5. Javascript 字符串(二)常用操作整理

    一.js获取字符串的字节数 这个好使--- function getBytesLength(str) { // 在GBK编码里,除了ASCII字符,其它都占两个字符宽 return str.repla ...

  6. 具有相同名称 的类/接口已在使用。请使用类定制设置来解决此冲突。java调用第三方的webservice应用实例

    WSDLToJava Error: http://10.96.84.124:81/BTRPWebServiceForSMB/OnSMBOrderService.svc?xsd=xsd0 [0,0]: ...

  7. Chrome 开发者控制台中,你可能意想不到的功能

    Chrome 有内置的开发者工具.它拥有丰富的特性,比如元素(Elements).网络(Network)和安全(Security).今天,我们主要关注一下 JavaScript 控制台. 当我最初写代 ...

  8. 多个组件联合打印输出——PrintableComponentLink

    DevExpress强大到难以想象,其很多组件.接口.自定义事件可完成较好的效果,可节省不少事.如下图所示,用的两个ChartControl.一个GridControl制作的平断面示意图,里面涉及不少 ...

  9. 树莓派raspberry pi配置无线路由器AP

    raspi-config进入系统配置面板 进行 Expand Filesystem  扩展文件系统否则备份系统的时候备份文件会急速膨胀. (1)配置网络环境nano /etc/network/inte ...

  10. BAT 删除隐藏文件

    删除文件 del命令参数说明/F   强制删除文件./S      从所有子目录删除指定文件./Q      安静模式.删除全局通配符时,不要求确认./A      根据属性选择要删除的文件. 删除指 ...