1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package fr.ens.transcriptome.doelan.data;
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Set;
29
30 import fr.ens.transcriptome.doelan.DoelanException;
31
32 /***
33 * This class defines a container for testsuites.
34 * @author Laurent Jourdren
35 */
36 public class QualityTestSuiteList {
37
38 private Map suites = new HashMap();
39 private Map reverseIndex = new HashMap();
40
41 /***
42 * Add a testSuite to the list
43 * @param chipType Type of the TestSuite
44 * @param testSuiteURL TestSuite to add
45 * @throws DoelanException if an error occur while adding the element
46 */
47 public void addTestSuite(final String chipType,
48 final QualityTestSuiteURL testSuiteURL) throws DoelanException {
49
50 if (testSuiteURL == null || testSuiteURL.getName().equals(""))
51 throw new DoelanException("Test suite name is empty");
52
53 Set s = (Set) this.suites.get(chipType);
54
55 if (s == null)
56 throw new DoelanException("The chip type doesn't exists");
57
58 Iterator it = s.iterator();
59 while (it.hasNext()) {
60 QualityTestSuiteURL qtsurl = (QualityTestSuiteURL) it.next();
61 if (qtsurl.getName().equals(testSuiteURL.getName()))
62 throw new DoelanException("This test suite name already exists");
63 }
64
65 s.add(testSuiteURL);
66
67 this.suites.put(chipType, s);
68 this.reverseIndex.put(testSuiteURL, chipType);
69
70 }
71
72 /***
73 * Add a chip type to the list
74 * @param chipType Type of the TestSuite
75 * @throws DoelanException if an error occurs while adding a new chip type
76 */
77 public void addChipType(final String chipType) throws DoelanException {
78
79 if (chipType == null)
80 throw new DoelanException("The chip type is null");
81
82 Set s = (Set) this.suites.get(chipType);
83
84 if (s != null)
85 throw new DoelanException("The chip type already exists");
86
87 this.suites.put(chipType, new HashSet());
88
89 }
90
91 /***
92 * Remove a test suite
93 * @param testSuiteURL Test suite to remove
94 * @throws DoelanException if an error occur while removing the element
95 */
96 public void removeTestSuite(final QualityTestSuiteURL testSuiteURL)
97 throws DoelanException {
98
99 if (testSuiteURL == null)
100 throw new DoelanException("The test suite doesn't exists");
101
102 String chipType = getChipType(testSuiteURL);
103 if (chipType == null)
104 throw new DoelanException("This test suite has no chip type");
105
106 Set s = (Set) this.suites.get(chipType);
107
108 s.remove(testSuiteURL);
109 this.reverseIndex.remove(testSuiteURL);
110
111 }
112
113 /***
114 * Remove a chip if it is empty.
115 * @param chipType The chip type to remove
116 * @throws DoelanException if an error occur while removing the element
117 */
118 public void removeChipType(final String chipType) throws DoelanException {
119
120 if (chipType == null)
121 throw new DoelanException("The chip type doesn't exists");
122
123 Set s = (Set) this.suites.get(chipType);
124 if (s == null)
125 throw new DoelanException("The chip type doesn't exists");
126
127 if (s.size() != 0)
128 throw new DoelanException("The chip type contains tests suites");
129
130 this.suites.remove(chipType);
131 }
132
133 /***
134 * Rename a chip type
135 * @param oldName Old name of the chip type
136 * @param newName New name of the chip type
137 * @throws DoelanException if an error occur while removing the element
138 */
139 public void renameChipType(final String oldName, final String newName)
140 throws DoelanException {
141
142 if (oldName == null || newName == null)
143 throw new DoelanException("The new or old name is null");
144
145 if (this.suites.containsKey(newName))
146 throw new DoelanException("The name already exists");
147
148 Set s = (Set) this.suites.get(oldName);
149 if (s == null)
150 throw new DoelanException("The old chip name doesn't exists");
151
152 this.suites.put(newName, s);
153 this.suites.remove(oldName);
154
155 Iterator it = s.iterator();
156 while (it.hasNext())
157 this.reverseIndex.put(it.next(), newName);
158 }
159
160 /***
161 * Rename a test suite
162 * @param chipType Chip type of the test suite to rename
163 * @param oldName Old name of the test suite
164 * @param newName New name of the test suite
165 * @throws DoelanException if an error occurs while renaming the test suite
166 */
167 public void renameTestSuite(final String chipType, final String oldName,
168 final String newName) throws DoelanException {
169
170 if (oldName == null || newName == null)
171 throw new DoelanException("The new or old name is null");
172
173 Set s = (Set) this.suites.get(chipType);
174 if (s == null)
175 throw new DoelanException("The chip type doesn't exists");
176
177 Iterator it = s.iterator();
178 while (it.hasNext()) {
179
180 QualityTestSuiteURL qtsurl = (QualityTestSuiteURL) it.next();
181 if (qtsurl.getName().equals(newName))
182 throw new DoelanException("The test suite already exists");
183 }
184
185 it = s.iterator();
186 while (it.hasNext()) {
187
188 QualityTestSuiteURL qtsurl = (QualityTestSuiteURL) it.next();
189 if (qtsurl.getName().equals(oldName)) {
190 qtsurl.setName(newName);
191 return;
192 }
193 }
194
195 throw new DoelanException("The test suite doesn't exists");
196 }
197
198 /***
199 * Clear the list.
200 */
201 public void clear() {
202 this.suites.clear();
203 this.reverseIndex.clear();
204 }
205
206 /***
207 * Get the type list of the TestSuites
208 * @return An array string with the names of the types
209 */
210 public String[] getChipTypes() {
211
212 Set s = this.suites.keySet();
213 final int size = s.size();
214 String[] result = new String[size];
215
216 int i = 0;
217 Iterator it = s.iterator();
218 while (it.hasNext())
219 result[i++] = (String) it.next();
220
221 return result;
222 }
223
224 /***
225 * Return all the TestSuiteURL for a chip type
226 * @param chipType Chip type of the testsuites
227 * @return An array of TestSuiteURL
228 */
229 public QualityTestSuiteURL[] getTestSuiteURLs(final String chipType) {
230
231 Set s = (Set) this.suites.get(chipType);
232 if (s == null)
233 return null;
234
235 QualityTestSuiteURL[] result = new QualityTestSuiteURL[s.size()];
236
237 int i = 0;
238 Iterator it = s.iterator();
239 while (it.hasNext())
240 result[i++] = (QualityTestSuiteURL) it.next();
241
242 return result;
243 }
244
245 /***
246 * Get the type of a test suite from this test suite url.
247 * @param url URL of the test suite.
248 * @return The type of the test suite
249 */
250 public String getChipType(final QualityTestSuiteURL url) {
251 return (String) this.reverseIndex.get(url);
252 }
253
254 /***
255 * Return the number of chip types.
256 * @return The number of chip types
257 */
258 public int size() {
259 return this.suites.size();
260 }
261
262 /***
263 * Test if a test suite is set.
264 * @return true if a test suite is set.
265 */
266 public boolean isATestSuite() {
267
268 Iterator it1 = this.suites.keySet().iterator();
269
270 while (it1.hasNext()) {
271 String key = (String) it1.next();
272 Set s = (Set) this.suites.get(key);
273
274 if (s.size() > 0)
275 return true;
276 }
277
278 return false;
279 }
280
281 }