Reputation: 253
I'm using java 1.6 and i know that from java 1.7 there is option to switch on string but here i use the if/elseif to route type name,my question if there is a elegant way that i can change it to switch Yet
public static SwitchType<?> switchT(final String typeName,
final String memberName) {
if (typeName.equals("java.lang.String")) {
return new SwitchInputType<String>(new String(memberName + " "));
} else if (typeName.equals("char")) {
return new SwitchInputType<Character>(new Character('a'));
} else if (typeName.equals("decimal") ||
typeName.equals("java.math.BigDecimal")) {
return new SwitchInputType<BigDecimal>(new BigDecimal("34.58"));
} else if (typeName.equals("boolean")) {
}
Upvotes: 1
Views: 7002
Reputation: 1073
Although it might be a little counter intuitive using enum
has proven quite powerful in that regard.
Every enum
has a valueOf(String)
method that returns an Element for that name. Then you may use the retrieved Element in a switch statement. The only ugly part is that valueOf(String)
throws an IllegalArgumentException
. So catching that is equivalent to a default case.
enum Type{
JAVA_LANG_STRING;
CHAR;
DECIMAL;
BOOLEAN;
JAVA_LANG_BIGDECIMAL;
}
public static SwitchType<?> switchT(final String typeName,
final String memberName) {
try{
Type t = Type.valueOf(typeName.toUppercase().replace(".","_"));
switch(t){
case Type.JAVA_LANG_STRING: return new SwitchInputType<String>(new String(memberName + " "));
case Type.CHAR: return new SwitchInputType<Character>(new Character('a'));
case Type.DECIMAL:
case Type.JAVA_MATH_BIGDECIMAL: return new SwitchInputType<BigDecimal>(new BigDecimal("34.58"));
}catch(IllegalArgumentException e){
//default case
}
}
Also enums may implement interfaces. Either by providing one implementation for each element or one global one.
interface SwitchInputTypeFacotry {
SwitchInputType get(String arg);
}
enum TypeName implements SwitchInputTypeFacotry{
CHAR{
SwitchInputType get(String arg){
return new SwitchInputType<Character>(new Character('a'));
}
}
[...]
}
public static SwitchType<?> switchT(final String typeName,
final String memberName) {
try{
SwitchInputTypeFacotry t = Type.valueOf(typeName.toUppercase().replace(".","_"));
return t.get(memberName);
}catch(IllegalArgumentException e){
//default case
}
}
The second way makes it very easy to extend functionality (as long as it stays in one module, subclassing is not possible with enums).
Upvotes: 1
Reputation: 691675
You could use a Map<String, SwitchTypeFactory>
:
public interface SwitchTypeFactory {
SwitchType<?> create(String memberName);
}
...
private static Map<String, SwitchTypeFactory> factories = new HashMap<String, SwitchTypeFactory>();
static {
factories.put("java.lang.String", new SwitchTypeFactory() {
@Override
public SwitchType<?> create(String memberName) {
return new SwitchInputType<String>(memberName + " ");
}
});
factories.put("char", new SwitchTypeFactory() {
@Override
public SwitchType<?> create(String memberName) {
return new SwitchInputType<Character>(Character.valueOf('a'))
}
});
...
}
public static SwitchType<?> switchT(final String typeName, final String memberName) {
return factories.get(typeName).create(memberName);
}
Upvotes: 1
Reputation: 382102
Many patterns are available, from the use of an enumeration to the use of a Map<String,Implementation>
but none of them will be more concise nor faster that what you have in this precise case. They would only make sense if more code was dependent of this typeName
.
Upvotes: 1