From 5063ab12c48a6612d903c44c943230b6c323f602 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 24 Jan 2021 20:01:17 +0800 Subject: Code refactor: eliminate unnecessary instance creation of IncrementalBackupFileNameEncoder --- .../java/com/keuin/kbackupfabric/KBCommands.java | 39 ++++++++++++---------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/KBCommands.java') diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 73b61df..d9213c3 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -1,6 +1,8 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer; +import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; import com.keuin.kbackupfabric.backup.name.PrimitiveBackupFileNameEncoder; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; @@ -12,6 +14,7 @@ import com.keuin.kbackupfabric.operation.abstracts.i.Invokable; import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; import com.keuin.kbackupfabric.operation.backup.method.ConfiguredIncrementalBackupMethod; import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod; +import com.keuin.kbackupfabric.util.DateUtil; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -93,16 +96,13 @@ public final class KBCommands { || name.toLowerCase().endsWith(".kbi")) ); - Function backupInformationProvider = new Function() { - @Override - public String apply(File file) { - Objects.requireNonNull(file); - if (file.getName().toLowerCase().endsWith(".zip")) - return getPrimitiveBackupInformationString(file.getName(), file.length()); - else if (file.getName().toLowerCase().endsWith(".kbi")) - return getIncrementalBackupInformationString(file.getName()); - return file.getName(); - } + Function backupInformationProvider = file -> { + Objects.requireNonNull(file); + if (file.getName().toLowerCase().endsWith(".zip")) + return getPrimitiveBackupInformationString(file.getName(), file.length()); + else if (file.getName().toLowerCase().endsWith(".kbi")) + return getIncrementalBackupInformationString(file); + return file.getName(); }; synchronized (backupFileNameList) { @@ -290,7 +290,7 @@ public final class KBCommands { getLevelPath(server), getBackupSaveDirectory(server).getCanonicalPath() ) : new ConfiguredIncrementalBackupMethod( - new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()), + IncrementalBackupFileNameEncoder.INSTANCE.encode(customBackupName, LocalDateTime.now()), getLevelPath(server), getBackupSaveDirectory(server).getCanonicalPath(), getIncrementalBackupBaseDirectory(server).getCanonicalPath() @@ -393,17 +393,22 @@ public final class KBCommands { private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { return String.format( - "%s , size: %s", + "(ZIP) %s , size: %s", new PrimitiveBackupFileNameEncoder().decode(backupFileName), getFriendlyFileSizeString(backupFileSizeBytes) ); } - private static String getIncrementalBackupInformationString(String backupFileName) { - return String.format( - "(Incremental) %s", - new IncrementalBackupFileNameEncoder().decode(backupFileName) - ); + private static String getIncrementalBackupInformationString(File backupFile) { + try { + SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(backupFile); + return "(Incremental) " + info.getBackupName() + + ", " + DateUtil.getString(info.getBackupTime()) + + ((info.getTotalSizeBytes() > 0) ? + ("size: " + BackupFilesystemUtil.getFriendlyFileSizeString(info.getTotalSizeBytes())) : ""); + } catch (IOException e) { + return "(Incremental) " + backupFile.getName(); + } } // /** -- cgit v1.2.3