讲真,java的枚举类提供的方法太少了,Enum只有两个valueOf可以搞,如果碰上需要传入枚举类型的值,获取枚举类对象或者枚举类型名称的时候,只能干瞪眼,或者循环比对。最近项目就遇到这种,而且感觉以后以及别人都不会少遇到,所以就写了个帮助类,希望以后能用上吧。这个帮助类是基于以下枚举类的类型提供的:
public enum DeleteStatusEnum { /** * 值为0(就是NOT_DELETE)是数据库默认值,未删除; * 值为1(就是HAS_DELETE)是已经被逻辑删除了 */ NOT_DELETE("未删除","0"), HAS_DELETE("已删除","1"); private String typeName; private String typeCode; private DeleteStatusEnum(String typeName,String typeCode){ this.typeName = typeName; this.typeCode = typeCode; } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public String getTypeCode() { return typeCode; } public void setTypeCode(String typeCode) { this.typeCode = typeCode; } }
帮助类的内容:
package com.hikvision.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 此帮助类严格限定为有typeName和typeCode的枚举类,如定义枚举类型为 ADMIN(typeName,typeCode)这种 * * ClassName: EnumOperatorUtil.java * Copyright xiehao * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年2月28日 * @author xiehao * @version V1.0 * @since JDK 1.8 * @see */ public class EnumHelperUtil{ /** * indexOf,传入的参数ordinal指的是需要的枚举值在设定的枚举类中的顺序,以0开始 * T * @param clazz * @param ordinal * @return * @author xiehao */ public static <T extends Enum<T>> T indexOf(Class<T> clazz, int ordinal){ return (T) clazz.getEnumConstants()[ordinal]; } /** * nameOf,传入的参数name指的是枚举值的名称,一般是大写加下划线的 * T * @param clazz * @param name * @return Enum T * @author xiehao */ public static <T extends Enum<T>> T nameOf(Class<T> clazz, String name){ return (T) Enum.valueOf(clazz, name); } /** * 使用枚举类型对应的typeCode获取枚举类型 * T * @param clazz 枚举类的class * @param getTypeCodeMethodName 传入的typeCode的get方法 * @param typeCode 传入的typeCode值,这个方法为String类型 * @return * @author xiehao */ public static <T extends Enum<T>> T getByStringTypeCode(Class<T> clazz,String getTypeCodeMethodName, String typeCode){ T result = null; try{ T[] arr = clazz.getEnumConstants(); Method targetMethod = clazz.getDeclaredMethod(getTypeCodeMethodName); String typeCodeVal = null; for(T entity:arr){ typeCodeVal = targetMethod.invoke(entity).toString(); if(typeCodeVal.contentEquals(typeCode)){ result = entity; break; } } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return result; } /** * 使用枚举类型对应的typeCode获取枚举类型 * T * @param clazz 枚举类的class * @param getTypeCodeMethodName 传入的typeCode的get方法 * @param typeCode 传入的typeCode值,这个方法为Integer类型 * @return * @author xiehao */ public static <T extends Enum<T>> T getByIntegerTypeCode(Class<T> clazz,String getTypeCodeMethodName, Integer typeCode){ T result = null; try{ T[] arr = clazz.getEnumConstants(); Method targetMethod = clazz.getDeclaredMethod(getTypeCodeMethodName); Integer typeCodeVal = null; for(T entity:arr){ typeCodeVal = Integer.valueOf(targetMethod.invoke(entity).toString()); if(typeCodeVal.equals(typeCode)){ result = entity; break; } } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return result; } /** * 使用枚举类型对应的typeName获取枚举类型 * T * @param clazz 枚举类的class * @param getTypeNameMethodName 传入的typeName的get方法 * @param typeName 传入的typeName值,这个方法为String类型 * @return * @author xiehao */ public static <T extends Enum<T>> T getByStringTypeName(Class<T> clazz,String getTypeNameMethodName, String typeName){ T result = null; try{ T[] arr = clazz.getEnumConstants(); Method targetMethod = clazz.getDeclaredMethod(getTypeNameMethodName); String typeNameVal = null; for(T entity:arr){ typeNameVal = targetMethod.invoke(entity).toString(); if(typeNameVal.contentEquals(typeName)){ result = entity; break; } } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return result; } }
以上内容其实还可以再精简优化,时间有限,就先记录下来吧