1. import lombok.AllArgsConstructor;
    2. import lombok.Getter;
    3. import java.util.Arrays;
    4. import java.util.Map;
    5. import java.util.stream.Collectors;
    6. /**
    7. * @author shizi
    8. * @since 2020/9/14 9:58 上午
    9. */
    10. @AllArgsConstructor
    11. public enum ConfigValueTypeEnum {
    12. /**
    13. * yml配置
    14. */
    15. YML("yml配置"),
    16. /**
    17. * properties配置
    18. */
    19. PROPERTIES("properties配置"),
    20. /**
    21. * 打包中
    22. */
    23. JSON("json配置"),
    24. /**
    25. * string字符配置
    26. */
    27. STRING("string字符配置");
    28. @Getter
    29. private final String desc;
    30. private static final Map<Integer, ConfigValueTypeEnum> indexEnumMap;
    31. private static final Map<String, ConfigValueTypeEnum> nameEnumMap;
    32. static {
    33. indexEnumMap = Arrays.stream(ConfigValueTypeEnum.values()).collect(Collectors.toMap(ConfigValueTypeEnum::ordinal, e -> e));
    34. nameEnumMap = Arrays.stream(ConfigValueTypeEnum.values()).collect(Collectors.toMap(ConfigValueTypeEnum::name, e -> e));
    35. }
    36. public static ConfigValueTypeEnum parse(Integer index) {
    37. if (!indexEnumMap.containsKey(index)) {
    38. throw new RuntimeException("不支持下标: " + index);
    39. }
    40. return indexEnumMap.get(index);
    41. }
    42. public static ConfigValueTypeEnum parse(String name) {
    43. if (!nameEnumMap.containsKey(name)) {
    44. throw new RuntimeException("不支持name: " + name);
    45. }
    46. return nameEnumMap.get(name);
    47. }
    48. }
    1. @Test
    2. public void test3() {
    3. String local = "YML";
    4. show(ConfigValueTypeEnum.parse(local));
    5. show(ConfigValueTypeEnum.parse(1));
    6. }