1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-24 02:08:48 +00:00

feat: 添加枚举解析

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-02-04 20:38:44 +08:00
parent 52fa5f0f48
commit 192aaa3515

View File

@ -3,6 +3,7 @@ package pw.yumc.YumCore.config.inject;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -32,6 +33,14 @@ public class InjectParse {
} }
public static Object parse(Class clazz, ConfigurationSection config, String path) { public static Object parse(Class clazz, ConfigurationSection config, String path) {
if (clazz.isEnum()) {
String value = config.getString(path);
try {
return Enum.valueOf(clazz, value);
} catch (IllegalArgumentException ex) {
throw new ConfigParseException(String.format("%s 不是 %s 有效值为 %s", value, clazz.getSimpleName(), Arrays.toString(clazz.getEnumConstants())));
}
}
return allparse.containsKey(clazz) ? allparse.get(clazz).parse(config, path) : null; return allparse.containsKey(clazz) ? allparse.get(clazz).parse(config, path) : null;
} }
@ -75,6 +84,7 @@ public class InjectParse {
return new SimpleDateFormat(config.getString(path)); return new SimpleDateFormat(config.getString(path));
} }
} }
public static class ListParse implements Parse<List> { public static class ListParse implements Parse<List> {
public ListParse() { public ListParse() {
allparse.put(List.class, this); allparse.put(List.class, this);