JAL-3141 Code checkout. Not compiling.
[jalview.git] / src / jalview / io / BackupFiles.java
index 2d75746..39cd23e 100644 (file)
@@ -4,6 +4,19 @@ import jalview.bin.Cache;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.TreeMap;
+
+/*
+ * BackupFiles used for manipulating (naming rolling/deleting) backup/version files when an alignment or project file is saved.
+ * User configurable options are:
+ * BACKUPFILES_ENABLED - boolean flag as to whether to use this mechanism or act as before, including overwriting files as saved.
+ * BACKUPFILES_SUFFIX - a template to insert just before the file extension.  Use '%n' to be replaced by a 0-led SUFFIX_DIGITS long integer.
+ * BACKUPFILES_ROLL_MAX - the maximum number of backupfiles to keep for any one alignment or project file.
+ * BACKUPFILES_SUFFIX_DIGITS - the number of digits to insert replace %n with (e.g. BACKUPFILES_SUFFIX_DIGITS = 3 would make "001", "002", etc)
+ * BACKUPFILES_REVERSE_ORDER - if true then "logfile" style numbering and file rolling will occur. If false then ever-increasing version numbering will occur, but old files will still be deleted if there are more than ROLL_MAX backup files. 
+ */
 
 public class BackupFiles
 {
@@ -19,6 +32,8 @@ public class BackupFiles
 
   public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
 
+  protected static String NUM_PLACEHOLDER = "%n";
+
   public static String REVERSE_ORDER = NS + "_REVERSE_ORDER";
 
   private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS;
@@ -28,20 +43,20 @@ public class BackupFiles
 
   // enabled - default flag as to whether to do the backup file roll (if not
   // defined in preferences)
-  private boolean enabled = true;
+  private boolean enabled;
 
   // defaultSuffix - default template to use to append to basename of file
-  private String suffix = "-v%n";
+  private String suffix;
 
   // defaultMax - default max number of backup files
-  private int max = 4;
+  private int max;
 
   // defaultDigits - number of zero-led digits to use in the filename
-  private int digits = 2;
+  private int digits;
 
   // reverseOrder - set to true to make newest (latest) files lowest number
   // (like rolled log files)
-  private boolean reverseOrder = false;
+  private boolean reverseOrder;
 
   // temp saved file to become new saved file
   private File tempFile;
@@ -55,7 +70,7 @@ public class BackupFiles
   // REVERSE_ORDER
   public BackupFiles(File file)
   {
-    this(file, true, "-v%n", 4, 2, false);
+    this(file, true, "-v" + NUM_PLACEHOLDER, 4, 3, false);
   }
 
   // set, get and rename temp file into place
@@ -74,7 +89,7 @@ public class BackupFiles
     return tempFile.renameTo(file);
   }
 
-  protected BackupFiles(File file, boolean defaultEnabled,
+  public BackupFiles(File file, boolean defaultEnabled,
           String defaultSuffix,
           int defaultMax, int defaultDigits, boolean defaultReverseOrder)
   {
@@ -126,9 +141,10 @@ public class BackupFiles
     // split filename up to insert suffix template in the right place. template
     // and backupMax can be set in .jalview_properties
     String dir = "";
+    File dirFile;
     try
     {
-      File dirFile = file.getParentFile();
+      dirFile = file.getParentFile();
       dir = dirFile.getCanonicalPath();
     } catch (Exception e)
     {
@@ -156,13 +172,21 @@ public class BackupFiles
     if (reverseOrder)
     {
       // backup style numbering
-      for (int m = 0; m < max; m++)
+
+      int tempMax = max;
+      // max == -1 means no limits
+      if (max == -1)
+      {
+        // do something cleverer here (possibly)!
+        tempMax = 10000;
+      }
+
+      for (int m = 0; m < tempMax; m++)
       {
-        int n = max - m;
-        numString = String.format("%0" + digits + "d", n);
-        String backupSuffix = suffix.replaceAll("%n", numString);
-        String backupfilename = dir + File.separatorChar + basename
-                + backupSuffix + extension;
+        int n = tempMax - m;
+        String backupfilename = dir + File.separatorChar
+                + BackupFilenameParts.getBackupFilename(n, basename, suffix,
+                        digits, extension);
         File backupfile_n = new File(backupfilename);
 
         if (!backupfile_n.exists())
@@ -197,33 +221,73 @@ public class BackupFiles
         lastfile = backupfile_n;
       }
 
+      // now actually backup the important file!
+      ret = ret && file.renameTo(lastfile);
     }
     else
     {
-      // version style numbering (with file rolling though)
-
-      // check if all backup files exist
-      int largest = 0;
-      for (int m = 0; m < max; m++)
+      // version style numbering (with earliest file deletion if max files
+      // reached)
+
+      // find existing backup files
+      BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix,
+              digits,
+              extension);
+      File[] backupFiles = dirFile.listFiles(bff);
+      int nextIndexNum;
+      
+      if (backupFiles.length == 0)
       {
-        int n = m + 1;
-        numString = String.format("%0" + digits + "d", n);
-        String backupSuffix = suffix.replaceAll("%n", numString);
-        String backupfilename = dir + File.separatorChar + basename
-                + backupSuffix + extension;
-        File backupfile_n = new File(backupfilename);
-        if (backupfile_n.exists())
+        nextIndexNum = 1;
+      } else {
+
+        // and sort them (based on integer found in the suffix) using a
+        // precomputed Hashmap for speed
+        HashMap bfHashMap = new HashMap<Integer, File>();
+        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);
+
+        bfTreeMap.values().toArray(backupFiles);
+        
+        // max value of -1 means keep all backup files
+        if (bfTreeMap.size() >= max && max != -1)
         {
-          largest = n;
+          // need to delete some files to keep number of backups to designated
+          // max
+          int numToDelete = bfTreeMap.size() - max;
+          File[] filesToDelete = Arrays.copyOfRange(backupFiles, 0,
+                  numToDelete - 1);
+
+          /******************************************
+           * CONFIRM THESE DELETIONS WITH THE USER! *
+           ******************************************/
+          for (int i = 0; i < filesToDelete.length; i++)
+          {
+            File toDelete = filesToDelete[i];
+            toDelete.delete();
+          }
+
         }
+
+        nextIndexNum = bfTreeMap.lastKey() + 1;
+
+        // Let's make the new backup file!! yay, got there at last!
+        String nextBackupFilename = dir + File.separatorChar
+                + BackupFilenameParts.getBackupFilename(nextIndexNum,
+                        basename, suffix, digits, extension);
+        File nextBackupFile = new File(nextBackupFilename);
+        ret = ret && file.renameTo(nextBackupFile);
       }
-      // MORE CODE HERE BEN!
     }
 
-    // now actually backup the important file!
-    ret = ret && file.renameTo(lastfile);
-
     return ret;
   }
 
 }
+