开发者

Enums,use in switch case

开发者 https://www.devze.com 2022-12-30 13:14 出处:网络
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.

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'开发者_C百科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?


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() ).


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);
  }
}


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

public enum MethodType { ... }

public MethodType myMethod;

switch (myMethod)
{
    case MethodType.DOUBLE:
        //...
        break;
    case MethodType.LIST:
        //...
        break;
//...
}

Edit:

Previously, this snippet used var as the variable name, but that's a reserved keyword. Changed to myMethod.


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");
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消