2013-03-23 46 views
1

我使用ifelse if來查找某種類型的代碼並從中創建相應的值。我不知道如何使它更有效,我發現在一個論壇下面的文章,但我沒有像類型boolean,我的類型爲bollean.edmchar.edm在不同的枚舉類型中對Java 1.6中的字符串使用switch開關

是否有使用下面的代碼的方式調整以支持我的情況?

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

你能否試着更準確地解釋你的目標是什麼?有什麼知道你的類型'bollean.edm'等? – user905686 2013-03-23 10:12:57

+0

我不認爲這是類型應提供爲字符串的問題,順便說一句,這是Odata類型 – 2013-03-23 10:16:45

回答

1

this,看起來像一個文件到你的misterious Odata type。或多或少工作sollution應該是這樣的(剛剛從標準java.lang.classes那些Odata type改變String typeName值什麼;)):

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

希望這有助於你

+0

dantuch - 謝謝! – 2013-03-23 15:12:44