Clover icon

Coverage Report

  1. Project Clover database Tue Oct 29 2024 21:36:55 GMT
  2. Package jalview.io

File BackupFilenameParts.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
29% of files have more coverage

Code metrics

18
48
8
1
173
118
24
0.5
6
8
3

Classes

Class Line # Actions
BackupFilenameParts 25 48 24
0.662162266.2%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import java.io.File;
24   
 
25    public class BackupFilenameParts
26    {
27    private String base;
28   
29    private String templateStart;
30   
31    private int num;
32   
33    private int digits;
34   
35    private String templateEnd;
36   
37    private boolean isBackupFile;
38   
 
39  0 toggle private BackupFilenameParts()
40    {
41  0 this.isBackupFile = false;
42    }
43   
 
44  150 toggle public BackupFilenameParts(File file, String base, String template,
45    int digits)
46    {
47  150 this(file.getName(), base, template, digits);
48    }
49   
 
50  4434 toggle public BackupFilenameParts(String filename, String base, String template,
51    int suggesteddigits)
52    {
53  4434 this(filename, base, template, suggesteddigits, false);
54    }
55   
 
56  4434 toggle public BackupFilenameParts(String filename, String base, String template,
57    int suggesteddigits, boolean extensionMatch)
58    {
59  4434 this.isBackupFile = false;
60   
61  4434 int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
62  4434 int digits = 0;
63  4434 String templateStart = template;
64  4434 String templateEnd = "";
65  4434 if (numcharstart > -1)
66    {
67  4170 templateStart = template.substring(0, numcharstart);
68  4170 templateEnd = template.substring(
69    numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
70  4170 digits = suggesteddigits;
71    }
72   
73  4434 String savedFilename = "";
74    // if extensionOnly is set then reset the filename to the last occurrence of
75    // the extension+templateStart and try the match
76  4434 if (extensionMatch)
77    {
78    // only trying to match from extension onwards
79   
80  0 int extensioncharstart = filename
81    .lastIndexOf('.' + base + templateStart);
82  0 if (extensioncharstart == -1)
83    {
84  0 return;
85    }
86   
87  0 savedFilename = filename.substring(0, extensioncharstart + 1); // include
88    // the "."
89  0 filename = filename.substring(extensioncharstart + 1);
90    }
91   
92    // full filename match
93   
94    // calculate minimum length of a backup filename
95  4434 int minlength = base.length() + template.length()
96    - BackupFiles.NUM_PLACEHOLDER.length() + digits;
97   
98  4434 if (!(filename.startsWith(base + templateStart)
99    && filename.endsWith(templateEnd)
100    && filename.length() >= minlength))
101    {
102    // non-starter
103  4134 return;
104    }
105   
106  300 int startLength = base.length() + templateStart.length();
107  300 int endLength = templateEnd.length();
108  300 String numString = numcharstart > -1
109    ? filename.substring(startLength, filename.length() - endLength)
110    : "";
111   
112  300 if (filename.length() >= startLength + digits + endLength
113    && filename.startsWith(base + templateStart)
114    && filename.endsWith(templateEnd)
115    // match exactly digits number of number-characters (numString
116    // should be all digits and at least the right length), or more than
117    // digits long with proviso it's not zero-leading.
118    && (numString.matches("[0-9]{" + digits + "}")
119    || numString.matches("[1-9][0-9]{" + digits + ",}")))
120    {
121  300 this.base = extensionMatch ? savedFilename + base : base;
122  300 this.templateStart = templateStart;
123  300 this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0;
124  300 this.digits = digits;
125  300 this.templateEnd = templateEnd;
126  300 this.isBackupFile = true;
127    }
128   
129    }
130   
 
131  0 toggle public static BackupFilenameParts currentBackupFilenameParts(
132    String filename, String base, boolean extensionMatch)
133    {
134  0 BackupFilenameParts bfp = new BackupFilenameParts();
135  0 BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
136    .getSavedBackupEntry();
137  0 String template = bfpe.suffix;
138  0 if (template == null)
139    {
140  0 return bfp;
141    }
142  0 int digits;
143  0 try
144    {
145  0 digits = bfpe.digits;
146    } catch (Exception e)
147    {
148  0 return bfp;
149    }
150  0 return new BackupFilenameParts(filename, base, template, digits,
151    extensionMatch);
152    }
153   
 
154  4284 toggle public boolean isBackupFile()
155    {
156  4284 return this.isBackupFile;
157    }
158   
 
159  150 toggle public int indexNum()
160    {
161  150 return this.num;
162    }
163   
 
164  78 toggle public static String getBackupFilename(int index, String base,
165    String template, int digits)
166    {
167  78 String numString = String.format("%0" + digits + "d", index);
168  78 String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER,
169    numString);
170  78 String backupfilename = base + backupSuffix;
171  78 return backupfilename;
172    }
173    }