[英]JAVA - Best suitable data structure
我对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数据结构,但我似乎无法使其正常工作。
非常感谢你。
您可以使用如下枚举:
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; }
}
您可以使用enum构建结构:
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语句中使用枚举值。
使用java.util.Map实现(HashMap)。 使用错误代码作为键,并使用描述作为值。
enum ErrorCode{
001,002
}
和
class ErrorWrapper{
private ErrorCode errorCode;
private String description;
//setters + getters
}
有一个Map<ErrorCode, List<ErrorWrapper>>
只需使用一个
HashMap<String, String>
因为您说过错误代码是字符串,并且描述也是字符串。
首先,“错误永不改变”在不久的将来将是错误的:)
我将使用属性文件来存储这些错误代码和描述(如果需要,可以解析“位置”数据)
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
我认为,这将更加动态
我为你写了一些东西; 包含测试代码。 只需在默认包中创建一个Main类,然后复制粘贴我的代码并运行它即可。 您可以使用错误类中的方法getError(String code)来通过代码获取错误消息:
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;
}
}
}
Java enum
非常适合您的问题。 我可以想到另一种方法,该方法在添加新的错误代码或修改现有代码方面更为灵活。
getError(ERROR_CODE)
方法将简单地在输入错误代码后附加-1
并查询此Map并返回适当的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.