feat: 配置支持读取枚举值

This commit is contained in:
2024-05-21 02:27:58 +08:00
parent 059377b5d7
commit c0ebee6976
6 changed files with 58 additions and 6 deletions

View File

@@ -86,8 +86,21 @@ public abstract class AbstractConfiguration implements Configuration {
}
@Override
public <T extends Enum<?>> Enum<?> getEnum(Class<T> clazz, String key) {
return null;
@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends Enum> T getEnum(Class<T> clazz, String key) {
String rawValue = getString(key);
Assert.notNull(rawValue);
return (T)Enum.valueOf(clazz, rawValue);
}
@Override
@SuppressWarnings({"rawtypes"})
public <T extends Enum> T getEnum(Class<T> clazz, String key, T defaultValue) {
T value = null;
try {
value = getEnum(clazz, key);
}catch (Exception ignored){}
return value == null ? defaultValue : value;
}
/**

View File

@@ -3,6 +3,7 @@ package com.serliunx.ddns.config;
import com.serliunx.ddns.support.Refreshable;
/**
* 配置信息逻辑定义
* @author SerLiunx
* @since 1.0
*/
@@ -68,5 +69,24 @@ public interface Configuration extends Refreshable {
*/
Boolean getBoolean(String key, Boolean defaultValue);
<T extends Enum<?>> Enum<?> getEnum(Class<T> clazz, String key);
/**
* 获取枚举值
* @param clazz 枚举类
* @param key 键
* @return 枚举值
* @param <T> 枚举类型参数
*/
@SuppressWarnings("rawtypes")
<T extends Enum> T getEnum(Class<T> clazz, String key);
/**
* 获取枚举值
* @param clazz 枚举类
* @param key 键
* @param defaultValue 默认值
* @return 枚举值
* @param <T> 枚举类型参数
*/
@SuppressWarnings("rawtypes")
<T extends Enum> T getEnum(Class<T> clazz, String key, T defaultValue);
}