Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.io

File FileFormats.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
29
11
1
196
95
18
0.62
2.64
11
1.64

Classes

Class Line # Actions
FileFormats 44 29 18
0.9898%
 

Contributing tests

This file is covered by 327 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.util.Locale;
24   
25    import java.util.ArrayList;
26    import java.util.HashSet;
27    import java.util.LinkedHashMap;
28    import java.util.List;
29    import java.util.Map;
30    import java.util.Set;
31   
32    import jalview.bin.ApplicationSingletonProvider;
33    import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
34   
35    /**
36    * A singleton registry of alignment file formats known to Jalview. On startup,
37    * the 'built-in' formats are added (from the FileFormat enum). Additional
38    * formats can be registered (or formats deregistered) programmatically, for
39    * example with a Groovy script.
40    *
41    * @author gmcarstairs
42    *
43    */
 
44    public class FileFormats implements ApplicationSingletonI
45    {
 
46  9198 toggle public static FileFormats getInstance()
47    {
48  9198 return ApplicationSingletonProvider.getInstance(FileFormats.class);
49    }
50   
51    /**
52    * Private constructor registers Jalview's built-in file formats
53    */
 
54  54 toggle private FileFormats()
55    {
56  54 reset();
57    }
58   
59    /*
60    * A lookup map of file formats by upper-cased name
61    */
62    private Map<String, FileFormatI> formats;
63   
64    /*
65    * Formats in this set are capable of being identified by IdentifyFile
66    */
67    private Set<FileFormatI> identifiable;
68   
69   
70    /**
71    * Reset to just the built-in file formats packaged with Jalview. These are
72    * added (and will be shown in menus) in the order of their declaration in the
73    * FileFormat enum.
74    */
 
75  68 toggle public synchronized void reset()
76    {
77  68 formats = new LinkedHashMap<String, FileFormatI>();
78  68 identifiable = new HashSet<FileFormatI>();
79  68 for (FileFormat format : FileFormat.values())
80    {
81  1768 registerFileFormat(format, format.isIdentifiable());
82    }
83    }
84   
85    /**
86    * Answers true if the format is one that can be identified by IdentifyFile.
87    * Answers false for a null value.
88    */
 
89  8 toggle public boolean isIdentifiable(FileFormatI f)
90    {
91  8 return identifiable.contains(f);
92    }
93   
94    /**
95    * Registers a file format for case-insensitive lookup by name
96    *
97    * @param format
98    */
 
99  4 toggle public void registerFileFormat(FileFormatI format)
100    {
101  4 boolean isIdentifiable = format instanceof FileFormat
102    && ((FileFormat) format).isIdentifiable();
103  4 registerFileFormat(format, isIdentifiable);
104    }
105   
 
106  1772 toggle protected void registerFileFormat(FileFormatI format,
107    boolean isIdentifiable)
108    {
109  1772 String name = format.getName().toUpperCase(Locale.ROOT);
110  1772 if (formats.containsKey(name))
111    {
112  1 jalview.bin.Console
113    .errPrintln("Overwriting file format: " + format.getName());
114    }
115  1772 formats.put(name, format);
116  1772 if (isIdentifiable)
117    {
118  1772 identifiable.add(format);
119    }
120    }
121   
122    /**
123    * Deregisters a file format so it is no longer shown in menus
124    *
125    * @param name
126    */
 
127  4 toggle public void deregisterFileFormat(String name)
128    {
129  4 FileFormatI ff = formats.remove(name.toUpperCase(Locale.ROOT));
130  4 identifiable.remove(ff);
131    }
132   
133    /**
134    * Answers a list of writeable file formats (as strings, corresponding to the
135    * getName() and forName() methods)
136    *
137    * @param textOnly
138    * if true, only text (not binary) formats are included
139    * @return
140    */
 
141  586 toggle public List<String> getWritableFormats(boolean textOnly)
142    {
143  586 List<String> l = new ArrayList<String>();
144  586 for (FileFormatI ff : formats.values())
145    {
146  15235 if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
147    {
148  7106 l.add(ff.getName());
149    }
150    }
151  586 return l;
152    }
153   
154    /**
155    * Answers a list of readable file formats (as strings, corresponding to the
156    * getName() and forName() methods)
157    *
158    * @return
159    */
 
160  317 toggle public List<String> getReadableFormats()
161    {
162  317 List<String> l = new ArrayList<String>();
163  317 for (FileFormatI ff : formats.values())
164    {
165  8241 if (ff.isReadable())
166    {
167  6973 l.add(ff.getName());
168    }
169    }
170  317 return l;
171    }
172   
173    /**
174    * Returns the file format with the given name, or null if format is null or
175    * invalid. This is not case-sensitive.
176    *
177    * @param format
178    * @return
179    */
 
180  506 toggle public FileFormatI forName(String format)
181    {
182  506 return format == null ? null
183    : formats.get(format.toUpperCase(Locale.ROOT));
184    }
185   
186    /**
187    * Returns an iterable collection of registered file formats (in the order in
188    * which they were registered)
189    *
190    * @return
191    */
 
192  8270 toggle public Iterable<FileFormatI> getFormats()
193    {
194  8270 return formats.values();
195    }
196    }