From 5dc6e700cbcbc01833a02a193f660a286b934898 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 13 Jan 2023 02:49:57 +0800 Subject: Code cleanup. --- .../com/keuin/kbackupfabric/ui/BackupManager.java | 76 +++++++------- .../com/keuin/kbackupfabric/ui/KBCommands.java | 111 +-------------------- .../kbackupfabric/ui/PrimitiveBackupInfo.java | 5 - 3 files changed, 37 insertions(+), 155 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/ui') diff --git a/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java b/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java index d4081c6..7ee46f9 100644 --- a/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java +++ b/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java @@ -1,7 +1,5 @@ package com.keuin.kbackupfabric.ui; -import org.jetbrains.annotations.NotNull; - import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -28,51 +26,47 @@ public class BackupManager { * @return all backups. */ public Iterable getAllBackups() { - return new Iterable() { - @NotNull - @Override - public Iterator iterator() { - if (backupStorageDirectory.exists()) { - if (!backupStorageDirectory.isDirectory()) { - throw new RuntimeException("Backup directory is not a directory."); - } - } else { - if (!backupStorageDirectory.mkdirs()) { - throw new RuntimeException("Backup directory does not exist and cannot be created."); - } + return () -> { + if (backupStorageDirectory.exists()) { + if (!backupStorageDirectory.isDirectory()) { + throw new RuntimeException("Backup directory is not a directory."); } - File[] backupFiles = backupStorageDirectory.listFiles(); - if (backupFiles == null) { - throw new RuntimeException("Cannot list files in backup directory."); + } else { + if (!backupStorageDirectory.mkdirs()) { + throw new RuntimeException("Backup directory does not exist and cannot be created."); } + } + File[] backupFiles = backupStorageDirectory.listFiles(); + if (backupFiles == null) { + throw new RuntimeException("Cannot list files in backup directory."); + } - return new Iterator() { - private final Iterator fileIterator = Arrays.stream(backupFiles).filter(file -> { - String name = file.getName().toLowerCase(); - return name.endsWith(".zip") || name.endsWith(".kbi"); - }).iterator(); + return new Iterator() { + private final Iterator fileIterator = Arrays.stream(backupFiles).filter(file -> { + String name = file.getName().toLowerCase(); + return name.endsWith(".zip") || name.endsWith(".kbi"); + }).iterator(); - @Override - public boolean hasNext() { - return fileIterator.hasNext(); - } + @Override + public boolean hasNext() { + return fileIterator.hasNext(); + } - @Override - public BackupInfo next() { - try { - File backupFile = fileIterator.next(); - String fileName = backupFile.getName().toLowerCase(); - if (fileName.endsWith(".zip")) - return PrimitiveBackupInfo.fromFile(backupFile); - if (fileName.endsWith(".kbi")) - return IncrementalBackupInfo.fromFile(backupFile); - throw new RuntimeException("Invalid backup file extname"); - } catch (IOException e) { - throw new RuntimeException(e); - } + @Override + public BackupInfo next() { + try { + File backupFile = fileIterator.next(); + String fileName = backupFile.getName().toLowerCase(); + if (fileName.endsWith(".zip")) + return PrimitiveBackupInfo.fromFile(backupFile); + if (fileName.endsWith(".kbi")) + return IncrementalBackupInfo.fromFile(backupFile); + throw new RuntimeException("Invalid backup file extname"); + } catch (IOException e) { + throw new RuntimeException(e); } - }; - } + } + }; }; } } diff --git a/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java index e148f24..d23a6fd 100644 --- a/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java @@ -40,12 +40,8 @@ public final class KBCommands { private static MinecraftServer server; private static BackupManager backupManager; private static final Object managerCreatorLock = new Object(); - - //private static final Logger LOGGER = LogManager.getLogger(); - private static final List backupList = new ArrayList<>(); // index -> backupName private static Invokable pendingOperation = null; - //private static BackupMethod activatedBackupMethod = new PrimitiveBackupMethod(); // The backup method we currently using public static void setServer(MinecraftServer server) { KBCommands.server = server; @@ -124,21 +120,6 @@ public final class KBCommands { // TODO: Show concrete info from metadata for `.zip` backup // MinecraftServer server = context.getSource().getMinecraftServer(); // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory} -// File[] files = getBackupSaveDirectory(server).listFiles( -// (dir, name) -> dir.isDirectory() && -// (name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) -// || name.toLowerCase().endsWith(".kbi")) -// ); - -// Function backupInformationProvider = file -> { -// Objects.requireNonNull(file); -// if (file.getName().toLowerCase().endsWith(".zip")) -// return getPrimitiveBackupInformationString(file.getName(), file.length()); -// // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory} -// else if (file.getName().toLowerCase().endsWith(".kbi")) -// return getIncrementalBackupInformationString(file); -// return file.getName(); -// }; updateBackupList(); synchronized (backupList) { @@ -150,22 +131,6 @@ public final class KBCommands { BackupInfo info = backupList.get(i); printBackupInfo(context, info, i); } -// if (files != null) { -// if (files.length != 0) { -// msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)"); -// } else { -// msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); -// } -// int i = 0; -// for (File file : files) { -// ++i; -// String backupFileName = file.getName(); -// msgInfo(context, String.format("[%d] %s", i, backupInformationProvider.apply(file))); -// backupFileNameList.add(backupFileName); -// } -// } else { -// msgErr(context, "Error: failed to list files in backup folder."); -// } } return SUCCESS; } @@ -230,21 +195,6 @@ public final class KBCommands { } -// public static int incrementalBackup(CommandContext context) { -// //KBMain.backup("name") -// String backupName = StringArgumentType.getString(context, "backupName"); -// if (backupName.matches("[0-9]*")) { -// // Numeric param is not allowed -// backupName = String.format("a%s", backupName); -// msgWarn(context, String.format("Pure numeric name is not allowed. Renaming to %s", backupName)); -// } -// return doBackup(context, backupName, IncrementalBackupMethod.getInstance()); -// } -// -// public static int incrementalBackupWithDefaultName(CommandContext context) { -// return doBackup(context, DEFAULT_BACKUP_NAME, IncrementalBackupMethod.getInstance()); -// } - /** * Delete an existing backup with context parameter backupName. * Simply set the pending backupName to given backupName, for the second confirmation. @@ -390,7 +340,7 @@ public final class KBCommands { // By the way, update suggestion list. BackupNameSuggestionProvider.updateCandidateList(); - return returnValue ? SUCCESS : FAILED; // block compiler's complain. + return returnValue ? SUCCESS : FAILED; // block compiler's complaint. } /** @@ -401,7 +351,7 @@ public final class KBCommands { */ public static int cancel(CommandContext context) { if (pendingOperation != null) { - PrintUtil.msgInfo(context, String.format("The %s has been cancelled.", pendingOperation.toString()), true); + PrintUtil.msgInfo(context, String.format("The %s has been cancelled.", pendingOperation), true); pendingOperation = null; return SUCCESS; } else { @@ -422,22 +372,6 @@ public final class KBCommands { try { // List all backups updateBackupList(); -// MinecraftServer server = context.getSource().getMinecraftServer(); -// List files = Arrays.asList(Objects.requireNonNull(getBackupSaveDirectory(server).listFiles())); -// files.removeIf(f -> !f.getName().startsWith(BackupFilesystemUtil.getBackupFileNamePrefix())); -// files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName()))); -// File prevBackupFile = files.get(0); -// String backupFileName = prevBackupFile.getName(); -// int i; -// synchronized (backupList) { -// i = backupList.indexOf(backupFileName); -// if (i == -1) { -// backupList.add(backupFileName); -// i = backupList.size(); -// } else { -// ++i; -// } -// } synchronized (backupList) { if (!backupList.isEmpty()) { BackupInfo info = backupList.get(0); @@ -454,47 +388,6 @@ public final class KBCommands { return SUCCESS; } -// private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { -// return String.format( -// "(ZIP) %s , size: %s", -// PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName), -// getFriendlyFileSizeString(backupFileSizeBytes) -// ); -// } - -// 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) { -// e.printStackTrace(); -// return "(Incremental) " + backupFile.getName(); -// } -// } - -// /** -// * Select the backup method we use. -// * @param context the context. -// * @return stat code. -// */ -// public static int setMethod(CommandContext context) { -// String desiredMethodName = StringArgumentType.getString(context, "backupMethod"); -// List backupMethods = Arrays.asList(BackupType.PRIMITIVE_ZIP_BACKUP, BackupType.OBJECT_TREE_BACKUP); -// for (BackupType method : backupMethods) { -// if(method.getName().equals(desiredMethodName)) { -// // Incremental backup -//// activatedBackupMethod = -// msgInfo(context, String.format("Backup method is set to: %s", desiredMethodName)); -// return SUCCESS; -// } -// } -// -// return SUCCESS; -// } - private static String parseBackupFileName(CommandContext context, String userInput) { try { diff --git a/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java b/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java index 1895647..e3a0060 100644 --- a/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java +++ b/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java @@ -38,11 +38,6 @@ public class PrimitiveBackupInfo implements BackupInfo { public static PrimitiveBackupInfo fromFile(File zipFile) { // TODO: fix this, use metadata file instead -// fileName = zipFile.getName(); -// BackupFileNameEncoder.BackupBasicInformation info = PrimitiveBackupFileNameEncoder.INSTANCE.decode(fileName); -// if (info == null) -// throw new IllegalArgumentException("Invalid file name."); -// return new PrimitiveBackupInfo(info.customName, info.time, FilesystemUtil.getFileSizeBytes(zipFile)); return new PrimitiveBackupInfo(zipFile.getName(), FilesystemUtil.getFileSizeBytes(zipFile)); } -- cgit v1.2.3