user2218825
user2218825

Reputation:

Java - How can I use Type Safe Enumerations as switch-case statement?

I am developing a Java program, where I have to use the following TypeSafeEnum (that is given in an external component):

public class MyInterfaceTypeEnum
    extends TypeSafeEnum
{
    public MyInterfaceTypeEnum(String paramString, int paramInt)
    {
        super(paramString, paramInt);
    }

    public static final MyInterfaceTypeEnum UNKNOWN = new MyInterfaceTypeEnum("UNKNOWN", 0);
    public static final MyInterfaceTypeEnum TCP = new MyInterfaceTypeEnum("TCP", 10);
    public static final MyInterfaceTypeEnum UDP = new MyInterfaceTypeEnum("UDP", 20);
    public static final MyInterfaceTypeEnum IPX = new MyInterfaceTypeEnum("IPX", 30);
    public static final MyInterfaceTypeEnum RS232 = new MyInterfaceTypeEnum("RS232", 40);
}

My task-process class has the following method:

private void processInterface(MyInterfaceTypeEnum type)
{
    switch(type)
    {
    case RS232:
        {
            // do action
            break;
        }
    case TCP:
    case UDP:
        {
            // do action
            break;
        }
    case IPX:
        {
            // do action
            break;
        }
    case UNKNOWN:
    default:
        {
            // do default action
            break;
        }
    }
}

The compiler fails on the switch statement's line with the following message:

Cannot switch on a value of type MyInterfaceTypeEnum. Only convertible int values, strings or enum variables are permitted.

I googled, but everywhere was written: this TypeSafeEnum pattern may okay. Please give me ideas how to work around this.

UPDATED:

Important to mention: the implementation of MyInterfaceTypeEnum can't be changed by me (it is part of a used (imported) component). Therefore I am forced to use it. So, basic situation is: the "enum" (misused class) is already given.

If I would give up using MyInterfaceTypeEnum, I also would not be able to use a lot of functions (those using this type-safe-enum intensively). So I have no other choice but to accept this and try to find the simpliest possibility.

I know, if-else statement-tree can be used. But I want to avoid that. Since the MyInterfaceTypeEnum was added in an external component (implemented by others and I can't change it).

Upvotes: 0

Views: 469

Answers (3)

GhostCat
GhostCat

Reputation: 140523

There is no work around. Plain and simple.

The built-in switch statement only works with int, String or (built-in) enum values. Exactly as that error message is telling you. Therefore you are left with if/else chains, or maybe a cascade of if statements, with a return statement in each if block.

Or, you create a true Java enum that wraps around that existing broken enum class of yours. In other words: you create an extra layer of protection, so that all the code you will create isn't impacted by that enum class which you are forced to use. There is even a name for that: anti corruption layer.

Upvotes: 0

juwil
juwil

Reputation: 466

Provided you have a field name or a field value in TypeSafeEnum with public access getter, which do store the values of the constructor TypeSafeEnum (String paramString, int paramInt), you could:

private void processInterface(MyInterfaceTypeEnum type){
switch(type.getName()) {
case "RS232":
    {
        // do action
        break;
    }
case "TCP":

or do similar with the getter for value (which would be less readable.). @GhostCat's approach of wrapping your class into a real enum looks better to me.

Upvotes: 0

Mark Bramnik
Mark Bramnik

Reputation: 42511

Since java 1.5 Enums are supported in Java

public enum Protocols {
   TCP,
   UDP,
   PCX
}

....
void processInterface(Protocols proto) {

  switch(proto) {
     case TCP: ...
     case UDP: ...
  }
} 

Upvotes: 1

Related Questions