tags:

views:

131

answers:

4

Hi, I have an Enum defined which contains method return type like "String",Float,List,Double etc.

I will be using it in switch case statements. For example my enum is

public enum MethodType {
    DOUBLE,LIST,STRING,ARRAYLIST,FLOAT,LONG;
}

In a property file, I've key value pairs as follows. Test1=String Test2=Double

In my code I'm getting the value for the key. I need to use the VALUE in Switch Case to Determine the Type and based on that I've to implement some logic. For example something like this

switch(MethodType.DOUBLE){
     case DOUBLE:
        //Dobule logic
}

Can someone please help me to implement this?

+3  A: 

You've defined the enum, but you need to define a variable that is that type. Like this:

public enum MethodType { ... }

public MethodType var;

switch (var)
{
    case MethodType.DOUBLE:
        //...
        break;
    case MethodType.LIST:
        //...
        break;
//...
}
JYelton
But in my case I'll get the value based on the key from the property file. For example my value will be "String", I need to use this in the switch case. Is there any alternate approach to this. Could you please let me know?
Deepa
I'm not totally sure I understand what you mean. If you're looking for more key/value pairs, you might want to use a dictionary instead. If you want to see what type a value is, for example if var *is* a double, the code would be `if (var is double) { // do stuff }`
JYelton
+2  A: 

You don't need a switch at all (this is in java btw so might not work for you): Obviously you'll want to add some null checks, better exception handling, etc.

public enum MethodType {

        String,LONG,DOUBLE,THING;

        static MethodType fromString(String x) throws Exception {
            for (MethodType currentType: MethodType.values()){
                if (x.equals(currentType.toString())){
                    return currentType;
                }
            }
            throw new Exception("Unmatched Type");
        }
    }
StevenWilkins
And there's also the restriction that the string matches the representation of the methodType. If that's a problem, you can define an abstract stringRepresentation method and implement it for each MethodType
StevenWilkins
This is exactly what the `valueOf(String)` static method of all enums does, e.g. `MethodType.valueOf("DOUBLE") == MethodType.DOUBLE`.
ZoogieZork
@ZoogieZork - You're right, excellent point, totally slipped my mind.
StevenWilkins
+2  A: 

I guess this is what you are looking for:

public class C_EnumTest {
    public enum MethodType {
        DOUBLE,LIST,STRING,ARRAYLIST,FLOAT,LONG;
    }
    public static void main( String[] args ) {
        String value = "DOUBLE";
        switch( MethodType.valueOf( value ) ) {
        case DOUBLE:
            System.out.println( "It's a double" );
            break;
        case LIST:
            System.out.println( "It's a list" );
            break;
        }
    }
}

For not being case sensitive you could do a MethodType.valueOf( value.toUpperCase() ).

tangens
This is exactly what I was looking for. Is it possible to have an ignorecase check for the MethodType.valueOf?
Deepa
Thanks everyone for providing the Help.
Deepa
Just added a line for not being case sensitive.
tangens
A: 

This may be a little closer to what you need. You can make the propertyName property anything you need it to be in this case:

public enum MethodType {

  STRING("String"),
  LONG("Long"),
  DOUBLE("Double"),
  THING("Thing");

  private String propertyName;

  MethodType(String propName) {
    this.propertyName = propName;
  }

  public String getPropertyName() {
    return propertyName;
  }

  static MethodType fromPropertyName(String x) throws Exception {
    for (MethodType currentType : MethodType.values()) {
      if (x.equals(currentType.getPropertyName())) {
        return currentType;
      }
    }
    throw new Exception("Unmatched Type: " + x);
  }
}
digitalsanctum