Best practice to look up Java Enum

前端 未结 10 837
甜味超标
甜味超标 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:36

    Guava also provides such function which will return an Optional if an enum cannot be found.

    Enums.getIfPresent(MyEnum.class, id).toJavaUtil()
                .orElseThrow(()-> new RuntimeException("Invalid enum blah blah blah.....")))
    

提交回复
热议问题