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 --- .../kbackupfabric/backup/name/BackupFileNameEncoder.java | 7 +++---- .../backup/name/IncrementalBackupFileNameEncoder.java | 15 ++++++++------- .../backup/name/PrimitiveBackupFileNameEncoder.java | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/name') 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; } -- cgit v1.2.3