JAL-3141 Changed suffix position to after the extension (removed extension detection...
[jalview.git] / src / jalview / io / BackupFilenameParts.java
1 package jalview.io;
2
3 import java.io.File;
4
5 public class BackupFilenameParts
6 {
7   String base;
8
9   String templateStart;
10
11   int num;
12
13   int digits;
14
15   String templateEnd;
16
17   boolean isBackupFile;
18
19   public BackupFilenameParts(File file, String base, String template, int digits)
20   {
21     this(file.getName(), base, template, digits);
22   }
23
24   public BackupFilenameParts(String filename, String base, String template,
25           int digits)
26   {
27     this.isBackupFile = false;
28
29     // calculate minimum length of a backup filename
30     int minlength = base.length() + template.length()
31             - BackupFiles.NUM_PLACEHOLDER.length() + digits ;
32
33     if (!(filename.startsWith(base) && filename.length() >= minlength))
34     {
35       // non-starter
36       return;
37     }
38
39     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
40     String templateStart = template;
41     String templateEnd = "";
42     if (numcharstart > -1)
43     {
44       templateStart = template.substring(0, numcharstart);
45       templateEnd = template.substring(
46               numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
47     }
48     
49     int startLength = base.length() + templateStart.length();
50     int endLength = templateEnd.length();
51     String numString = filename.substring(startLength,
52             filename.length() - endLength);
53
54     if (filename.length() >= startLength + digits + endLength
55             && filename.startsWith(base + templateStart)
56             && filename.endsWith(templateEnd)
57             && numString.matches("[0-9]+"))
58     {
59       this.base = base;
60       this.templateStart = templateStart;
61       this.num = Integer.parseInt(numString);
62       this.digits = digits;
63       this.templateStart = templateStart;
64       this.templateEnd = templateEnd;
65       this.isBackupFile = true;
66     }
67     
68   }
69
70   public boolean isBackupFile()
71   {
72     return this.isBackupFile;
73   }
74
75   public int indexNum()
76   {
77     return this.num;
78   }
79 }