Override valueof() and toString() in Java enum

前端 未结 7 1613
傲寒
傲寒 2020-12-02 12:36

The values in my enum are words that need to have spaces in them, but enums can\'t have spaces in their values so it\'s all bunched up. I want to override

相关标签:
7条回答
  • 2020-12-02 13:07

    The following is a nice generic alternative to valueOf()

    public static RandomEnum getEnum(String value) {
      for (RandomEnum re : RandomEnum.values()) {
        if (re.description.compareTo(value) == 0) {
          return re;
        }
      }
      throw new IllegalArgumentException("Invalid RandomEnum value: " + value);
    }
    
    0 讨论(0)
  • 2020-12-02 13:11

    Try this, but i don't sure that will work every where :)

    public enum MyEnum {
        A("Start There"),
        B("Start Here");
    
        MyEnum(String name) {
            try {
                Field fieldName = getClass().getSuperclass().getDeclaredField("name");
                fieldName.setAccessible(true);
                fieldName.set(this, name);
                fieldName.setAccessible(false);
            } catch (Exception e) {}
        }
    }
    
    0 讨论(0)
  • 2020-12-02 13:12

    You still have an option to implement in your enum this:

    public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name){...}
    
    0 讨论(0)
  • 2020-12-02 13:26

    You can use a static Map in your enum that maps Strings to enum constants. Use it in a 'getEnum' static method. This skips the need to iterate through the enums each time you want to get one from its String value.

    public enum RandomEnum {
    
        StartHere("Start Here"),
        StopHere("Stop Here");
    
        private final String strVal;
        private RandomEnum(String strVal) {
            this.strVal = strVal;
        }
    
        public static RandomEnum getEnum(String strVal) {
            if(!strValMap.containsKey(strVal)) {
                throw new IllegalArgumentException("Unknown String Value: " + strVal);
            }
            return strValMap.get(strVal);
        }
    
        private static final Map<String, RandomEnum> strValMap;
        static {
            final Map<String, RandomEnum> tmpMap = Maps.newHashMap();
            for(final RandomEnum en : RandomEnum.values()) {
                tmpMap.put(en.strVal, en);
            }
            strValMap = ImmutableMap.copyOf(tmpMap);
        }
    
        @Override
        public String toString() {
            return strVal;
        }
    }
    

    Just make sure the static initialization of the map occurs below the declaration of the enum constants.

    BTW - that 'ImmutableMap' type is from the Google guava API, and I definitely recommend it in cases like this.


    EDIT - Per the comments:

    1. This solution assumes that each assigned string value is unique and non-null. Given that the creator of the enum can control this, and that the string corresponds to the unique & non-null enum value, this seems like a safe restriction.
    2. I added the 'toSTring()' method as asked for in the question
    0 讨论(0)
  • 2020-12-02 13:28

    I don't think your going to get valueOf("Start Here") to work. But as far as spaces...try the following...

    static private enum RandomEnum {
        R("Start There"), 
        G("Start Here"); 
        String value;
        RandomEnum(String s) {
            value = s;
        }
    }
    
    System.out.println(RandomEnum.G.value);
    System.out.println(RandomEnum.valueOf("G").value);
    
    Start Here
    Start Here
    
    0 讨论(0)
  • 2020-12-02 13:33

    You can try out this code. Since you cannot override valueOf method you have to define a custom method (getEnum in the sample code below) which returns the value that you need and change your client to use this method instead.

    public enum RandomEnum {
    
        StartHere("Start Here"),
        StopHere("Stop Here");
    
        private String value;
    
        RandomEnum(String value) {
            this.value = value;
        }
    
        public String getValue() {
            return value;
        }
    
        @Override
        public String toString() {
            return this.getValue();
        }
    
        public static RandomEnum getEnum(String value) {
            for(RandomEnum v : values())
                if(v.getValue().equalsIgnoreCase(value)) return v;
            throw new IllegalArgumentException();
        }
    }
    
    0 讨论(0)
提交回复
热议问题