排序使用Comparator静态方法实现 (#812)

* 排序使用Comparator静态方法实现

* 修复排序逻辑错误

* fix compile error
pull/813/head
liaozan 2020-04-30 22:25:37 +08:00 committed by GitHub
parent 694667fcf5
commit 36b3a2d3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 24 deletions

View File

@ -38,6 +38,7 @@ import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -190,17 +191,10 @@ public class BackupServiceImpl implements BackupService {
// Build backup dto
try (Stream<Path> subPathStream = Files.list(backupParentPath)) {
return subPathStream
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX))
.map(backupPath -> buildBackupDto(BACKUP_RESOURCE_BASE_URI, backupPath))
.sorted((leftBackup, rightBackup) -> {
// Sort the result
if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) {
return 1;
} else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) {
return -1;
}
return 0;
}).collect(Collectors.toList());
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX))
.map(backupPath -> buildBackupDto(BACKUP_RESOURCE_BASE_URI, backupPath))
.sorted(Comparator.comparingLong(BackupDTO::getUpdateTime).reversed())
.collect(Collectors.toList());
} catch (IOException e) {
throw new ServiceException("Failed to fetch backups", e);
}
@ -291,8 +285,8 @@ public class BackupServiceImpl implements BackupService {
try {
String haloDataFileName = HaloConst.HALO_DATA_EXPORT_PREFIX +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-")) +
IdUtil.simpleUUID().hashCode() + ".json";
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-")) +
IdUtil.simpleUUID().hashCode() + ".json";
Path haloDataPath = Files.createFile(Paths.get(haloProperties.getDataExportDir(), haloDataFileName));
@ -315,17 +309,10 @@ public class BackupServiceImpl implements BackupService {
try (Stream<Path> subPathStream = Files.list(exportedDataParentPath)) {
return subPathStream
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_DATA_EXPORT_PREFIX))
.map(backupPath -> buildBackupDto(DATA_EXPORT_BASE_URI, backupPath))
.sorted((leftBackup, rightBackup) -> {
// Sort the result
if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) {
return 1;
} else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) {
return -1;
}
return 0;
}).collect(Collectors.toList());
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_DATA_EXPORT_PREFIX))
.map(backupPath -> buildBackupDto(DATA_EXPORT_BASE_URI, backupPath))
.sorted(Comparator.comparingLong(BackupDTO::getUpdateTime).reversed())
.collect(Collectors.toList());
} catch (IOException e) {
throw new ServiceException("Failed to fetch exported data", e);
}