Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Enum's ordinal value in switch-case statement

For my project I am using enums, and I need to implement the switch-case statement, where ordinal numbers of values of the specific Enum are checked, like this way:

        switch ( variable )
        {
        case MyEnum.A.ordinal():
            return true;
        case MyEnum.B.ordinal():
            return true;
        default:
            return false;
        }

Note: return values are only an example.

Unfortunately, Eclipse (I'm using 1.6 JDK) gives my compilation error "case expressions must be constant expressions". What I should do? Is there any other method than static lookup table, described here: Convert from enum ordinal to enum type ?

like image 523
DoktorNo Avatar asked Jul 23 '11 08:07

DoktorNo


People also ask

Can we pass enum in switch-case?

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.

What type of variable can be used in switch statement?

The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. You can have any number of case statements within a switch.

Which of the following Cannot be checked in a switch-case statement?

Which of the following cannot be checked in a switch-case statement? Explanation: The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.

Can switch-case statement character be checked?

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.


2 Answers

This is how is done, provided you have a serialized ordinal somehow, somewhere. Usual way to persist an enum is by its name, not ordinal, though. Also you should not use ordinal in normal circumstances unless trying to implement something like EnumMap/Set. Of course, the enum can be just a port from C alike stuff and dealing with the inevitable int, needs a transform to the Enum object.

Just use Enum.values() to obtain an array ordered by ordinal(), since the array is cloned each time, keeping a ref towards is ok.

enum E{
 A, B, C...   
}

final static E[] vals = E.values();//copy the values(), calling values() clones the array
boolean f(int variable){
  switch(vals[variable]){
  case A:
...
  case B:
...
//break;
  default:
...
   }
}

Just noticed you need only true and false, that's a Set type of behavior. You can use java.util.EnumSet or a simple long, if feeling brave (and not having more than 64 enum constants). for example:

private static <E extends Enum> long ord(E e){
  return 1L<<e.ordinal();
}

static final long positiveSet = ord(E.A)+ord(E.B);
boolean f(int ordinal){
  return 0!=(positiveSet&(1L<<ordinal));
}
like image 112
4 revs Avatar answered Sep 27 '22 23:09

4 revs


First of all, you should not rely on the ordinal that much. If possible make your variable a String (and transform to enum using Enum.valueOf(string) or at best make it enum.

If you really can't, then use enum.values()[ordinal].Then use the enum in the switch.

like image 20
Bozho Avatar answered Sep 28 '22 01:09

Bozho