JAL-3141 Made some suggested changes from code review
[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 String extension;
17
18   public BackupFilenameFilter(String base, String template, int digits,
19           String extension)
20   {
21     this.base = base;
22     this.template = template;
23     this.digits = digits;
24     this.extension = extension;
25   }
26
27   @Override
28   public boolean accept(File dir, String filename)
29   {
30     boolean ret = false;
31     try
32     {
33       File file = new File(
34               dir.getCanonicalPath() + File.separatorChar + filename);
35       if (file.isDirectory())
36       {
37         // backup files aren't dirs!
38         return false;
39       }
40     } catch (IOException e)
41     {
42       System.out.println("IOException when checking file '" + filename
43               + "' is a backupfile");
44     }
45
46     BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
47             template, digits, extension);
48     ret = bffp.isBackupFile();
49     return ret;
50   }
51
52   public static String getBackupFilename(int index, String base,
53           String template, int digits, String extension)
54   {
55     String numString = String.format("%0" + digits + "d", index);
56     String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
57             numString);
58     String backupfilename = base + backupSuffix + extension;
59     return backupfilename;
60   }
61
62 }
63