2013-03-16 64 views
1

我需要通過int來查找枚舉。枚舉是folows:通過常量特定數據在枚舉常量內查找

public enum ErrorCode{ 

    MissingReturn(1,"Some long String here"), 
    InvalidArgument(2,"Another long String here"); 


    private final int shortCode ; 

    private final String detailMessage; 

    ErrorCode(shortCode ,detailMessage){ 
     this.shortCode = shortCode ; 
     this.detailMessage= detailMessage; 
    } 

    public String getDetailedMessage(){ 
     return this.detailMessage; 
    }  

    public int getShortCode(){ 
     return this.shortCode ; 
    }  

    } 

現在是需要有一個將採取int代碼和應該返回我屬於存儲在Enum .Passing一個代碼的String消息的查找方法「1」應該返回字符串「這裏有一些長字符串」。什麼是實現此功能的最佳方式?

public static String lookUpMessageFromCode(int code){ 


} 

P.S:類EnumMap是否適用於這種用例?如果是,請讓我知道爲什麼?

+0

我已經看到了這問(並回答)多次在過去幾天。 – NPE 2013-03-16 08:37:42

+0

@NPE任何指針? – Geek 2013-03-16 08:38:47

+0

短碼值是否爲任意值(無特定碼型),還是隻有1,2,3等? – Bohemian 2013-03-16 10:32:22

回答

1

根據您與您的enum相關的int價值觀,我想補充的ErrorCode S,或靜態Map<Integer,ErrorCode>enum類的靜態數組,並用它做一個查找從代碼的方法在消息中。在你的情況下,一個數組更合適,因爲你的值爲12,它們很小。我還會更改簽名以返回ErrorCode

private static final ErrorCode[] allErrorCodes = new ErrorCode[] { 
    null, MissingReturn, InvalidArgument 
}; 

public static ErrorCode lookUpByCode(int code) { 
    // Add range checking to see if the code is valid 
    return allErrorCodes[code]; 
} 

誰需要的信息會得到像這樣的來電:

String message = ErrorCode.lookUpByCode(myErrorCode).getDetailedMessage(); 
+0

我們不能使用'EnumMap'嗎?請看最後一行。 – Geek 2013-03-16 08:41:58

+1

@Geek否,因爲'EnumMap'用於映射* from * enum',而你的任務調用從整數*映射到'enum'。 – dasblinkenlight 2013-03-16 08:43:54

+0

感謝您的澄清。 – Geek 2013-03-16 08:46:14

0

我只想通過你的枚舉值迭代,並檢查代碼。該解決方案可讓您利用現有的Enum而無需創建另一個對象進行管理。

public enum ErrorCode { 

    MissingReturn(1, "Some long String here"), 
    InvalidArgument(2, "Another long String here"); 

    private final int shortCode; 

    private final String detailMessage; 

    ErrorCode(int shortCode, String detailMessage) { 
     this.shortCode = shortCode; 
     this.detailMessage = detailMessage; 
    } 

    public String getDetailedMessage() { 
     return this.detailMessage; 
    } 

    public int getShortCode() { 
     return this.shortCode; 
    } 

    public static String lookUpMessageFromCode(int code) { 
     String message = null; 
     for (ErrorCode errorCode : ErrorCode.values()) { 
      if (errorCode.getShortCode() == code) { 
       message = errorCode.getDetailedMessage(); 
       break; 
      } 
     } 
     return message; 
    } 

    public static void main(String[] args) { 
     System.out.println(ErrorCode.lookUpMessageFromCode(1)); 
     System.out.println(ErrorCode.lookUpMessageFromCode(2)); 
    } 

} 

有一點要注意

Enum構造函數沒有關於它的參數類型的信息。

ErrorCode(int shortCode, String detailMessage) { 
    this.shortCode = shortCode; 
    this.detailMessage = detailMessage; 
} 
+0

如果枚舉中有很多常量,那麼你的程序不能很好地擴展。 dasblinkenlight的答案在這方面做得很好,因爲查找將在O(1)時間發生。 – Geek 2013-03-16 09:19:24

0

這裏是另一種選擇:

 public static String lookUpMessageFromCode(int code){ 
    for(ErrorCode ec:ErrorCode.values()){ 
     if(ec.shortCode==code) 
      return ec.detailMessage; 
    } 
    return null; 
}