JAL-3141 Code checkout. Not compiling.
[jalview.git] / src / jalview / io / BackupFilenameFilter.java
1 package jalview.io;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5
6 public class BackupFilenameFilter implements FilenameFilter
7 {
8
9   public String base;
10
11   public String template;
12
13   public int digits;
14
15   public String extension;
16
17   public BackupFilenameFilter(String base, String template, int digits,
18           String extension)
19   {
20     this.base = base;
21     this.template = template;
22     this.digits = digits;
23     this.extension = extension;
24   }
25
26   @Override
27   public boolean accept(File file, String filename)
28   {
29     // CHECK THIS IS NOT ALWAYS THE PARENT DIR
30     if (file.isDirectory())
31     {
32       return true;
33     }
34     else
35     {
36       BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
37               template, digits, extension);
38       return bffp.isBackupFile();
39     }
40   }
41
42 }
43
44 class BackupFilenameParts
45 {
46   File file;
47
48   String base;
49
50   String templateStart;
51
52   int num;
53
54   int digits;
55
56   String templateEnd;
57
58   String extension;
59
60   boolean isBackupFile;
61
62   public BackupFilenameParts(File file, String base, String template, int digits,
63           String extension)
64   {
65     this(file.getName(), base, template, digits, extension);
66   }
67
68   public BackupFilenameParts(String filename, String base, String template,
69           int digits, String extension)
70   {
71     this.isBackupFile = false;
72
73     if (!(filename.startsWith(base) && filename.endsWith(extension)))
74     {
75       return;
76     }
77
78     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
79     String templateStart = template;
80     String templateEnd = "";
81     if (numcharstart > -1)
82     {
83       templateStart = template.substring(0, numcharstart);
84       templateEnd = template.substring(numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
85     }
86     int startLength = base.length() + templateStart.length();
87     int endLength = templateEnd.length() + extension.length();
88     String numString = filename.substring(startLength, filename.length() - endLength + 1);
89
90     if (filename.length() >= startLength + digits + endLength
91             && filename.startsWith(base + templateStart)
92             && filename.endsWith(templateEnd + extension)
93             && numString.matches("[0-9]+"))
94     {
95       this.file = file;
96       this.base = base;
97       this.templateStart = templateStart;
98       this.num = Integer.parseInt(numString);
99       this.digits = digits;
100       this.templateStart = templateStart;
101       this.templateEnd = templateEnd;
102       this.isBackupFile = true;
103     }
104     
105   }
106
107   public static String getBackupFilename(int index, String base,
108           String template,
109           int digits, String extension)
110   {
111     String numString = String.format("%0" + digits + "d", index);
112     String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
113             numString);
114     String backupfilename = base + backupSuffix + extension;
115     return backupfilename;
116   }
117
118   public boolean isBackupFile()
119   {
120     return this.isBackupFile;
121   }
122
123   public int indexNum()
124   {
125     return this.num;
126   }
127 }