JAL-3141 Reformatted code in eclipse
[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,
20           int digits)
21   {
22     this(file.getName(), base, template, digits);
23   }
24
25   public BackupFilenameParts(String filename, String base, String template,
26           int suggesteddigits)
27   {
28     this.isBackupFile = false;
29
30     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
31     int digits = 0;
32     String templateStart = template;
33     String templateEnd = "";
34     if (numcharstart > -1)
35     {
36       templateStart = template.substring(0, numcharstart);
37       templateEnd = template.substring(
38               numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
39       digits = suggesteddigits;
40     }
41
42     // calculate minimum length of a backup filename
43     int minlength = base.length() + template.length()
44             - BackupFiles.NUM_PLACEHOLDER.length() + digits;
45
46     if (!(filename.startsWith(base) && filename.length() >= minlength))
47     {
48       // non-starter
49       return;
50     }
51
52     int startLength = base.length() + templateStart.length();
53     int endLength = templateEnd.length();
54     String numString = numcharstart > -1
55             ? filename.substring(startLength, filename.length() - endLength)
56             : "";
57
58     if (filename.length() >= startLength + digits + endLength
59             && filename.startsWith(base + templateStart)
60             && filename.endsWith(templateEnd)
61             && numString.matches("[0-9]*"))
62     {
63       this.base = base;
64       this.templateStart = templateStart;
65       this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0;
66       this.digits = digits;
67       this.templateEnd = templateEnd;
68       this.isBackupFile = true;
69     }
70
71   }
72
73   public boolean isBackupFile()
74   {
75     return this.isBackupFile;
76   }
77
78   public int indexNum()
79   {
80     return this.num;
81   }
82 }