Switch statements can only be used on ints or enums. For strings we are heavily relying on if-else-if blocks.
That said, if you really want a switch, you can provide a string switching capability with an enum.
import java.util.HashMap;
import java.util.Map;
public enum UserActionMapping {
CREATE_USER("create"),UPDATE_USER("update");
private final String token;
private static Map<String,UserActionMapping> tokenMap;
private UserActionMapping(String token){
this.token = token;
map(token,this);
}
private static void map(String token, UserActionMapping op){
if (tokenMap==null) tokenMap = new HashMap<String,UserActionMapping>();
tokenMap.put(token,op);
}
public static UserActionMapping forToken(String token){
return tokenMap.get(token);
}
public static long getActionMapping(String[] actionMappingOps){
long returnValue=0L;
for (String opString : actionMappingOps){
UserActionMapping operation = UserActionMapping.forToken(opString);
System.out.println(operation);
switch (operation) {
case CREATE_USER:{
System.out.println("create user");
returnValue= 1;
break;
}
case UPDATE_USER:{
System.out.println("update user");
returnValue=2;
break;
}
}
System.out.println ("returnValue="+returnValue);
}
return returnValue;
}
public static void main(String[] args) {
String[] actionMappingOps = new String[]{"create","update"};
getActionMapping(actionMappingOps);
}
}
My Quotes
When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY
Powered by Find-IP.net
Friday, April 9, 2010
Subscribe to:
Post Comments
(
Atom
)
No comments :