Convert integer value to matching Java Enum

前端 未结 11 1291
慢半拍i
慢半拍i 2020-12-02 08:18

I\'ve an enum like this:

public enum PcapLinkType {
  DLT_NULL(0)
  DLT_EN10MB(1)
  DLT_EN3MB(2),
  DLT_AX25(3),
  /*snip, 200 more enums, not always consecu         


        
11条回答
  •  感动是毒
    2020-12-02 08:54

    You could add a static method in your enum that accepts an int as a parameter and returns a PcapLinkType.

    public static PcapLinkType of(int linkType) {
    
        switch (linkType) {
            case -1: return DLT_UNKNOWN
            case 0: return DLT_NULL;
    
            //ETC....
    
            default: return null;
    
        }
    }
    

提交回复
热议问题