jdk8 优化以下代码: for (DqExecuteResult dqExecuteResult : dqExecuteResults) { String errorOutputPath = dqExecuteResult.getErrorOutputPath(); Path path = new Path(errorOutputPath); R<List<String>> getFileResult = resourceClient.getFilesAtPath(path.toUri().getPath()); if (null != getFileResult && getFileResult.isSuccess()) { for (String fullPath : getFileResult.getData()) { R<List<String>> previewResult = resourceClient.viewFileByPath(new Path(fullPath).toUri().getPath(), query.getCurrent(), query.getSize(), "\n"); if (null != previewResult && previewResult.isSuccess()) { if("原始库".equals(datasourceName)){ Long datasourceId = iDataSourceClient.queryRawDataSource().getId(); List<MetaColumn> tableColumns = iDataSourceMetaClient.getTableColumns(datasourceId, tableName); for (MetaColumn metaColumn : tableColumns){ headers.add(metaColumn.getColumnName()); } } else if("标准库".equals(datasourceName)){ Long datasourceId = iDataSourceClient.queryModelDataSource().getId(); List<MetaColumn> tableColumns = iDataSourceMetaClient.getTableColumns(datasourceId, tableName); for (MetaColumn metaColumn : tableColumns){ headers.add(metaColumn.getColumnName()); } } int headerSize = headers.size(); List<String> datas = previewResult.getData(); StringBuilder contextBuilder = new StringBuilder(); for(String data : datas){ contextBuilder.append(data.replaceAll("[\r\n\t]", " ")); contextBuilder.append("\n"); } StringReader reader = new StringReader(contextBuilder.toString()); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader); for (CSVRecord record : records) { if (record.size() == headerSize){ List<String> content = new ArrayList<>(); for (String column : record) { content.add(column); } contents.add(content); } } } } } }
时间: 2024-02-27 17:52:52 浏览: 142
可以通过以下方式对代码进行优化:
1. 首先,可以将重复的代码段提取为一个方法,以便在两个 if 块中重用。例如:
```
private List<MetaColumn> getTableColumns(String datasourceName, String tableName) {
Long datasourceId;
if ("原始库".equals(datasourceName)) {
datasourceId = iDataSourceClient.queryRawDataSource().getId();
} else if ("标准库".equals(datasourceName)) {
datasourceId = iDataSourceClient.queryModelDataSource().getId();
}
return iDataSourceMetaClient.getTableColumns(datasourceId, tableName);
}
```
2. 其次,可以将 CSV 解析和数据处理逻辑提取为一个方法:
```
private List<List<String>> processCSVData(List<String> datas, List<String> headers) throws IOException {
int headerSize = headers.size();
List<List<String>> contents = new ArrayList<>();
StringBuilder contextBuilder = new StringBuilder();
for (String data : datas) {
contextBuilder.append(data.replaceAll("[\r\n\t]", " "));
contextBuilder.append("\n");
}
StringReader reader = new StringReader(contextBuilder.toString());
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader);
for (CSVRecord record : records) {
if (record.size() == headerSize) {
List<String> content = new ArrayList<>();
for (String column : record) {
content.add(column);
}
contents.add(content);
}
}
return contents;
}
```
3. 然后,可以使用 Java 8 的 Lambda 表达式和方法引用来简化代码。例如:
```
dqExecuteResults.stream()
.map(DqExecuteResult::getErrorOutputPath)
.map(Path::new)
.map(Path::toUri)
.map(URI::getPath)
.map(resourceClient::getFilesAtPath)
.filter(Objects::nonNull)
.filter(R::isSuccess)
.flatMap(result -> result.getData().stream())
.map(Path::new)
.map(Path::toUri)
.map(URI::getPath)
.map(fullPath -> resourceClient.viewFileByPath(fullPath, query.getCurrent(), query.getSize(), "\n"))
.filter(Objects::nonNull)
.filter(R::isSuccess)
.flatMap(result -> {
List<String> datas = result.getData();
List<String> headers = new ArrayList<>();
if ("原始库".equals(datasourceName) || "标准库".equals(datasourceName)) {
getTableColumns(datasourceName, tableName).stream()
.map(MetaColumn::getColumnName)
.forEach(headers::add);
}
try {
return processCSVData(datas, headers).stream();
} catch (IOException e) {
e.printStackTrace();
return Stream.empty();
}
})
.forEach(contents::add);
```
这段代码使用了 stream 操作来简化循环和条件语句,使用方法引用、Lambda 表达式和函数式接口来写出更简洁的代码。
阅读全文