一、简介
枚举类,写起来代码比较优雅,一个对象的所有的状态值和状态名称都在一个枚举类中,方便维护。另外可以有一些特殊用法,如接口枚举集合(适用于枚举值冲突的情况),【单例模式TODO】等。
二、代码
2.0 情景
比方说:我现在有一个系统类对象,里面有一个状态字段,0对应删除,1对应正常。无论在页面显示还是下载EXCEL在代码中转换还是新增修改的时候设置状态,都可以用一个枚举类来维护,然后根据索引获得对应名称。之前一直在列表页面显示,是将状态值返回,然后页面根据不同的状态值显示不同的状态名称,现在看来感觉比较low,直接在代码中进行转换,一个get就可以了。不断追求优雅写法,要有点追求嘛, 没有追求,和码畜般的咸鱼有什么分别 哈哈。。。。。
2.1 代码
package com.authorization.privilege.constant.system;
/**
* @author qjwyss
* @date 2018/12/4
* @description 系统对象枚举类
*/
public enum SysSystemEnum {
STATE_DEL("删除", 0), STATE_NORMAL("正常", 1);
private String name;
private Integer intIndex;
private String stringIndex;
private SysSystemEnum(String name, Integer intIndex) {
this.name = name;
this.intIndex = intIndex;
}
private SysSystemEnum(String name, String stringIndex) {
this.name = name;
this.stringIndex = stringIndex;
}
/**
* 根据整型索引得到对应的名称
*
* @param intIndex
* @return 名称
*/
public static String getName(Integer intIndex) {
for (SysSystemEnum e : SysSystemEnum.values()) {
if (e.getIntIndex().equals(intIndex)) {
return e.name;
}
}
return null;
}
/**
* 根据字符串型索引得到对应的名称
*
* @param stringIndex
* @return 名称
*/
public static String getName(String stringIndex) {
for (SysSystemEnum e : SysSystemEnum.values()) {
if (e.getStringIndex().equals(stringIndex)) {
return e.name;
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getIntIndex() {
return intIndex;
}
public void setIntIndex(Integer intIndex) {
this.intIndex = intIndex;
}
public String getStringIndex() {
return stringIndex;
}
public void setStringIndex(String stringIndex) {
this.stringIndex = stringIndex;
}
}
2.2 使用
2.2.1 获取名称
List<SysSystemVO> sysSystemVOList = this.sysSystemReadMapper.selectSysSystemVOList(sysSystemVO);
if (!CollectionUtils.isEmpty(sysSystemVOList)) {
sysSystemVOList.forEach(eachSysSystemVO -> {
dataList.add(Arrays.asList(
eachSysSystemVO.getSystemName(),
eachSysSystemVO.getSystemKey(),
eachSysSystemVO.getDescription(),
SysSystemEnum.getName(eachSysSystemVO.getState()),
eachSysSystemVO.getCreateUid(),
eachSysSystemVO.getCreateTime().toString()
));
});
}
2.2.2 获取对应的值
public ResultVO<Void> saveSysSystem(SysSystemVO sysSystemVO) throws Exception {
SysSystem sysSystem = new SysSystem();
sysSystem.setSid(CommonUtil.generatorId());
sysSystem.setSystemName(sysSystemVO.getSystemName());
sysSystem.setSystemKey(sysSystemVO.getSystemKey());
sysSystem.setDescription(sysSystemVO.getDescription());
sysSystem.setCreateUid(sysSystemVO.getLoginUid());
sysSystem.setCreateTime(new Date());
sysSystem.setState(SysSystemEnum.STATE_NORMAL.getIntIndex());
this.sysSystemWriteMapper.insertSelective(sysSystem);
return ResultVO.getSuccess("新增系统成功");
}
三、额外应用
3.1 接口枚举常量
3.1.0 场景
如果在同一个枚举类中定义冲突的值的时候,编译可以通过,但是在根据值获取名称的时候会发生覆盖的情况。
自定义的枚举类都继承Enum类,并且是final类型, 所以不能继承, 但是可以实现接口,该方式就是接口枚举集合方式,通常用来解决枚举值冲突的情况。当枚举的值存在相同冲突的时候,需要使用该方式。比方说现在有一个系统对象,里面包含状态变量:0删除,1正常;还包含类型变量:1菜单 2 按钮,这时候为1的值在一个enum中会发生冲突,这时候就可以使用枚举接口来汇总多个枚举类。
3.1.1 枚举常量接口
3.1.2 枚举值接口
/**
* @author qjwyss
* @description 枚举常量接口
*/
public interface StandardTraceConstant {
/**
* 是否对客户显示
*/
enum ShowCustomer implements StandardTraceConstant {
NOT_SHOW(0, "不显示"), SHOW(1, "显示");
private Integer key;
private String value;
ShowCustomer(Integer key, String value) {
this.key = key;
this.value = value;
}
/**
* 根据整型索引得到对应的名称
*
* @param key
* @return value
*/
public static String getValue(Integer key) {
for (ShowCustomer e : ShowCustomer.values()) {
if (e.key.equals(key)) {
return e.value;
}
}
return null;
}
/**
* 判断是否包含value
*
* @param value
* @return 如果包含value, 则返回对应的key, 否则返回null
*/
public static Integer containValue(String value) {
for (ShowCustomer e : ShowCustomer.values()) {
if (e.value.equals(value)) {
return e.key;
}
}
return null;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* 状态
*/
enum State implements StandardTraceConstant {
EXCEPTION(0, "异常"), NORMAL(1, "正常");
private Integer key;
private String value;
State(Integer key, String value) {
this.key = key;
this.value = value;
}
/**
* 根据整型索引得到对应的名称
*
* @param key
* @return value
*/
public static String getValue(Integer key) {
for (State e : State.values()) {
if (e.key.equals(key)) {
return e.value;
}
}
return null;
}
/**
* 判断是否包含value
*
* @param value
* @return 如果包含value, 则返回对应的key, 否则返回null
*/
public static Integer containValue(String value) {
for (State e : State.values()) {
if (e.value.equals(value)) {
return e.key;
}
}
return null;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* 标准轨迹导入失败原因
*/
enum ImportErrorInfo implements StandardTraceConstant {
/**
* 标准轨迹代码不能为空
*/
ERROR_TRACE_CODE_NOT_ALLOW_NULL,
/**
* 标准轨迹代码格式不合法
*/
ERROR_TRACE_CODE_STYLE_ILLEGAL,
/**
* 环节代码不能为空
*/
ERROR_CYCLE_CODE_NOT_ALLOW_NULL,
/**
* 环节代码格式不合法
*/
ERROR_CYCLE_CODE_STYLE_ILLEGAL,
/**
* 节点代码不能为空
*/
ERROR_NODE_CODE_NOT_ALLOW_NULL,
/**
* 节点代码格式不合法
*/
ERROR_NODE_CODE_STYLE_ILLEGAL,
/**
* 标准轨迹中文名称不能为空
*/
ERROR_TRACE_NAME_CN_NOT_ALLOW_NULL,
/**
* 标准轨迹英文名称不能为空
*/
ERROR_TRACE_NAME_EN_NOT_ALLOW_NULL,
/**
* 客户是否可见不能为空
*/
ERROR_SHOW_CUSTOMER_NOT_ALLOW_NULL,
/**
* 客户是否可见值不合法,只能是 是 或 否
*/
ERROR_SHOW_CUSTOMER_VALUE_ILLEGAL,
/**
* 状态不能为空
*/
ERROR_STATE_NOT_ALLOW_NULL,
/**
* 状态值不合法,只能是 正常 或 异常
*/
ERROR_STATE_VALUE_ILLEGAL,
/**
* 环节代码不存在
*/
ERROR_CYCLE_CODE_NOT_EXIST,
/**
* 节点代码不存在
*/
ERROR_NODE_CODE_NOT_EXIST,
/**
* 标准轨迹代码已经存在
*/
ERROR_TRACE_CODE_ALREADY_EXIST,
/**
* 节点代码不属于环节代码
*/
ERROR_NODE_CODE_NOT_BELONG_CYCLE,
/**
* 标准轨迹代码重复
*/
ERROR_REPEAT_TRACE_CODE,
/**
* 备注格式不合法
*/
ERROR_REMARK_STYLE_ILLEGAL;
}
}
TODO: 枚举不能继承 也不能使用泛型 那这块代码怎么优化 ? 好多重复的?????? TODO 待研究
3.2 单例模式 TODO