BackupFilter and other unfinished business
[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 BackupFileFilter 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 BackupFileFilter(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     if (file.isDirectory())
30     {
31       return true;
32     }
33     else
34     {
35       int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
36       String templateStart = template;
37       String templateEnd = "";
38       if (numcharstart > -1)
39       {
40         templateStart = template.substring(0, numcharstart);
41         templateEnd = template.substring(numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
42       }
43       int startLength = base.length() + templateStart.length();
44       int endLength = templateEnd.length() + extension.length();
45       if (filename.length() == startLength + digits + endLength
46               && filename.startsWith(base + templateStart)
47               && filename.endsWith(templateEnd + extension)
48               && filename
49                       .substring(startLength,
50                               filename.length() - endLength + 1)
51                       .matches("[0-9]+"))
52       {
53         return true;
54       }
55
56     }
57     return false;
58   }
59
60 }