2012-07-04 91 views
5

我是一種新的Java,但現在我面臨着一個困境。我有一個錯誤列表,看起來像:JAVA - 最合適的數據結構

"ERROR CODE" "POSITION" "Error description" 
"000" "1" "No error" 
"001" "1" "Connection error" 
"002" "1" "Error sending reversal or batch capture process" 
"003" "1" "Error after authorization – message sent to host and answer received" 
"004" "1" "Error sending message for authorization" 
"005" "1" "Error receiving message from host" 

和更多的錯誤。

現在我正在開發一個JAVA庫,我真正需要做的是以某種方式實現這個錯誤(錯誤永遠不會改變,它們總是相同的),以便使用該庫的開發人員可以很容易地識別錯誤描述給出ERROR_CODE。

例如:String getError(ERROR_CODE);並返回與ERROR_CODE關聯的錯誤描述字符串。

我想到宣佈ENUM數據結構,但我似乎無法使它正常工作。

非常感謝。

+2

向我們展示您的枚舉的嘗試 –

+4

正確的結構確實是一個枚舉。 –

+0

我不會說適當的結構是一個枚舉,id說它完全取決於情況。特別是考慮到他非常清楚地說錯誤代碼是一個字符串,這可能是因爲某些錯誤代碼包含非數字字符。 –

回答

3

您可以使用像這樣的枚舉:

enum Error { 

    ERROR_000("000", 1, "No error"), 
    ERROR_001("001", 1, "Connection error"), 
    ERROR_002("002", 1, "Error sending reversal or batch capture process"), 
    ERROR_003("003", 1, "Error after authorization – message sent" + 
         "to host and answer received"), 
    ERROR_004("004", 1, "Error sending message for authorization"), 
    ERROR_005("005", 1, "Error receiving message from host"); 

    private final String code; 
    private final int position; 
    private final String description; 
    private static final Map<String, Error> errorMap = 
     new HashMap<String, Error>(); 

    static { 
     for (Error error : Error.values()) { 
      errorMap.put(error.code, error); 
     } 
    } 

    Error(final String code, final int position, final String description) { 
     this.code = code; 
     this.position = position; 
     this.description = description; 
    } 

    public static Error getError(String code) { 
     return errorMap.get(code); 
    } 
    // add getters and setters here: 
    public String getCode() { return this.code; } 
    public int getPosition() { return this.position; } 
    public String getDescription() { return this.description; } 
} 
+0

我非常喜歡這個示例。我唯一想知道的是,如果我可以添加另一個條件getError方法。我需要指定位置,因爲我有相同的ERROR_CODE,但在不同的位置。例如:位置上的ERROR_CODE:001具有描述:「無效的消息頭」 –

+1

而不是在getError中返回字符串,您可以返回實際的Error對象。 這樣你可以做這樣的事情: 錯誤error = Error.getError(「001」)' 'System.out.println(「code:」+ error.getCode()+「position:」+ error。 getPosition()+「description:」+ error.getDescription();' 我已經更新了我的答案。 – munyengm

0
enum ErrorCode{ 
001,002 
} 

class ErrorWrapper{ 
private ErrorCode errorCode; 
private String description; 
//setters + getters  
} 

有一個Map<ErrorCode, List<ErrorWrapper>>

1

使用java.util.Map實現(HashMap的)。使用錯誤代碼作爲鍵和描述作爲值。

0

只需使用一個

HashMap<String, String> 

,因爲你說你的錯誤代碼是字符串,說明也是一個字符串。

2

可以使用枚舉建立一個結構:

public enum Error { 

    public final int code; 
    public final String message; 

    e0 (000, "No Error"), 
    e1 (001, "Connection error"); 

    public Error(int code, String message) { 
     this.code = code; 
     this.message = message; 
    } 

    public static Error byCode(int code) { 
     return Error.valueOf("e"+code); // you may add try/catch on IllegalArgumentException, etc. 
    } 
} 

您可以添加儘可能多的訪問者(靜態與否,他們可以例如使用一個靜態HashMap中的信息找到),因爲你需要。

自java 1.5以後,您可以在switch語句中使用枚舉值。

0

首先,「錯誤永遠不會改變」將是錯誤的在不久的將來:)

我會用一個屬性文件來存儲這些錯誤代碼和說明(如果必要的話「位置」的數據可以被解析)

Properties properties = new Properties(); 
    try { 
     properties.load(new FileInputStream("errors.properties")); 
    } catch (IOException e) {} 

然後,你getError方法是:

public String getError(String errorCode){ 
    return properties.getProperty(errorCode); 
} 

你errors.properties文件將是這樣的:

001=No error 
002=Connection error 

我認爲,這將是更加動感

+0

錯誤很少獨立於代碼添加。在我看來,在代碼之外聲明它們是沒有意義的。只有國際化的錯誤消息(如果有的話)應該在屬性中聲明。 –

+0

如您所說,錯誤代碼大多數依賴於代碼,但錯誤描述在大多數情況下不是 –

0

我寫的東西了你;包括測試代碼。只需在默認包中創建一個Main類,複製粘貼我的代碼並運行它。您可以使用該方法getError(字符串代碼)從類錯誤的代碼來獲取錯誤消息:

import java.util.*; 
public class Main { 
    public static void main(String[] args) { 
     for(String code : new String[]{"000", "001", "002", "003", "004", "005"}) { 
      System.out.println(Errors.getError(code)); 
     } 
    } 
} 
class Errors { 
    static { 
     Errors.errors = new HashMap<String, Error>(); 
     for(String[] error : new String[][]{ 
       {"000", "1", "No error"}, 
       {"001", "1", "Connection error"}, 
       {"002", "1", "Error sending reversal or batch capture process"}, 
       {"003", "1", "Error after authorization – message sent to host and answer received"}, 
       {"004", "1", "Error sending message for authorization"}, 
       {"005", "1", "Error receiving message from host"} 
     }) { 
      Errors.errors.put(error[0], new Error(error[0], error[1], error[2])); 
     } 

    } 
    private static Map<String, Error> errors; 
    public static String getError(String code) { 
     return Errors.errors.get(code).message; 
    } 
    private static class Error { 
     private String code; 
     private String position; 
     private String message; 
     public Error(String code, String position, String message) { 
      super(); 
      this.code = code; 
      this.position = position; 
      this.message = message; 
     } 
     @Override 
     public String toString() { 
      return this.getClass().getSimpleName() + " | code: " + this.code + " | position: " + this.position + " | message: " + this.message; 
     } 
    } 
} 
0

的Java enum都非常適合於您的問題。我可以考慮另一種方法,在添加新的錯誤代碼或修改現有代碼方面稍微靈活一些。

  1. 創建一個條目的屬性文件中像下面
    • 000-1 =無錯誤
    • 001-1 =連接錯誤
  2. 包這個文件放在罐子將分發給給開發者。
  3. 創建一個類並放置一個代碼,它將從jar中讀取該文件,並創建這些名稱值對的Map(或HashMap)。
  4. 將這段代碼放入一個靜態塊中,以便在加載類時初始化HashMap。
  5. getError(ERROR_CODE)方法將簡單地將-1附加到輸入錯誤代碼並查詢此Map並返回相應的錯誤消息。
  6. 要添加新消息或修改現有消息,只需更改屬性文件的內容即可。無需更改代碼。