Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch for type string for java 1.6 [duplicate]

I have the following code and I'm wondering if there is a way to use a switch block instead of a bunch of if/else statements. I know that Java supports strings in switch blocks as of Java 1.7 but I'm still working with Java 1.6:

} else if (typeName.equals("Boolean")) {

            return new SwitchInputType<Boolean>(new Boolean((String) memberValue));

        } else if (typeName.equals("Double")) {

            return new SwitchInputType<Double>(new Double((String) memberValue));

        } else if (typeName.equals("Int32"))
like image 324
John Jerrby Avatar asked Mar 20 '13 20:03

John Jerrby


People also ask

Can we use switch case for Strings in Java?

Yes, we can use a switch statement with Strings in Java.

Does switch work with Double Java?

switch expression can't be float, double or boolean.

Does switch support String data type?

A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer (discussed in Numbers and Strings).

What is switch () in Java?

The switch statement or switch case in java is a multi-way branch statement. Based on the value of the expression given, different parts of code can be executed quickly. The given expression can be of a primitive data type such as int, char, short, byte, and char.


2 Answers

You could even make the enum do it for you:

public static void main(String[] args) throws InterruptedException {
    String typeName = "Boolean";
    String memberValue = "memberValue";
    SwitchInputType type = Type.valueOf(typeName).makeType(memberValue);
}

enum Type {
    Boolean {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Boolean>(new Boolean(memberValue));
        }
    },
    Double {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Double>(new Double(memberValue));
        }
    }, 
    Int32 {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Integer>(new Integer(memberValue));
        }
    };

    // All must do this.
    abstract SwitchInputType makeType(String memberValue);
}

static class SwitchInputType<T> {
    public SwitchInputType(Object o) {
    }
}
like image 142
OldCurmudgeon Avatar answered Oct 20 '22 06:10

OldCurmudgeon


As your strings are all valid identifiers, you could create an enumeration with those strings as the item labels, use Enum.valueOf(Class, String) (or the similar valueOf(String) method that will be created in your enumeration class) to convert to a member of the enumeration type, and then switch based on that...

Example:

enum TypeName { Boolean, Double, Int32 }

switch (TypeName.valueOf(typeName)) {
   case Boolean: // ...
   case Double: // ...
   case Int32: // ...
}
like image 30
Jules Avatar answered Oct 20 '22 08:10

Jules