当前位置: 首页 > article >正文

java enum code-label模式的使用方法

通常我们定义的枚举类都会有两个字段:code、label,然后我们有会需要两个方法:
1.根据code获得枚举实例对象:getByCode

2.根据code获得对应的label:getLabelByCode

当然可以在每个枚举类中实现这两个方法,但是会发现这两个方法逻辑完全一致,但是又必须每次都写,代码不够简洁,有重复造轮子的嫌疑。

下面给出一种实现方式,解决重复造轮子的问题。

1.创建公共接口

public  interface EnumInterface {

    String getCode();
    String getLabel();

}

2.创建枚举类并实现接口


public enum PlanTypeEnum implements EnumInterface {

    WEEK_PLAN {
        @Override
        public String getCode() {
            return "week_plan";
        }

        @Override
        public String getLabel() {
            return "周计划";
        }
    },
    DAY_PLAN {
        @Override
        public String getCode() {
            return "day_plan";
        }

        @Override
        public String getLabel() {
            return "日计划";
        }
    },
    TEMP_PLAN {
        @Override
        public String getCode() {
            return "temp_plan";
        }

        @Override
        public String getLabel() {
            return "临时计划";
        }
    }
}

3.创建工具类

public class CodeEnumUtil {
    private CodeEnumUtil() {}

    /**
     * 根据code获取枚举
     * @param clazz 枚举类
     * @param code code
     * @return 枚举
     */
    public static EnumInterface getByCode(Class<? extends Enum<?>> clazz, String code) {
        final Enum<?>[] enums = clazz.getEnumConstants();
        for (Enum<?> e : enums) {
            if (e instanceof EnumInterface) {
                EnumInterface enumInterface = (EnumInterface) e;
                if (enumInterface.getCode().equalsIgnoreCase(code)) {
                    return enumInterface;
                }

            }
        }
        return null;
    }

    /**
     * 没有找到时,返回code
     *
     * @param code code
     * @return label
     */
    public static String getLabelByCode(Class<? extends Enum<?>> clazz, String code) {
        EnumInterface enumInterface = getByCode(clazz, code);
        if (enumInterface == null) {
            return code;
        }
        return enumInterface.getLabel();
    }
}

4.使用

CodeEnumUtil.getLabelByCode(PlanTypeEnum.class,"week_plan")


http://www.kler.cn/news/305265.html

相关文章:

  • MATLAB算法实战应用案例精讲-【人工智能】数据血缘分析(概念篇)
  • 计算机视觉学习路线(纯纯小白)
  • idea开发Java程序的步骤及设置
  • Typescript 的类型断言
  • 召回02 Swing 召回通道
  • C#命令行参数解析库System.CommandLine介绍
  • 分享一些智慧农业数据集
  • springboot医院预约挂号系统 ---附源码73444
  • 【计算机网络 - 基础问题】每日 3 题(六)
  • leetcode01——27. 移除元素(双指针)、977. 有序数组的平方(双指针)、209. 长度最小的子数组(双指针/滑动窗口)
  • 准备好启程了:Nuro将授权其自动驾驶系统
  • 北斗盒子:海上安全的智能守护者——落水报警应用案例
  • GO Govaluate
  • 38. 如何在Spring Boot项目中集成MyBatis-Plus?
  • 信通院发布首个《大模型媒体生产与处理》标准,阿里云智能媒体服务作为业界首家“卓越级”通过
  • redis 中缓存 百万级别表的查询数据 出错:Query execution was interrupted
  • 添加选择登录ssh终端
  • RAPIDS AI 加速制造业预测性维护效率
  • 51单片机-蜂鸣器制作音乐(小星星天空之城)
  • 【python因果推断库15】使用 sci-kit learn 模型进行回归断点分析
  • Linux基础-Makefile的编写、以及编写第一个Linux程序:进度条(模拟在 方便下载的同时,更新图形化界面)
  • ubuntu 22.04 ~24.04 如何修改登录背景
  • 【JavaScript】LeetCode:707设计链表
  • Python版《天天酷跑+源码》,详细讲解,手把手教学-python游戏开发
  • jmeter设置全局token
  • (180)时序收敛--->(30)时序收敛三十
  • 大模型教程:使用 Milvus、vLLM 和 Llama 3.1 搭建 RAG 应用
  • 怎么让手机ip地址变化?介绍几种实用方法
  • uniapp 微信小程序自定义tabbar层级低于canvas解决方案
  • 见刊丨“GPU池化”术语发布