zoukankan      html  css  js  c++  java
  • 自定义枚举类型的常用操作-附源码(xjl456852原创)

    自定义枚举类型中,假如我们有name和desc这样的属性,并在这个基础上定义了多个对象.
    那么就可能用到通过name获取desc,或者通过desc获取name.通过name或者desc获取对应的枚举对象的名称或ordinal等等.
    我们可以定义一个接口,所有的枚举类型都实现这个接口,这样使用会更方便.
    直接上代码,看看如何方便的操作这些需求.

    定义一个接口:
    1. package com.xjl456852.myenum;
    2. /**
    3. * Created by xiejianglei on 2017/1/9.
    4. */
    5. public interface TestEnum {
    6. String code();
    7. String desc();
    8. //这个方法是子类枚举类型自带的方法,子类无需实现,也无法实现
    9. String name();
    10. //这个方法是子类枚举类型自带的方法,子类无需实现,也无法实现
    11. int ordinal();
    12. }
    自定义枚举类型,实现定义的接口:
    1. package com.xjl456852.myenum;
    2. /**
    3. * Created by xiejianglei on 2017/1/9.
    4. */
    5. public enum TestSubEnum implements TestEnum {
    6. MALE("1","man"),FEMALE("2", "woman");
    7. private final String code;
    8. private final String desc;
    9. TestSubEnum(String code, String desc) {
    10. this.code = code;
    11. this.desc = desc;
    12. }
    13. @Override
    14. public String code() {
    15. return this.code;
    16. }
    17. @Override
    18. public String desc() {
    19. return this.desc;
    20. }
    21. }

    写个工具类:
    1. package com.xjl456852.myenum;
    2. /**
    3. * Created by xiejianglei on 2017/1/9.
    4. */
    5. public class Util {
    6. /**
    7. * 通过code获取desc
    8. * @param code
    9. * @param type
    10. * @param <T>
    11. * @return
    12. */
    13. public static <T>String getDescByCode(String code, Class<T>type) {
    14. if (type.isEnum()) {
    15. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    16. if (constant.code().equalsIgnoreCase(code)) {
    17. return constant.desc();
    18. }
    19. }
    20. }
    21. return null;
    22. }
    23. /**
    24. * 通过desc获取code
    25. * @param name
    26. * @param type
    27. * @param <T>
    28. * @return
    29. */
    30. public static <T>String getCodeByDesc(String name, Class<T>type) {
    31. if (type.isEnum()) {
    32. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    33. if (constant.desc().equalsIgnoreCase(name)) {
    34. return constant.code();
    35. }
    36. }
    37. }
    38. return null;
    39. }
    40. /**
    41. * 通过desc获取对应EnumName对象的名称
    42. * @param name
    43. * @param type
    44. * @param <T>
    45. * @return
    46. */
    47. public static <T>String getEnumNameByDesc(String name, Class<T>type) {
    48. if (type.isEnum()) {
    49. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    50. if (constant.desc().equalsIgnoreCase(name)) {
    51. return constant.name();
    52. }
    53. }
    54. }
    55. return null;
    56. }
    57. /**
    58. * 通过code获取对应EnumName对象的名称
    59. * @param code
    60. * @param type
    61. * @param <T>
    62. * @return
    63. */
    64. public static <T>String getEnumNameByCode(String code, Class<T>type) {
    65. if (type.isEnum()) {
    66. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    67. if (constant.code().equalsIgnoreCase(code)) {
    68. return constant.name();
    69. }
    70. }
    71. }
    72. return null;
    73. }
    74. /**
    75. * 通过code或desc获取对应EnumName对象的名称
    76. * @param any
    77. * @param type
    78. * @param <T>
    79. * @return
    80. */
    81. public static <T>TestEnum getEnumTypeByAny(String any, Class<T>type) {
    82. if (type.isEnum()) {
    83. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    84. if (constant.code().equalsIgnoreCase(any) || constant.desc().equalsIgnoreCase(any)) {
    85. return constant;
    86. }
    87. }
    88. }
    89. return null;
    90. }
    91. /**
    92. * 通过EnumName对象的名称,获取对应的枚举对象
    93. * @param enumName
    94. * @param type
    95. * @param <T>
    96. * @return
    97. */
    98. public static <T>TestEnum getEnumTypeByEnumName(String enumName, Class<T>type) {
    99. if (type.isEnum()) {
    100. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    101. //也可以使用constant.toString()替代constant.name()
    102. if (constant.name().equalsIgnoreCase(enumName)) {
    103. return constant;
    104. }
    105. }
    106. }
    107. return null;
    108. }
    109. /**
    110. * 通过code或desc获取对应ordinal值
    111. * @param any
    112. * @param type
    113. * @param <T>
    114. * @return
    115. */
    116. public static <T>int getOrdinalByAny(String any, Class<T>type) {
    117. if (type.isEnum()) {
    118. for (TestEnum constant : (TestEnum[])type.getEnumConstants()){
    119. if (constant.code().equalsIgnoreCase(any) || constant.desc().equalsIgnoreCase(any)) {
    120. return constant.ordinal();
    121. }
    122. }
    123. }
    124. return -1;
    125. }
    126. }
    测试方法:
    1. package com.xjl456852.myenum;
    2. import static com.xjl456852.myenum.Util.*;
    3. /**
    4. * Created by xiejianglei on 2017/1/9.
    5. */
    6. public class TestMain {
    7. public static void main(String args[]) {
    8. String code = "1";
    9. String desc = "man";
    10. String enumName = "MALE";
    11. //通过枚举类型自带方法获取枚举类型的名字
    12. System.out.println(TestSubEnum.MALE.name());
    13. //通过自定义枚举类型的code,获取枚举类型的desc
    14. System.out.println(getDescByCode(code, TestSubEnum.class));
    15. //通过自定义枚举类型的desc,获取枚举类型的code
    16. System.out.println(getCodeByDesc(desc, TestSubEnum.class));
    17. //通过自定义枚举类型的code,获取枚举类型的名字
    18. System.out.println(getEnumNameByCode(code, TestSubEnum.class));
    19. //通过自定义枚举类型的desc,获取枚举类型的名字
    20. System.out.println(getEnumNameByDesc(desc, TestSubEnum.class));
    21. //通过自定义枚举类型的code或desc,获取枚举类型对象
    22. System.out.println(getEnumTypeByAny(code, TestSubEnum.class));
    23. //通过自定义枚举类型的code或desc,获取枚举类型对象
    24. System.out.println(getEnumTypeByAny(desc, TestSubEnum.class));
    25. //通过自定义枚举类型的code或desc,获取枚举类型的ordinal
    26. System.out.println(getOrdinalByAny(code, TestSubEnum.class));
    27. //通过自定义枚举类型的对象名称,获取对应枚举类型的对象
    28. System.out.println(getEnumTypeByEnumName(enumName, TestSubEnum.class));
    29. }
    30. }
    测试结果如下:
    MALE
    man
    1
    MALE
    MALE
    MALE
    MALE
    0
    MALE





  • 相关阅读:
    程序经理_产品经理_项目经理
    github上排名靠前的java项目之_storm
    垂直型与水平型电子商务网站的理解
    关于驱动更新的一点学习
    Balanced Binary Tree
    Gray Code
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Maximum Depth of Binary Tree
    Next Permutation
  • 原文地址:https://www.cnblogs.com/xjl456852/p/6266257.html
Copyright © 2011-2022 走看看