summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/backup/formatter
diff options
context:
space:
mode:
authorKeuin <[email protected]>2020-08-02 18:31:50 +0800
committerkeuin <[email protected]>2020-08-02 18:31:50 +0800
commitd9c2af035ac2edb68bc8b6a1f94b7c772c3509c8 (patch)
tree4a27dd9a313090ae75d35e4d27d5bdd40b1eb218 /src/main/java/com/keuin/kbackupfabric/util/backup/formatter
parentf09d420df609194a17e9d9ec63d710df8135aa8e (diff)
Refactor & object tree impl. stage 1
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/formatter')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java32
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java47
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java46
3 files changed, 125 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java
new file mode 100644
index 0000000..eae3639
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java
@@ -0,0 +1,32 @@
+package com.keuin.kbackupfabric.util.backup.formatter;
+
+import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter;
+import com.sun.istack.internal.NotNull;
+
+import java.time.LocalDateTime;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public interface BackupFileNameFormatter {
+
+ BackupFileName format(@NotNull String fileName);
+
+ class BackupFileName {
+ public final LocalDateTime time;
+ public final String name;
+
+ public BackupFileName(LocalDateTime time, String name) {
+ this.time = time;
+ this.name = name;
+ }
+ }
+
+ static BackupFileNameFormatter objectTreeBackup() {
+ return ObjectTreeBackupFileNameFormatter.getInstance();
+ }
+
+ static BackupFileNameFormatter primitiveZipBackup() {
+ return PrimitiveZipBackupFileNameFormatter.getInstance();
+ }
+
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java
new file mode 100644
index 0000000..e5503a9
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java
@@ -0,0 +1,47 @@
+package com.keuin.kbackupfabric.util.backup.formatter;
+
+import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil;
+import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter;
+import com.sun.istack.internal.NotNull;
+import com.sun.istack.internal.Nullable;
+import org.spongepowered.asm.mixin.Overwrite;
+
+import java.time.LocalDateTime;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class ObjectTreeBackupFileNameFormatter implements BackupFileNameFormatter {
+
+ private static final ObjectTreeBackupFileNameFormatter instance = new ObjectTreeBackupFileNameFormatter();
+
+ public static ObjectTreeBackupFileNameFormatter getInstance() {
+ return instance;
+ }
+
+ @Override
+ public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) {
+ LocalDateTime time = getTime(fileName);
+ String name = getBackupName(fileName);
+ return new BackupFileNameFormatter.BackupFileName(time,name);
+ }
+
+ @Nullable
+ private LocalDateTime getTime(String fileName) {
+ Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(0);
+ return BackupNameTimeFormatter.timeStringToLocalDateTime(timeString);
+ }
+ return null;
+ }
+
+ private String getBackupName(String backupFileName) {
+ try {
+ if (backupFileName.matches(BackupFilesystemUtil.getBackupFileNamePrefix() + ".+\\.json"))
+ return backupFileName.substring(BackupFilesystemUtil.getBackupFileNamePrefix().length(), backupFileName.length() - 4);
+ } catch (IndexOutOfBoundsException ignored) {
+ }
+ return backupFileName;
+ }
+
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java
new file mode 100644
index 0000000..40450eb
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java
@@ -0,0 +1,46 @@
+package com.keuin.kbackupfabric.util.backup.formatter;
+
+import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil;
+import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter;
+import com.sun.istack.internal.NotNull;
+import com.sun.istack.internal.Nullable;
+
+import java.time.LocalDateTime;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class PrimitiveZipBackupFileNameFormatter implements BackupFileNameFormatter {
+
+ private static final PrimitiveZipBackupFileNameFormatter instance = new PrimitiveZipBackupFileNameFormatter();
+
+ public static PrimitiveZipBackupFileNameFormatter getInstance() {
+ return instance;
+ }
+
+ @Override
+ public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) {
+ LocalDateTime time = getTime(fileName);
+ String name = getBackupName(fileName);
+ return new BackupFileNameFormatter.BackupFileName(time,name);
+ }
+
+ @Nullable
+ private LocalDateTime getTime(String fileName) {
+ Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(0);
+ return BackupNameTimeFormatter.timeStringToLocalDateTime(timeString);
+ }
+ return null;
+ }
+
+ private String getBackupName(String backupFileName) {
+ try {
+ if (backupFileName.matches(BackupFilesystemUtil.getBackupFileNamePrefix() + ".+\\.zip"))
+ return backupFileName.substring(BackupFilesystemUtil.getBackupFileNamePrefix().length(), backupFileName.length() - 4);
+ } catch (IndexOutOfBoundsException ignored) {
+ }
+ return backupFileName;
+ }
+
+}