当前位置:网站首页>枚举通用接口&枚举使用规范
枚举通用接口&枚举使用规范
2022-07-06 20:18:00 【InfoQ】
- 数据表里字段值为有穷序列的字段,对应到程序里特定的枚举。字段数据类型尽量用varchar取代int(或tinyint)。毋庸置疑,字母组合总比0、1、2、3这样的数字易于识别。
- 数据表字段如果有对应的枚举,则,在字段注释上要标明枚举类名,方便程序溯源。
- 枚举一般有两部分,一个是枚举项值,一个是枚举描述。那么,这两个属性怎么命名呢? code和desc?还是value和desc?还是key和value?本文重点针对这个问题阐述。
@Getter
@AllArgsConstructor
public enum LevelEnum {
FIRST("FIRST", "一级"),
SECOND("SECOND", "二级"),
THIRD("THIRD", "三级");
private String code;
private String value;
public static LevelEnum getBeanByCode(String code) {
LevelEnum[] statusEnums = LevelEnum.values();
for (LevelEnum v : statusEnums) {
if (v.getCode().equals(code)) {
return v;
}
}
return null;
}
}@Getter
@AllArgsConstructor
public enum OrderStatusEnum {
INIT("INIT", "初始"),
ACCOUNTING("ACCOUNTING", "记账中"),
SUCCESS("SUCCESS", "付款成功"),
FAILED("FAILED", "付款失败");
private String key;
private String description;
public static OrderStatusEnum getBeanByCode(String code) {
OrderStatusEnum[] values = OrderStatusEnum.values();
for (OrderStatusEnum v : values) {
if (v.getKey().equals(code)) {
return v;
}
}
return null;
}
}@AllArgsConstructor
public enum ProductEnum {
BOSSKG("BOSS开工"),
HUICHUXING("惠出行"),
SICHEBANGONG("私车办公"),
YOUFU("优付"),
UNKNOWN("未知"),
;
private String description;
private String getCode(){
return this.toString();
}
public String getDescription() {
return description;
}
public static ProductEnum getBean(String value) {
ProductEnum[] values = ProductEnum.values();
for(ProductEnum temp : values){
if(temp.getCode().equals(value)){
return temp;
}
}
return ProductEnum.UNKNOWN;
}
}@Getter
@AllArgsConstructor
public enum SeasonEnum {
SPRING(1, "春"),
SUMMER(2, "夏"),
AUTUMN(3, "秋"),
WINTER(4, "冬");
private int code;
private String description;
public static SeasonEnum getBeanByCode(Integer code) {
if (null == code) return null;
SeasonEnum[] values = SeasonEnum.values();
for (SeasonEnum temp : values) {
if (temp.getCode() == code) {
return temp;
}
}
return null;
}
}/**
* 如若枚举名称和实际值不同,请务必重写getKey方法
* 枚举定义规范:枚举名切记大写,描述尽量清晰,不要出现错误拼写,请自觉仔细检查
* 例如:
* MONDAY("星期一"),
* TUESDAY("星期二")
*
* @author shaozhengmao
* @create 2021-06-21 10:18 上午
*/
public interface EnumAbility<T> {
/**
* 返回枚举实际值
* @return
*/
T getCode();
/**
* 返回枚举描述
*
* @return 枚举说明
*/
String getDescription();
/**
* 对比当前枚举对象和传入的枚举值是否一致(String类型会忽略大小写)
* 当前枚举项是否匹配远端传入的值(比如:数据库的字段值、rpc传入的参数值)
*
* @param enumCode 枚举code
* @return 是否匹配
*/
default boolean codeEquals(T enumCode) {
if (enumCode == null) return false;
if (enumCode instanceof String) {
return ((String) enumCode).equalsIgnoreCase((String) getCode());
} else {
return Objects.equals(this.getCode(), enumCode);
}
}
/**
* 对比两个枚举项是否完全相同(==)
*
* @param anotherEnum 枚举
* @return 是否相同
*/
default boolean equals(EnumAbility<T> anotherEnum) {
return this == anotherEnum;
}
}
@Getter
@AllArgsConstructor
public enum LevelEnum implements EnumAbility<String> {
FIRST("FIRST", "一级"),
SECOND("SECOND", "二级"),
THIRD("THIRD", "三级");
private String code;
private String value;
@Override
public String getDescription() {
return value;
}
/**
* 2021-12-18 23:00 zhanggz:本方法存在歧义,请使用{@link #getDescription()}
* @return
*/
@Deprecated
public String getValue() {
return value;
}
public static LevelEnum getBeanByCode(String code) {
return (LevelEnum) EnumAbilityUtil.getEnumByCode(LevelEnum.class, code);
}
}@Getter
@AllArgsConstructor
public enum OrderStatusEnum implements EnumAbility<String> {
INIT("INIT", "初始"),
ACCOUNTING("ACCOUNTING", "记账中"),
SUCCESS("SUCCESS", "付款成功"),
FAILED("FAILED", "付款失败");
private String key;
private String description;
@Override
public String getCode() {
return key;
}
/**
* 2021-12-18 23:00 zhanggz:本方法存在歧义,请使用{@link #getCode()}
* @return
*/
@Deprecated
public String getKey() {
return key;
}
public static OrderStatusEnum getBeanByCode(String code) {
return (OrderStatusEnum) EnumAbilityUtil.getEnumByCode(OrderStatusEnum.class, code);
}
}@AllArgsConstructor
public enum ProductEnum implements EnumAbility<String> {
BOSSKG("BOSS开工"),
HUICHUXING("惠出行"),
SICHEBANGONG("私车办公"),
YOUFU("优付"),
UNKNOWN("未知"),
;
private String description;
@Override
private String getCode(){
return this.toString();
}
@Override
public String getDescription() {
return description;
}
public static ProductEnum getBean(String code) {
return (ProductEnum) EnumAbilityUtil.getEnumByCode(ProductEnum.class, code);
}
}@Getter
@AllArgsConstructor
public enum SeasonEnum implements EnumAbility<Integer> {
SPRING(1, "春"),
SUMMER(2, "夏"),
AUTUMN(3, "秋"),
WINTER(4, "冬");
private Integer code;
private String description;
public static SeasonEnum getBeanByCode(Integer code) {
if (null == code) return null;
return (SeasonEnum) EnumAbilityUtil.getEnumByCode(SeasonEnum.class, code);
}
}边栏推荐
- 制作(转换)ico图标
- Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
- MOS transistor realizes the automatic switching circuit of main and auxiliary power supply, with "zero" voltage drop and static current of 20ua
- 2022.6.28
- 腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
- 房费制——登录优化
- 如何替换模型的骨干网络(backbone)
- 树莓派设置静态ip
- 19.(arcgis api for js篇)arcgis api for js线采集(SketchViewModel)
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
猜你喜欢

Mathematical induction and recursion

Hazel engine learning (V)

Construction of knowledge map of mall commodities

leetcode-02(链表题)

装饰设计企业网站管理系统源码(含手机版源码)

函数重入、函数重载、函数重写自己理解

input_ delay

RestClould ETL 社区版六月精选问答

Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application

Development of wireless communication technology, cv5200 long-distance WiFi module, UAV WiFi image transmission application
随机推荐
Jerry's transmitter crashed after the receiver shut down [chapter]
Another million qubits! Israel optical quantum start-up company completed $15million financing
Leetcode-02 (linked list question)
How to replace the backbone of the model
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
Jerry's phonebook acquisition [chapter]
[swift] learning notes (I) -- familiar with basic data types, coding styles, tuples, propositions
The latest 2022 review of "small sample deep learning image recognition"
Opencv environment, and open a local PC camera.
Le tube MOS réalise le circuit de commutation automatique de l'alimentation principale et de l'alimentation auxiliaire, et la chute de tension "zéro", courant statique 20ua
杰理之关于 DAC 输出功率问题【篇】
19.(arcgis api for js篇)arcgis api for js线采集(SketchViewModel)
New benchmark! Intelligent social governance
sshd[12282]: fatal: matching cipher is not supported: aes256- [email protected] [preauth]
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
cocos3——8. Implementation Guide for beginners
迷失在MySQL的锁世界
校招行测笔试-数量关系