41c6859acd1d538bdf7c29f7c84b04b4710f414f
[jalview.git] / src / jalview / io / BackupFilenameFilter.java
1 package jalview.io;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6
7 public class BackupFilenameFilter implements FilenameFilter
8 {
9
10   public String base;
11
12   public String template;
13
14   public int digits;
15
16   public BackupFilenameFilter(String base, String template, int digits)
17   {
18     this.base = base;
19     this.template = template;
20     this.digits = digits;
21   }
22
23   @Override
24   public boolean accept(File dir, String filename)
25   {
26     try
27     {
28       File file = new File(
29               dir.getCanonicalPath() + File.separatorChar + filename);
30       if (file.isDirectory())
31       {
32         // backup files aren't dirs!
33         return false;
34       }
35     } catch (IOException e)
36     {
37       System.out.println("IOException when checking file '" + filename
38               + "' is a backupfile");
39     }
40
41     BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
42             template, digits);
43     return bffp.isBackupFile();
44   }
45
46   public static String getBackupFilename(int index, String base,
47           String template, int digits)
48   {
49     String numString = String.format("%0" + digits + "d", index);
50     String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
51             numString);
52     String backupfilename = base + backupSuffix;
53     return backupfilename;
54   }
55
56 }