用枚举类解决Switch-Case过长的sonar问题Reduce the number of switch cases from 38 to at most 30.

【问题】Switch-Case过长的sonar问题Reduce the number of switch cases from 38 to at most 30.

 1                         switch (m.get("操作").toString()) {
 2                             case "启动应用":
 3                                 builder.append("        BOperate.open();\r\n");
 4                                 break;
 5                             case "点击":
 6                                 builder.append("        BOperate.click(\"" + replaceQuotation(m.get("Xpath").toString().trim()) + "\",true);\r\n");
 7                                 break;
 8                             case "输入":
 9                                 builder.append("        BOperate.type(\"" + replaceQuotation(m.get("Xpath").toString().trim()) + "\"," + paramStr(m.get("参数1")) + ");\r\n");
10                                 break;
11                             case "获取文本":
12                                 builder.append("        " + valStr(paramStr(m.get("参数1"))) + " = BOperate.getText(\"" + replaceQuotation(m.get("Xpath").toString().trim()) + "\",true);\r\n");
13                                 break;

 

【解决】

引用处:

1                         //sonar问题:Reduce the number of switch cases from 38 to at most 30.
2                         // 解决方法:操作的关键字存到枚举类。遍历枚举类,对应的枚举类方法做操作处理
3                         String operationStr = m.get("操作").toString();
4                         String builderAppendStr = Operation.NULL.getInstance(operationStr).builderAppendStr();
5                         builder.append(builderAppendStr);

添加的枚举类(做后续扩展):其中的变量在原代码引用处保存到静态变量,在枚举类调用

 1 public enum Operation {
 2         NULL("") {
 3             @Override
 4             public String builderAppendStr() {
 5                 return null;
 6             }
 7         },
 8         OPEN("启动应用") {
 9             @Override
10             public String builderAppendStr() {
11                 return "        BOperate.open();\r\n";
12             }
13         },
14         CLICK("点击") {
15             @Override
16             public String builderAppendStr() {
17                 return "        BOperate.click(\"" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + "\",true);\r\n";
18             }
19         },
20         TYPE("输入") {
21             @Override
22             public String builderAppendStr() {
23                 return "        BOperate.type(\"" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + "\"," + paramStr(sTCConvertMap.get("参数1")) + ");\r\n";
24             }
25         },
26         GET_TEXT("获取文本") {
27             @Override
28             public String builderAppendStr() {
29                 return "        " + valStr(paramStr(sTCConvertMap.get("参数1"))) + " = BOperate.getText(\"" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + "\",true);\r\n";
30             }
31         }
32 
33         ;
34 
35 
36         Operation(String operationName) {
37             this.operationName = operationName;
38         }
39 
40         private String operationName;
41 
42         public String getOperationName() {
43             return operationName;
44         }
45 
46         public Operation getInstance(String operationName) {
47             for (Operation operation : Operation.values()) {
48                 if (operationName.equals(operation.getOperationName())) {
49                     return operation;
50                 }
51             }
52             return Operation.NULL;
53         }
54 
55         public abstract String builderAppendStr();
56     }

 

上一篇:[Swift]LeetCode819. 最常见的单词 | Most Common Word


下一篇:[leetcode 11] Container With Most Water