4

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bilthon
  • 2,461
  • 8
  • 36
  • 44
  • have a look at http://stackoverflow.com/questions/1080904/how-can-i-lookup-a-java-enum-from-its-string-value/1080914#1080914 it is for an Enum with a string value, but easily converted to an int. – Gareth Davis Aug 19 '11 at 06:14

2 Answers2

8

You're missing a method in your enum which maps an integer to the enum value. Exactly how you do that is up to you - if your integers are contiguous starting with 0, you could potentially use an array... or you could use a Map<Integer, ServerResponse>. Either way, you can then do:

// Please don't use caps for non-constants
int serverResponseNumber = ServerInterface.sendRequest();
ServerResponse serverResponse = ServerResponse.fromInt(serverResponseNumber);

if (serverResponse == null) {
    // Do something with an unknown response - switch will barf otherwise.
    // Possibly throw an exception yourself?
}
switch(serverResponse) {
    case SIGN_UP_SUCCESS:
    ...
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ServerResponse serverResponse = ServerResponse.values()[serverResponseNumber] @Jon will this not help? – Phani Kumar Bhamidipati Jul 17 '13 at 12:34
  • 1
    @BobbyKumar: That would work *if* you want to always tie the ordinal to the numeric value. It's not clear that's a good idea here. It also requires populating an array each time you use it... if you *were* going to use the ordinal, I would cache the value of `values` rather than call it every time. – Jon Skeet Jul 17 '13 at 12:48
3

What advantage would it give first creating an ENUM representing a number and then creating a MAP (or arrays) of Integers/Enums and then using enums is switch case?

Can't the numbers directly be used in switch case. If number documentation/understanding is an issue define number constant.

In my view its a misuse of enum what we are discussing here.

MAR
  • 31
  • 1
  • 1
    I kinda felt the same way. But as I come from a C, C++ background and was used to enums corresponding to integer values I just started using them as I was used to. – Bilthon Dec 08 '11 at 03:44