import java.util.HashMap;
import java.util.Map;
public class StackOverFlowQuestion {
private static final int ERROR_CODE100 = -100;
private static final int ERROR_CODE101 = -101;
private static final int ERROR_CODE102 = -102;
private static final int ERROR_CODE103 = -103;
private static final int ERROR_CODE104 = -104;
public enum ErrorDetails {
ERROR_CODE_100(ERROR_CODE100, "Error code 100 Desc", false),
ERROR_CODE_101(ERROR_CODE101, "Error code 101 Desc", false),
ERROR_CODE_102(ERROR_CODE102, "Error code 102 Desc", true),
ERROR_CODE_103(ERROR_CODE103, "Error code 103 Desc", false),
ERROR_CODE_104(ERROR_CODE104, "Error code 104 Desc", true);
private int errorCode;
private String errorMsg;
private boolean canRetry;
private ErrorDetails(int errorCode, String errorMsg, boolean canRetry) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.canRetry = canRetry;
}
public String getErrorMsg() {
return this.errorMsg;
}
public boolean canRetry() {
return this.canRetry;
}
public String toString() {
return "Error code : " + errorCode + ", errorMsg : " + errorMsg
+ ", canRetry : " + canRetry;
}
}
private Map<Integer, ErrorDetails> errorMap;
public StackOverFlowQuestion() {
System.out.println("StackOverFlowQuestion.StackOverFlowQuestion()");
errorMap = new HashMap<Integer, StackOverFlowQuestion.ErrorDetails>();
errorMap.put(ERROR_CODE100, ErrorDetails.ERROR_CODE_100);
errorMap.put(ERROR_CODE101, ErrorDetails.ERROR_CODE_101);
errorMap.put(ERROR_CODE102, ErrorDetails.ERROR_CODE_102);
errorMap.put(ERROR_CODE103, ErrorDetails.ERROR_CODE_103);
errorMap.put(ERROR_CODE104, ErrorDetails.ERROR_CODE_104);
System.out.println("errorMap : " + errorMap);
}
/**
* @param args
*/
public static void main(String[] args) {
long param = -100;
StackOverFlowQuestion question = new StackOverFlowQuestion();
System.out.println("question.errorMap : " + question.errorMap);
System.out.println("question.errorMap.containskey(param) : "
+ question.errorMap.containsKey(param));
ErrorDetails errorDetails = question.errorMap.get(param);
System.out.println("errorDetails : " + errorDetails);
System.out.println("question.errorMap.containskey((int)param) : "
+ question.errorMap.containsKey((int) param));
ErrorDetails errorDetailsWithInt = question.errorMap.get((int) param);
System.out.println("errorDetailsWithInt : " + errorDetailsWithInt);
int paramInt = -100;
System.out.println("param == paramInt : " + (param == paramInt));
}
}
================================================================================= Output:
StackOverFlowQuestion.StackOverFlowQuestion()
errorMap : {-100=Error code : -100, errorMsg : Error code 100 Desc, canRetry : false, -102=Error code : -102, errorMsg : Error code 102 Desc, canRetry : true, -101=Error code : -101, errorMsg : Error code 101 Desc, canRetry : false, -104=Error code : -104, errorMsg : Error code 104 Desc, canRetry : true, -103=Error code : -103, errorMsg : Error code 103 Desc, canRetry : false}
question.errorMap : {-100=Error code : -100, errorMsg : Error code 100 Desc, canRetry : false, -102=Error code : -102, errorMsg : Error code 102 Desc, canRetry : true, -101=Error code : -101, errorMsg : Error code 101 Desc, canRetry : false, -104=Error code : -104, errorMsg : Error code 104 Desc, canRetry : true, -103=Error code : -103, errorMsg : Error code 103 Desc, canRetry : false}
question.errorMap.containskey(param) : false
errorDetails : null
question.errorMap.containskey((int)param) : true
errorDetailsWithInt : Error code : -100, errorMsg : Error code 100 Desc, canRetry : false
param == paramInt : true
=================================================================================
Here are few question which I need clarification
- The code is compiling even if I pass a long as the parameter to the get method of the HashMap which is declared to have only Integer as the keys. I was expecting a compilation error here, because I somehow feel this violates the strict typing.
- When I pass the long variable containing the error code as the parameter to the get method of the HashMap() the map returns null.
- When I downcast the same long parameter to an int and pass it to the get method of the hash map, the map returns the proper Enum.
I suspect the below line in the HashMap.get() method if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
I am not sure whether int == long will fail or their corresponding wrappers will fail., i even added a check in the main method to verify the equality of the int and long variables.
I would like to understand the behavior here.