feat: 配置支持读取枚举值
This commit is contained in:
@@ -86,8 +86,21 @@ public abstract class AbstractConfiguration implements Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Enum<?>> Enum<?> getEnum(Class<T> clazz, String key) {
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
return null;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.serliunx.ddns.config;
|
|||||||
import com.serliunx.ddns.support.Refreshable;
|
import com.serliunx.ddns.support.Refreshable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 配置信息逻辑定义
|
||||||
* @author SerLiunx
|
* @author SerLiunx
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@@ -68,5 +69,24 @@ public interface Configuration extends Refreshable {
|
|||||||
*/
|
*/
|
||||||
Boolean getBoolean(String key, Boolean defaultValue);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import org.junit.Test;
|
|||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testContext(){
|
public void testContext() {
|
||||||
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
||||||
|
|
||||||
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import org.junit.Test;
|
|||||||
public class FactoryTest {
|
public class FactoryTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testYamlFileFactory(){
|
public void testYamlFileFactory() {
|
||||||
YamlFileInstanceFactory factory = new YamlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR);
|
YamlFileInstanceFactory factory = new YamlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR);
|
||||||
factory.refresh();
|
factory.refresh();
|
||||||
factory.getInstances().forEach(System.out::println);
|
factory.getInstances().forEach(System.out::println);
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.serliunx.ddns.test.config;
|
||||||
|
|
||||||
|
import com.serliunx.ddns.config.Configuration;
|
||||||
|
import com.serliunx.ddns.config.PropertiesConfiguration;
|
||||||
|
import com.serliunx.ddns.constant.InstanceSource;
|
||||||
|
import com.serliunx.ddns.constant.SystemConstants;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class PropertiesConfigTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
Configuration configuration = new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH);
|
||||||
|
configuration.refresh();
|
||||||
|
|
||||||
|
InstanceSource test = configuration.getEnum(InstanceSource.class, "test", InstanceSource.FILE_JSON);
|
||||||
|
System.out.println(test);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import org.junit.Test;
|
|||||||
public class ClientTest {
|
public class ClientTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test(){
|
public void test() {
|
||||||
IPAddressClient client = IPAddressClient.instance;
|
IPAddressClient client = IPAddressClient.instance;
|
||||||
System.out.println(client.getIPAddress().getQuery());
|
System.out.println(client.getIPAddress().getQuery());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user