diff options
author | Keuin <[email protected]> | 2021-01-24 20:01:17 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-24 20:01:17 +0800 |
commit | 5063ab12c48a6612d903c44c943230b6c323f602 (patch) | |
tree | 38ad6cf5f48377142a1d2fabfb1122eb9a681421 | |
parent | dfd27f1a01a612c39bc17cac9793c80cc9b72e6b (diff) |
Code refactor: eliminate unnecessary instance creation of IncrementalBackupFileNameEncoder
12 files changed, 83 insertions, 57 deletions
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<File, String> backupInformationProvider = new Function<File, String>() { - @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<File, String> 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(); + } } // /** diff --git a/src/main/java/com/keuin/kbackupfabric/backup/BackupNameTimeFormatter.java b/src/main/java/com/keuin/kbackupfabric/backup/BackupNameTimeFormatter.java index bcf5d18..39abc75 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/BackupNameTimeFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/BackupNameTimeFormatter.java @@ -1,32 +1,23 @@ package com.keuin.kbackupfabric.backup; +import com.keuin.kbackupfabric.util.DateUtil; + import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; public class BackupNameTimeFormatter { - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); @Deprecated public static String getTimeString() { - return LocalDateTime.now().format(formatter); + return DateUtil.getString(LocalDateTime.now()); } public static String localDateTimeToString(LocalDateTime localDateTime) { - return localDateTime.format(formatter); + return DateUtil.getString(localDateTime); } @Deprecated public static long timeStringToEpochSeconds(String timeString) { - ZoneId systemZone = ZoneId.systemDefault(); // my timezone - LocalDateTime localDateTime = LocalDateTime.parse(timeString, formatter); - ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(localDateTime); - return localDateTime.toEpochSecond(currentOffsetForMyZone); - } - - public static LocalDateTime timeStringToLocalDateTime(String timeString) { - return LocalDateTime.parse(timeString,formatter); + return DateUtil.toEpochSecond(timeString); } } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java index 45590ba..d084aef 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java @@ -26,13 +26,13 @@ public class IncBackupInfoSerializer { return (SavedIncrementalBackup) o; } else if (o instanceof ObjectCollection) { // backward compatibility with old-style (v0) incremental backup - BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(file.getName()); + BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(file.getName()); if (info == null) throw new IOException("Invalid backup file name."); return new SavedIncBackupV0(ObjectCollectionConverter.convert((ObjectCollection) o), info); } else if (o instanceof ObjectCollection2) { // compatible with 1.4.6 implementation - BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(file.getName()); + BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(file.getName()); if (info == null) throw new IOException("Invalid backup file name."); return new SavedIncBackupV0((ObjectCollection2) o, info); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/SavedIncBackupV1.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/SavedIncBackupV1.java index 0ebe06a..bd2d4eb 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/SavedIncBackupV1.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/SavedIncBackupV1.java @@ -2,10 +2,10 @@ package com.keuin.kbackupfabric.backup.incremental.serializer; import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; +import com.keuin.kbackupfabric.util.DateUtil; import java.io.Serializable; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.util.Objects; public class SavedIncBackupV1 implements SavedIncrementalBackup, Serializable { @@ -17,7 +17,6 @@ public class SavedIncBackupV1 implements SavedIncrementalBackup, Serializable { private final long increasedSizeBytes; private final int filesAdded; private final int totalFiles; - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); public SavedIncBackupV1(ObjectCollection2 objectCollection2, String backupName, ZonedDateTime backupTime, long totalSizeBytes, long increasedSizeBytes, int filesAdded, int totalFiles) { this.totalFiles = totalFiles; @@ -90,7 +89,7 @@ public class SavedIncBackupV1 implements SavedIncrementalBackup, Serializable { return String.format( "%s, created at %s, files: %d (total size: %s), copied size: %s, files added: %d", backupName, - backupTime.format(formatter), + DateUtil.getString(backupTime), totalFiles, BackupFilesystemUtil.getFriendlyFileSizeString(totalSizeBytes), BackupFilesystemUtil.getFriendlyFileSizeString(increasedSizeBytes), diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java index 8ad91f7..b471834 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java @@ -1,7 +1,8 @@ package com.keuin.kbackupfabric.backup.name; +import com.keuin.kbackupfabric.util.DateUtil; + import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; /** * Encode and decode backup file name for a specific backup type. @@ -49,8 +50,6 @@ public interface BackupFileNameEncoder { public final String customName; public final LocalDateTime time; - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss"); - protected BackupBasicInformation(String customName, LocalDateTime time) { this.customName = customName; this.time = time; @@ -58,7 +57,7 @@ public interface BackupFileNameEncoder { @Override public String toString() { - return String.format("%s, %s", customName, time.format(formatter)); + return String.format("%s, %s", customName, DateUtil.getString(time)); } } } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java index 77e19b9..45cd00e 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java @@ -1,24 +1,24 @@ package com.keuin.kbackupfabric.backup.name; +import com.keuin.kbackupfabric.util.DateUtil; + import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.regex.Matcher; import java.util.regex.Pattern; public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { + private static final String backupFileNamePrefix = "incremental"; - // TODO: Merge all date formatter like this, into one single instance - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); + public static final IncrementalBackupFileNameEncoder INSTANCE = new IncrementalBackupFileNameEncoder(); - // TODO: make this private and use singleton pattern - public IncrementalBackupFileNameEncoder() { + private IncrementalBackupFileNameEncoder() { } @Override public String encode(String customName, LocalDateTime time) { if (!isValidCustomName(customName)) throw new IllegalArgumentException("Invalid custom name"); - String timeString = time.format(formatter); + String timeString = DateUtil.getString(time); return backupFileNamePrefix + "-" + timeString + "_" + customName + ".kbi"; } @@ -31,8 +31,9 @@ public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder { if (matcher.find()) { String timeString = matcher.group(1); String customName = matcher.group(2); - return new BackupFileNameEncoder.BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter)); + return new BackupFileNameEncoder.BackupBasicInformation(customName, DateUtil.toLocalDateTime(timeString)); } return null; } + } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java index c8e73f9..1deec8f 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java @@ -1,19 +1,19 @@ package com.keuin.kbackupfabric.backup.name; +import com.keuin.kbackupfabric.util.DateUtil; + import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { private static final String backupFileNamePrefix = "kbackup"; - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); @Override public String encode(String customName, LocalDateTime time) { if (!isValidCustomName(customName)) throw new IllegalArgumentException("Invalid custom name"); - String timeString = time.format(formatter); + String timeString = DateUtil.getString(time); return backupFileNamePrefix + "-" + timeString + "_" + customName + ".zip"; } @@ -26,7 +26,7 @@ public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder { if (matcher.find()) { String timeString = matcher.group(1); String customName = matcher.group(2); - return new BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter)); + return new BackupBasicInformation(customName, DateUtil.toLocalDateTime(timeString)); } return null; } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java index c4be40d..2a9cbc8 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -74,7 +74,7 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod // newer saved info (with metadata) File indexFile = new File(backupIndexFileSaveDirectory, backupIndexFileName); - BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(backupIndexFileName); + BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(backupIndexFileName); IncBackupInfoSerializer.toFile(indexFile, SavedIncrementalBackup.newLatest( collection, info.customName, diff --git a/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java new file mode 100644 index 0000000..b101cfd --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java @@ -0,0 +1,31 @@ +package com.keuin.kbackupfabric.util; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +public class DateUtil { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); + + public static String getString(LocalDateTime localDateTime) { + return localDateTime.format(formatter); + } + + public static String getString(ZonedDateTime zonedDateTime) { + return zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime().format(formatter); + } + + @Deprecated + public static long toEpochSecond(String dateTimeString) { + ZoneId systemZone = ZoneId.systemDefault(); // my timezone + LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter); + ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(localDateTime); + return localDateTime.toEpochSecond(currentOffsetForMyZone); + } + + public static LocalDateTime toLocalDateTime(String timeString) { + return LocalDateTime.parse(timeString, formatter); + } +} diff --git a/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java b/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java index d50b264..3de75b9 100644 --- a/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java +++ b/src/test/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBakupBackwardCompatibilityTest.java @@ -22,7 +22,7 @@ public class IncBakupBackwardCompatibilityTest { private final LocalDateTime backupTime = LocalDateTime.of(2000, 1, 1, 1, 1, 1, 1); private final Path testRoot = Paths.get(".\\testfile\\IncBackupBackwardCompatibilityTest"); - private final File indexFile = new File(testRoot.toString(), new IncrementalBackupFileNameEncoder().encode(customName, backupTime)); + private final File indexFile = new File(testRoot.toString(), IncrementalBackupFileNameEncoder.INSTANCE.encode(customName, backupTime)); @Test public void testBackwardCompatibility() throws IOException { diff --git a/src/test/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoderTest.java b/src/test/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoderTest.java index f9695d5..0576c3e 100644 --- a/src/test/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoderTest.java +++ b/src/test/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoderTest.java @@ -11,7 +11,7 @@ public class IncrementalBackupFileNameEncoderTest { public void testEncode() { LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); String customName = "name"; - IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + IncrementalBackupFileNameEncoder encoder = IncrementalBackupFileNameEncoder.INSTANCE; assertEquals("incremental-0001-01-01_01-01-01_name.kbi", encoder.encode(customName, time)); } @@ -19,7 +19,7 @@ public class IncrementalBackupFileNameEncoderTest { public void testDecode() { LocalDateTime time = LocalDateTime.of(1, 1, 1, 1, 1, 1); String customName = "name"; - IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + IncrementalBackupFileNameEncoder encoder = IncrementalBackupFileNameEncoder.INSTANCE; BackupFileNameEncoder.BackupBasicInformation information = encoder.decode("incremental-0001-01-01_01-01-01_name.kbi"); assertEquals(time, information.time); assertEquals(customName, information.customName); @@ -27,7 +27,7 @@ public class IncrementalBackupFileNameEncoderTest { @Test public void isValid() { - IncrementalBackupFileNameEncoder encoder = new IncrementalBackupFileNameEncoder(); + IncrementalBackupFileNameEncoder encoder = IncrementalBackupFileNameEncoder.INSTANCE; assertTrue(encoder.isValidFileName("incremental-0001-01-01_01-01-01_name.kbi")); assertTrue(encoder.isValidFileName("incremental-0001-01-01_01-01-01_0001-01-01_01-01-01_name.kbi")); assertFalse(encoder.isValidFileName("incremental-0001-01-01_01-01-01incremental-0001-01-01_01-01-01_name.kbi")); diff --git a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java index 5fe0411..c69bd11 100644 --- a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -23,7 +23,7 @@ public class ConfiguredIncrementalBackupMethodTest { private final String sourceDirectoryName = "source"; private final String destDirectoryName = "destination"; private final String customBackupName = "index"; - private final String indexFileName = new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); + private final String indexFileName = IncrementalBackupFileNameEncoder.INSTANCE.encode(customBackupName, LocalDateTime.now()); private final double directoryFactor = 0.05; private final double fileFactor = 0.1; |