Edit: After reading your comment I think you are looking for something like this.
import java.util.HashMap;
import java.util.Map.Entry;
public enum ErrorStatus {
PAGE_NOT_FOUND("404", "Description for error 404");
private static final HashMap<String, ErrorStatus> ERRORS_BY_CODE;
private static final HashMap<String, ErrorStatus> ERRORS_BY_DESCR;
static {
ERRORS_BY_CODE = new HashMap<String, ErrorStatus>();
ERRORS_BY_CODE.put("404", PAGE_NOT_FOUND);
ERRORS_BY_DESCR = new HashMap<String, ErrorStatus>();
ERRORS_BY_DESCR.put("Description for error 404", PAGE_NOT_FOUND);
}
So the most important thing here is the use of HashMaps, much like ZouZou suggested. If you want to efficiently look for a description by a givwn code you'll need a map for that, if you want to efficiently look for a code by a given description you'll need a map for that too.
If you have a string like "404" or "500" and want to get the corresponding description you can use
public static ErrorStatus getErrorByCode(String code) {
return ERRORS_BY_CODE.get(code);
}
If you have the description like "Description for error 404" and want to get the corresponding error code you can use
public static ErrorStatus getErrorByDescr(String descr) {
return ERRORS_BY_DESCR.get(descr);
}
If you only have a string containing the description it gets a bit nasty. This is not the most efficient way to do it but assuming you wont have that many error codes it's all right. So if we have a string like "Here is the description of the page not found error 'Description for error 404'" then you can use
public static ErrorStatus getErrorByString(String str) {
for (Entry<String, ErrorStatus> entry : ERRORS_BY_DESCR.entrySet()){
if (str.contains(entry.getKey())) {
return entry.getValue();
}
}
return null;
}
Be carefull about the last method as it returns null if nothing was found and also only gives only the first error object it succeeds (while there can be more than one error description in a code).