diff options
author | Keuin <[email protected]> | 2021-01-13 13:22:19 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-13 13:22:19 +0800 |
commit | c82e1ee286e89f6e36e8920282d348ce8cfae9cf (patch) | |
tree | 63e9828aacc7cd691655dffc2c2f046d0c4dd807 /src/main/java/com/keuin/kbackupfabric/util/backup/name | |
parent | 81497ebee2cdb0dac3bcdc26c5d77d06bdb98b5d (diff) |
Refactor for a better code quality
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/name')
3 files changed, 42 insertions, 1 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java index 83967b7..972403b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java @@ -1,6 +1,7 @@ package com.keuin.kbackupfabric.util.backup.name; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; /** * Encode and decode backup file name for a specific backup type. @@ -38,12 +39,20 @@ public interface BackupFileNameEncoder { } class BackupBasicInformation { + public final String customName; public final LocalDateTime time; + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss"); + BackupBasicInformation(String customName, LocalDateTime time) { this.customName = customName; this.time = time; } + + @Override + public String toString() { + return String.format("%s, %s", customName, time.format(formatter)); + } } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java new file mode 100644 index 0000000..ae54930 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java @@ -0,0 +1,33 @@ +package com.keuin.kbackupfabric.util.backup.name; + +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-"; + 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); + return backupFileNamePrefix + "-" + timeString + "_" + customName + ".kbi"; + } + + @Override + public BackupFileNameEncoder.BackupBasicInformation decode(String fileName) { + Pattern pattern = Pattern.compile( + backupFileNamePrefix + "-" + "([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})_(.+)\\.kbi" + ); + Matcher matcher = pattern.matcher(fileName); + if (matcher.find()) { + String timeString = matcher.group(1); + String customName = matcher.group(2); + return new BackupFileNameEncoder.BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter)); + } + return null; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java index 39c2403..bcba114 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java @@ -6,7 +6,6 @@ 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"); |