帮我审查一下下带private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) { Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>(); List<String> colName = new ArrayList<>(); colName.add(Constants.ProjectView.COL_NAME_EXPRESSION); colName.add(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE); colName.add(Constants.ProjectView.COL_NAME_TOTAL); colName.add(Constants.ProjectView.COL_NAME_PERCENT); colName.add(Constants.ProjectView.COL_NAME_UNIT); for(String name : colName) { String value = null; if (Constants.ProjectView.COL_NAME_EXPRESSION.equals(name)) { value = bidNodeViewDto.getExpressionName() + ""; }else if(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE.equals(name)) { value = bidNodeViewDto.getExpressionVal() + ""; }else if(Constants.ProjectView.COL_NAME_PERCENT.equals(name)) { value = bidNodeViewDto.getPercentTotal() + ""; }else if(Constants.ProjectView.COL_NAME_TOTAL.equals(name)) { value = bidNodeViewDto.getAmount() + ""; }else if(Constants.ProjectView.COL_NAME_UNIT.equals(name)) { value = bidNodeViewDto.getUnitIndex() + ""; } String targetCode = targetCodeMap.get(name); if(Strings.isBlank(targetCode)) { continue; } ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult(); itemsCustomTargetResultMap.put(targetCode,itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null)); } return itemsCustomTargetResultMap; }
时间: 2024-03-19 08:43:21 浏览: 58
这段代码的主要作用是根据传入的参数`bidNodeViewDto`和`targetCodeMap`生成一个`Map`,其中`Map`的键值为`targetCode`,值为`ItemsCustomTargetResult`对象。
这个方法看起来没有太大问题,但是有一些可以改进的地方:
1. 在循环中,每次迭代都会新建一个`String`类型的变量`value`并初始化为`null`,这个可以放到循环外部先声明再初始化,避免每次都进行声明和初始化。
2. `if...else`嵌套语句比较多,可以考虑使用`switch`语句来代替。
3. 在循环中,如果`targetCode`为空字符串,则直接跳过当前迭代,没有任何处理。这个可以添加一个日志输出或者抛出异常来提醒调用者传入的参数有误。
下面是一种改进后的代码:
```
private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) {
Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>();
List<String> colName = Arrays.asList(Constants.ProjectView.COL_NAME_EXPRESSION, Constants.ProjectView.COL_NAME_EXPERSSION_VALUE, Constants.ProjectView.COL_NAME_TOTAL, Constants.ProjectView.COL_NAME_PERCENT, Constants.ProjectView.COL_NAME_UNIT);
for (String name : colName) {
String value = null;
switch (name) {
case Constants.ProjectView.COL_NAME_EXPRESSION:
value = String.valueOf(bidNodeViewDto.getExpressionName());
break;
case Constants.ProjectView.COL_NAME_EXPERSSION_VALUE:
value = String.valueOf(bidNodeViewDto.getExpressionVal());
break;
case Constants.ProjectView.COL_NAME_TOTAL:
value = String.valueOf(bidNodeViewDto.getAmount());
break;
case Constants.ProjectView.COL_NAME_PERCENT:
value = String.valueOf(bidNodeViewDto.getPercentTotal());
break;
case Constants.ProjectView.COL_NAME_UNIT:
value = String.valueOf(bidNodeViewDto.getUnitIndex());
break;
default:
throw new IllegalArgumentException("Invalid column name: " + name);
}
String targetCode = targetCodeMap.get(name);
if (targetCode == null || targetCode.isEmpty()) {
// 日志输出或者抛出异常
continue;
}
ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult();
itemsCustomTargetResultMap.put(targetCode, itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null));
}
return itemsCustomTargetResultMap;
}
```
阅读全文