JAL-3141 Made some suggested changes from code review
[jalview.git] / src / jalview / io / BackupFiles.java
index 784d8a1..28eefd6 100644 (file)
@@ -39,7 +39,7 @@ public class BackupFiles
 
   public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
 
-  protected static String NUM_PLACEHOLDER = "%n";
+  public static final String NUM_PLACEHOLDER = "%n";
 
   public static String REVERSE_ORDER = NS + "_REVERSE_ORDER";
 
@@ -58,8 +58,6 @@ public class BackupFiles
   // deleting old backup/version files
   private static boolean confirmDelete;
 
-  private static boolean classInit = false;
-
   // defaultSuffix - default template to use to append to basename of file
   private String suffix;
 
@@ -135,12 +133,8 @@ public class BackupFiles
 
   public static void classInit()
   {
-    if (!classInit)
-    {
-      setEnabled(Cache.getDefault(ENABLED, true));
-      setConfirmDelete(Cache.getDefault(CONFIRM_DELETE_OLD, true));
-      classInit = true;
-    }
+    setEnabled(Cache.getDefault(ENABLED, true));
+    setConfirmDelete(Cache.getDefault(CONFIRM_DELETE_OLD, true));
   }
 
   public static void setEnabled(boolean flag)
@@ -191,11 +185,6 @@ public class BackupFiles
     return path;
   }
 
-  public static String getNumPlaceHolder()
-  {
-    return NUM_PLACEHOLDER;
-  }
-
   public boolean setWriteSuccess(boolean flag)
   {
     boolean old = this.tempFileWriteSuccess;
@@ -476,5 +465,68 @@ public class BackupFiles
     return rename;
   }
 
+  public static TreeMap<Integer, File> lsBackupFilesAsTreeMap(
+          String fileName,
+          String suffix, int digits)
+  {
+    File[] backupFiles = null;
+
+    File file = new File(fileName);
+
+    String dir = "";
+    File dirFile;
+    try
+    {
+      dirFile = file.getParentFile();
+      dir = dirFile.getCanonicalPath();
+    } catch (Exception e)
+    {
+      System.out.println(
+              "Could not get canonical path for file '" + file + "'");
+      return new TreeMap<>();
+    }
+
+    String filename = file.getName();
+    String basename = filename;
+    String extension = "";
+    int dotcharpos = filename.lastIndexOf('.');
+    // don't split of filenames with the last '.' at the very beginning or
+    // very end of the filename
+    if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
+    {
+      basename = filename.substring(0, dotcharpos);
+      extension = filename.substring(dotcharpos); // NOTE this includes the '.'
+    }
+    
+    // find existing backup files
+    BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits, extension);
+    backupFiles = dirFile.listFiles(bff); // is clone needed?
+    
+    // sort the backup files (based on integer found in the suffix) using a
+    // precomputed Hashmap for speed
+    HashMap<Integer, File> bfHashMap = new HashMap<>();
+    for (int i = 0; i < backupFiles.length; i++)
+    {
+      File f = backupFiles[i];
+      BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix,
+              digits, extension);
+      bfHashMap.put(bfp.indexNum(), f);
+    }
+    TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
+    bfTreeMap.putAll(bfHashMap);
+
+    return bfTreeMap;
+  }
+
+  public static File[] lsBackupFiles(String fileName, String suffix,
+          int digits)
+  {
+    TreeMap<Integer, File> bfTreeMap = lsBackupFilesAsTreeMap(fileName,
+            suffix, digits);
+    File[] backupFiles = new File[bfTreeMap.size()];
+    bfTreeMap.values().toArray(backupFiles);
+    return backupFiles;
+  }
+
 }