当前位置:网站首页>枚举通用接口&枚举使用规范
枚举通用接口&枚举使用规范
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);
}
}
边栏推荐
- CVPR 2022 最佳论文候选 | PIP: 6个惯性传感器实现全身动捕和受力估计
- 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
- opencv环境的搭建,并打开一个本地PC摄像头。
- 「小样本深度学习图像识别」最新2022综述
- Hazel engine learning (V)
- [C language] question set of IX
- VHDL实现任意大小矩阵加法运算
- “去虚向实”大潮下,百度智能云向实而生
- How to verify accesstoken in oauth2 protocol
- Cocos2d-x box2d physical engine compilation settings
猜你喜欢
OC, OD, push-pull explanation of hardware
Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
Do you know the five most prominent advantages of E-bidding?
Variables, process control and cursors (MySQL)
Flink task exit process and failover mechanism
IDEA重启后无法创建Servlet文件的解决方案
[tools] basic concept of database and MySQL installation
VHDL实现任意大小矩阵乘法运算
Significance and measures of source code confidentiality
[cpk-ra6m4 development board environment construction based on RT thread studio]
随机推荐
校招行测笔试-数量关系
VHDL实现任意大小矩阵乘法运算
25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
树莓派设置静态ip
OC, OD, push-pull explanation of hardware
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
杰理之开启经典蓝牙 HID 手机的显示图标为键盘设置【篇】
杰理之发射端在接收端关机之后假死机【篇】
如何替换模型的骨干网络(backbone)
尚硅谷JVM-第一章 类加载子系统
Cryptography series: detailed explanation of online certificate status protocol OCSP
树莓派设置wifi自动连接
SQL Tuning Advisor一个错误ORA-00600: internal error code, arguments: [kesqsMakeBindValue:obj]
The first symposium on "quantum computing + application of financial technology" was successfully held in Beijing
Leetcode-02 (linked list question)
Flink Task退出流程与Failover机制
How to replace the backbone of the model
sshd[12282]: fatal: matching cipher is not supported: aes256- [email protected] [preauth]
[C language] question set of IX
“去虚向实”大潮下,百度智能云向实而生