Well.. it seems like enum classes in Java are a lot more versatile than their C or C++ counterpars, but for this specific code construct I'm trying to device their object nature is just giving me troubles.
What I have is something like this:
public static enum ServerResponse{
SIGN_UP_SUCESS(0),
INTERNAL_SERVER_ERROR(1),
EMAIL_ALREADY_REGISTERED(2),
SIGNIN_FAILED(3),
WAITING_CONFIRMATION(4),
SIGNIN_SUCESS(5),
BLOCKED_ACCOUNT(6),
INACTIVE_ACCOUNT(7);
private final int numcode;
ServerResponse(int num){
numcode = num;
}
final int numCode(){ return numcode;}
}
You see the problem now arises as the server gives me a numeric response, which I cannot compare directly with the enum class. My idea was then to create the numCode() method that would return the integer property of the instantiated enum. I tried to do something like this:
int SERVER_RESPONSE = ServerInterface.sendRequest();
switch(SERVER_RESPONSE){
ServerInterface.ServerResponse.BLOCKED_ACCOUNT.numCode():
//Do something
ServerInterface.ServerResponse.INTERNAL_SERVER_ERROR:
}
But as you can imagine none of the above worked. The first case comparison complains that "case expressions must be constant expressions" and the second type just gives me a type mismatch error.
So how should I do this? Well, right now I'm doing something like this:
for(ServerResponse response : ServerResponse.values()){
if(response.numCode() == SERVER_RESPONSE){
return response;
}
}
But it's ugly.. I would like to use a switch statement, that's the whole purpose of enum types after all right?? So please, what am I missing here?
Thanks
Nelson