传入参数判断是否包含在枚举类中

枚举类:

public enum  UserRewardTaskType {

    UPDATE_NAME_REWARD_MONEY("1000","测试1"),
    UPDATE_HEAD_REWARD_MONEY("1001","测试2"),
    FILL_ADDRESS_REWARD_MONEY("1002","测试3"),
    ;
    //code是1000,1001,1002
    private String code;
    //desc是测试1,2,3
    private String desc;

    private UserRewardTaskType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

判断:

 @Override
 public void before (int task_id) {
                boolean has_task=false;
                //遍历枚举类判断参数是否在枚举类中
                for(UserRewardTaskType u:UserRewardTaskType.values()){
                    if(u.getCode().equals(String.valueOf(task_id))){
                        has_task=true;
                        break;
                    }
                }
                //不存在就提示错误信息
                if (!has_task){
                    logger.error(MessageFormat.format("addCompleteTask,参数 task_id:{0}",task_id));
                    throw new ShopException(new ShopError(ShopResultCode.USER_REWARD_TASK));
                }
            }

 

你可能感兴趣的:(传入参数判断是否包含在枚举类中)