policeSecurity/policeSecurityServer/src/main/java/com/changhu/common/db/BaseEnum.java

45 lines
1.1 KiB
Java
Raw Normal View History

2024-08-29 17:06:00 +08:00
package com.changhu.common.db;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.changhu.common.exception.MessageException;
/**
* author: luozhun
* desc: BaseEnum
* createTime: 2023/8/16 17:38
*/
public interface BaseEnum<V> {
/**
* 获取值
*
* @return V
*/
V getValue();
/**
* 获取文本
*
* @return string
*/
String getLabel();
@SuppressWarnings("unchecked")
static <P, T extends BaseEnum<P>> T valueOf(Class<? extends BaseEnum<P>> enumType, P code) {
BaseEnum<P>[] enumConstants = enumType.getEnumConstants();
for (BaseEnum<P> pBaseEnum : enumConstants) {
if (ObjectUtil.equals(code, pBaseEnum.getValue())) {
return (T) pBaseEnum;
}
}
throw new MessageException("不存在值为:{} 的【{}】对象!", code.toString(), enumType.componentType().getSimpleName());
}
default Object serializer() {
return Dict.of(
"value", this.getValue(),
"label", this.getLabel()
);
}
}