45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
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()
|
|
);
|
|
}
|
|
}
|