List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode); }) .map(schemaRowRuleEo -> { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule); return schemaRowRule; }) .collect(Collectors.toList()); List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream() .filter(rule -> { if (StringUtils.isNotEmpty(rule.getColumnPermissionUserList())) { List<String> userList = JSONArray.parseArray(rule.getColumnPermissionUserList(), String.class); return userList.contains(userUid); } else if (StringUtils.isNotEmpty(rule.getColumnPermissionOrgList())) { List<String> orgList = JSONArray.parseArray(rule.getColumnPermissionOrgList(), String.class); return orgList.contains(orgCode); } return false; }) .map(rule -> { SchemaColumnRule columnRule = new SchemaColumnRule(); BeanUtils.copyProperties(rule, columnRule); return columnRule; }) .collect(Collectors.toList());代码优化
时间: 2023-12-14 13:39:46 浏览: 36
可以将一些重复的代码提前抽象出来,例如解析字符串为列表的部分可以提取为一个方法,同时可以使用方法引用的方式替换Lambda表达式,代码如下:
```
private <T> List<T> copyProperties(List<T> sourceList, Supplier<T> targetSupplier) {
return sourceList.stream()
.map(source -> {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
return target;
})
.collect(Collectors.toList());
}
...
List<String> userList = parseJsonArray(schemaRowRuleEo.getRowPermissionUserList());
List<String> orgList = parseJsonArray(schemaRowRuleEo.getRowPermissionOrgList());
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> userList.contains(userUid) || orgList.contains(orgCode))
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = parseJsonArray(rule.getColumnPermissionUserList());
List<String> orgList = parseJsonArray(rule.getColumnPermissionOrgList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.collect(Collectors.toList());
List<SchemaRowRule> copiedRowRuleList = copyProperties(userRowRuleList, SchemaRowRule::new);
List<SchemaColumnRule> copiedColumnRuleList = copyProperties(userColumnRuleList, SchemaColumnRule::new);
...
private List<String> parseJsonArray(String jsonArray) {
if (StringUtils.isNotEmpty(jsonArray)) {
return JSONArray.parseArray(jsonArray, String.class);
}
return Collections.emptyList();
}
```