Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case with an integer value that should be mapped to an enum type

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

like image 909
Bilthon Avatar asked Dec 21 '22 09:12

Bilthon


2 Answers

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:
    ...
}
like image 150
Jon Skeet Avatar answered Jan 14 '23 06:01

Jon Skeet


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.

like image 26
MAR Avatar answered Jan 14 '23 06:01

MAR