feat: 添加枚举解析

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2017-02-04 20:38:44 +08:00
parent 52fa5f0f48
commit 192aaa3515
1 changed files with 10 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package pw.yumc.YumCore.config.inject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -32,6 +33,14 @@ public class InjectParse {
}
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;
}
@ -75,6 +84,7 @@ public class InjectParse {
return new SimpleDateFormat(config.getString(path));
}
}
public static class ListParse implements Parse<List> {
public ListParse() {
allparse.put(List.class, this);