java枚举 enum

一、介绍

二、用法举例:

public enum DateTypeEnums {

    DAY(0,'天'),
    MONTH(1,"月"),
    WEEK(2,"周");

    public final int code;

    public final String message;

    DateTypeEnums(int code, String message ) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return message;
    }

}

三、枚举在switch中的写法:

错误写法:

ResultStructureEnum type = ResultStructureEnum.valueOf(userType);
switch (type) {
case ResultStructureEnum.STUDENT:
    ...
    break;
case ResultStructureEnum.TEACHER:
    ...
    break;
case ResultStructureEnum.PARENT:
    ...
    break;
...
}
# 这样编译不会通过,提示case后必须是一个常量

正确写法:

ResultStructureEnum type = ResultStructureEnum.valueOf(userType);
switch (type) {
case STUDENT:
    ...
    break;
case TEACHER:
    ...
    break;
case PARENT:
    ...
    break;
...
}
# 即在 case 后面可以直接写枚举类型,不用加枚举类的类名。

如果需要根据类型的编码返回枚举实例,可以在枚举类中加入方法:

/**
     * 根据编号获取对应枚举实例
     *
     * @param statNo
     * @return
     */
    public static DateTypeEnums  getEnum(intcode) {
       
        for (DateTypeEnums  result : DateTypeEnums.values()) {
            if (result.getCode == code) {
                return result;
            }
        }
        return null;
    }

四、enum&&Function

如enum加Function校验数据

package org.demo.annotation.enums;

import org.demo.annotation.constant.MenuValueCheckFunctions;

import java.util.function.Function;

public enum MenuEnums {
    USER_MANAGE("user_manage",1, MenuValueCheckFunctions.CHECK(10)),
    MAIN_MANAGE("main",0, MenuValueCheckFunctions.CHECK_NO_NULL);

    private final String code;
    private final Integer order;
    private final Function<String, String> checkFunction;

    MenuEnums(String code, Integer order, Function<String, String> checkFunction) {
        this.code = code;
        this.order = order;
        this.checkFunction = checkFunction;
    }


    public static String checkValue(MenuEnums menuEnums,String value) {
        if(menuEnums.checkFunction != null){
            return menuEnums.checkFunction.apply(value);
        }
        return value;
    }
}
package org.demo.annotation.constant;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.function.Function;

@Slf4j
public class MenuValueCheckFunctions {
    public static Function<String, String> CHECK(int maxLenth) {
        return value-> {
            if(value != null && value.length() > maxLenth){
                //throw new BaseException("length is too lang");
                log.error(value+" length is too lang");
                return value.substring(0, maxLenth);
            }
            return value;
        };
    }

    public static Function<String, String> CHECK_NO_NULL = value-> {
        if(StringUtils.isEmpty(value)){
            //throw new BaseException("value is null");
            log.error("value is null");
        }
        return value;
    };
}

测试:

public static void main(String[] args) {
        List<MenuDTO> menuDTOS = new ArrayList<>();
        MenuDTO userMenuDTO = MenuDTO.builder()
                .url(MenuEnums.checkValue(MenuEnums.USER_MANAGE,"/myDemo/test/main"))
                .build();
        menuDTOS.add(userMenuDTO);
        MenuDTO mainMenu = MenuDTO.builder()
                .url(MenuEnums.checkValue(MenuEnums.MAIN_MANAGE,""))
                .build();
        menuDTOS.add(mainMenu);
    }

控制台输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值