Clover icon

Coverage Report

  1. Project Clover database Mon Sep 2 2024 17:57:51 BST
  2. Package jalview.io

File FileFormats.java

 

Coverage histogram

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

Code metrics

10
29
11
1
194
94
18
0.62
2.64
11
1.64

Classes

Class Line # Actions
FileFormats 41 29 18
0.9898%
 

Contributing tests

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