Best practice to look up Java Enum

前端 未结 10 840
甜味超标
甜味超标 2021-02-01 13:17

We have a REST API where clients can supply parameters representing values defined on the server in Java Enums.

So we can provide a descriptive error, we add this

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 13:45

    Why do we have to write that 5 line code ?

    public class EnumTest {
    public enum MyEnum {
        A, B, C, D;
    }
    
    @Test
    public void test() throws Exception {
        MyEnum.valueOf("A"); //gives you A
        //this throws ILlegalargument without having to do any lookup
        MyEnum.valueOf("RADD"); 
    }
    }
    

提交回复
热议问题