优化这段代码if ((typeLists.size() == 1 && ((typeLists.contains(TransTradeType.DT.getValue()) && DT.size() == 0) || (typeLists.contains(TransTradeType.VIR.getValue()) && VIR.size() == 0))) || (typeLists.size() == 2 && ((typeLists.contains(TransTradeType.DT.getValue()) && DT.size() == 0) && (typeLists.contains(TransTradeType.VIR.getValue()) && VIR.size() == 0)))){ return JasperReportEngine.NO_DATA; }
时间: 2023-12-17 21:05:54 浏览: 146
可以使用更加简洁的方式来实现同样的逻辑,例如使用以下代码:
```
if (typeLists.containsAll(Arrays.asList(TransTradeType.DT.getValue(), TransTradeType.VIR.getValue()))) {
if (DT.isEmpty() && VIR.isEmpty()) {
return JasperReportEngine.NO_DATA;
}
} else if (typeLists.contains(TransTradeType.DT.getValue())) {
if (DT.isEmpty()) {
return JasperReportEngine.NO_DATA;
}
} else if (typeLists.contains(TransTradeType.VIR.getValue())) {
if (VIR.isEmpty()) {
return JasperReportEngine.NO_DATA;
}
}
```
这个代码块首先判断是否包含所有需要的交易类型(DT 和 VIR),如果是则判断 DT 和 VIR 是否均为空,如果是则返回 NO_DATA。如果不包含所有需要的交易类型,则分别判断 DT 和 VIR 是否为空。这样简洁易懂,同时保证了逻辑正确性。
相关问题
优化这段代码if (typeLists.remove(TransTradeType.TI.getValue())) { hqlBuilder.append(" (t1.type2 = 'TI' and t1.externalApplicationNo like 'BONUS%' and t1.externalCashRef not like 'DIV&BONUS%') "); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } } if (typeLists.remove(TransTradeType.DR.getValue())) { hqlBuilder.append(" (t1.type2 = 'DR' and t1.externalApplicationNo like 'SCRIP DIV%' and t1.externalCashRef not like 'DIV&BONUS%') "); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } } if (typeLists.remove(TransTradeType.DV.getValue())) { hqlBuilder.append(" (t1.type2 = 'DV' and t1.externalApplicationNo not like 'DIV&BONUS%')"); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } }
可以将这段代码进行重构,使用一个 Map 存储 TransTradeType 和对应的 HQL 查询语句,然后遍历 typeLists,判断是否包含该 TransTradeType,如果包含就将对应的 HQL 查询语句添加到 StringBuilder 中。如下所示:
```
Map<TransTradeType, String> hqlMap = new HashMap<>();
hqlMap.put(TransTradeType.TI, " (t1.type2 = 'TI' and t1.externalApplicationNo like 'BONUS%' and t1.externalCashRef not like 'DIV&BONUS%') ");
hqlMap.put(TransTradeType.DR, " (t1.type2 = 'DR' and t1.externalApplicationNo like 'SCRIP DIV%' and t1.externalCashRef not like 'DIV&BONUS%') ");
hqlMap.put(TransTradeType.DV, " (t1.type2 = 'DV' and t1.externalApplicationNo not like 'DIV&BONUS%')");
for (TransTradeType type : typeLists) {
if (hqlMap.containsKey(type)) {
hqlBuilder.append(hqlMap.get(type));
if (typeLists.size() > 1) {
hqlBuilder.append(" or ");
}
}
}
```
这样可以减少代码的重复性,提高代码的可读性和可维护性。
阅读全文