在Java 1.6中使用switch for String,具有不同的枚举类型

时间:2013-03-23 09:59:56

标签: java oop enums type-conversion

我正在使用ifelse if的代码来查找某些类型并从中创建相应的值。我想知道如何提高效率,我在论坛中发现了以下帖子,但我的类型不是boolean,我的类型是bollean.edmchar.edm等。< / p>

有没有办法使用以下代码进行调整以支持我的案例?

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

1 个答案:

答案 0 :(得分:1)

根据this,这看起来像是你的文件Odata type。或多或少的工作溶剂应如下所示(只需将{em>标准java.lang.classes 中的String typeName值更改为Odata type无论如何;)):( / p>

public class Test {
    public static void main(String[] args) throws InterruptedException {
            String typeName = "Edm.Double";
            String namePreparedForEncoding = typeName.replace('.', '_');
            Type type = Type.valueOf(namePreparedForEncoding);
            System.out.println(type);

            String memberValue = "42.99";
            SwitchInputType<?> value = type.makeType(memberValue);
            System.out.println(value);

            String typeName1 = "Edm.Int32";
            String namePreparedForEncoding1 = typeName1.replace('.', '_');
            Type type1 = Type.valueOf(namePreparedForEncoding1);
            System.out.println(type1);

            String memberValue1 = "42";
            SwitchInputType<?> value1 = type1.makeType(memberValue1);
            System.out.println(value1);
    }

    enum Type {
        Edm_Boolean {
            SwitchInputType makeType(String memberValue) {
                return new SwitchInputType<Boolean>(new Boolean(memberValue));
            }
        },
        Edm_Double {
            SwitchInputType makeType(String memberValue) {
                return new SwitchInputType<Double>(new Double(memberValue));
            }
        },
        Edm_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> {
        private Object o;

        public SwitchInputType(Object o) {
            this.o = o;
        }

        @Override
        public String toString() {
            return "SwitchInputType: " + o.toString();
        }
    }
}

输出:

Edm_Double
SwitchInputType: 42.99

Edm_Int32
SwitchInputType: 42

正如您可能注意到的那样,我已在枚举中将Edm.替换为Edm_ - 因为枚举不能是midlle中带点的名称。

PS:

如果您更改位toString()方法,您将确保转换确实有效:

    public String toString() {
        return String.format("SwitchInputType: (%s) %s", o.getClass().getSimpleName(), o);
    }

结果为:SwitchInputType: (Double) 42.99

希望这有助于你