枚举类根据values()获取枚举实例

一般的枚举类,我们只写对应的get set方法, 要拿取每个字段的值必须先确认枚举实例

这里运用枚举类的values方法,循环判断实例的值,获取到对应的实例

 

参照方法getEventStatus(int value)



import org.apache.commons.lang3.EnumUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public enum EventStatus {

    LAUNCHED(0, "待处理","发起"),
    ACCEPTED(1, "处理中","认领"),
    REJECTED(2, "待验收","打回"),
    TRANSFERRED(3, "处理中","转交"),
    RESOLVED(4, "待验收","解决"),
    CLOSED(5, "已关闭","关闭");

    private int value;
    private String description;//事件状态
    private String operateType;//用户操作类型

    EventStatus(int value, String description,String operateType) {
        this.value = value;
        this.description = description;
        this.operateType=operateType;
    }

    public static String getValueName(int intValue) {
        return getNameByValue(intValue,"description");
    }

    public static String getOperateName(int intValue) {
        return getNameByValue(intValue,"operateType");
    }

    public static  String getNameByValue(int intValue, String propertyName){
        EventStatus[] arr$ = values();
        int len$ = arr$.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            EventStatus t = arr$[i$];
            if (intValue == t.getValue()) {
                return String.valueOf(dynamicGet(t,propertyName));
            }
        }

        return "";
    }

    private static Object dynamicGet(EventStatus t, String propertyName) {
        try {
            Class clazz=t.getClass();
//            Field field = clazz.getDeclaredField(propertyName);
            PropertyDescriptor pd = new PropertyDescriptor(propertyName,
                    clazz);
            Method getMethod = pd.getReadMethod();//获得get方法
            Object o = getMethod.invoke(t);//执行get方法返回一个Object
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static EventStatus getEventStatus(int value){
        EventStatus[] arr$=values();
        int len$ = arr$.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            EventStatus status = arr$[i$];
            if (status.getValue()==value) {
                return status;
            }
        }

        return null;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getOperateType() {
        return operateType;
    }

    public void setOperateType(String operateType) {
        this.operateType = operateType;
    }

    public static void main(String[] args) {
        System.out.println(EventStatus.getEventStatus(0).getDescription());;
        System.out.println(getNameByValue(0,"description"));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值