]>
Commit | Line | Data |
---|---|---|
2003a22e | 1 | /** @file\r |
2 | \r | |
3 | The file is used to provide all kinds of sorting method \r | |
4 | \r | |
5 | Copyright (c) 2006, Intel Corporation\r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | package org.tianocore.frameworkwizard.common;\r | |
17 | \r | |
18 | import java.util.Vector;\r | |
19 | \r | |
20 | import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;\r | |
55e83b18 | 21 | import org.tianocore.frameworkwizard.module.Identifications.Guids.GuidsIdentification;\r |
22 | import org.tianocore.frameworkwizard.module.Identifications.Guids.GuidsVector;\r | |
23 | import org.tianocore.frameworkwizard.module.Identifications.LibraryClass.LibraryClassIdentification;\r | |
24 | import org.tianocore.frameworkwizard.module.Identifications.LibraryClass.LibraryClassVector;\r | |
25 | import org.tianocore.frameworkwizard.module.Identifications.PcdCoded.PcdCodedIdentification;\r | |
26 | import org.tianocore.frameworkwizard.module.Identifications.PcdCoded.PcdCodedVector;\r | |
2003a22e | 27 | import org.tianocore.frameworkwizard.module.Identifications.PcdCoded.PcdIdentification;\r |
28 | import org.tianocore.frameworkwizard.module.Identifications.PcdCoded.PcdVector;\r | |
55e83b18 | 29 | import org.tianocore.frameworkwizard.module.Identifications.Ppis.PpisIdentification;\r |
30 | import org.tianocore.frameworkwizard.module.Identifications.Ppis.PpisVector;\r | |
31 | import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsIdentification;\r | |
32 | import org.tianocore.frameworkwizard.module.Identifications.Protocols.ProtocolsVector;\r | |
2003a22e | 33 | import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r |
34 | import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r | |
35 | \r | |
36 | public class Sort {\r | |
37 | \r | |
38 | /**\r | |
39 | Sort all elements in the vector as the specific sort type\r | |
40 | \r | |
41 | @param v The vector need to be sorted\r | |
42 | @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending\r | |
43 | \r | |
44 | **/\r | |
45 | public static void sortVectorString(Vector<String> v, int mode) {\r | |
46 | if (v != null) {\r | |
47 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
48 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
49 | if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
50 | || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
51 | String temp = v.get(indexI);\r | |
52 | v.setElementAt(v.get(indexJ), indexI);\r | |
53 | v.setElementAt(temp, indexJ);\r | |
54 | }\r | |
55 | }\r | |
56 | }\r | |
57 | }\r | |
58 | }\r | |
59 | \r | |
60 | /**\r | |
61 | Sort all elements of vector and return sorted sequence\r | |
62 | \r | |
63 | @param v The vector need to be sorted\r | |
64 | @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending\r | |
65 | @return Vector<Integer> The sorted sequence\r | |
66 | \r | |
67 | **/\r | |
68 | public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {\r | |
69 | Vector<Integer> vSequence = new Vector<Integer>();\r | |
70 | //\r | |
71 | // Init sequence\r | |
72 | //\r | |
73 | if (v != null) {\r | |
74 | for (int index = 0; index < v.size(); index++) {\r | |
75 | vSequence.addElement(index);\r | |
76 | }\r | |
77 | }\r | |
78 | \r | |
79 | //\r | |
80 | // sort and get new sequence\r | |
81 | //\r | |
82 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
83 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
84 | if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
85 | || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
86 | //\r | |
87 | // Swap strings\r | |
88 | //\r | |
89 | String tempStr = v.get(indexI);\r | |
90 | v.setElementAt(v.get(indexJ), indexI);\r | |
91 | v.setElementAt(tempStr, indexJ);\r | |
92 | \r | |
93 | //\r | |
94 | // Swap sequences\r | |
95 | //\r | |
96 | int tempInt = vSequence.get(indexI);\r | |
97 | vSequence.setElementAt(vSequence.get(indexJ), indexI);\r | |
98 | vSequence.setElementAt(tempInt, indexJ);\r | |
99 | }\r | |
100 | }\r | |
101 | }\r | |
102 | \r | |
103 | return vSequence;\r | |
104 | }\r | |
105 | \r | |
106 | /**\r | |
107 | Sort all elements of vector as input sequence\r | |
108 | \r | |
109 | @param v The vector need to be sorted\r | |
110 | @param vSequence The sort sequence should be followed\r | |
111 | \r | |
112 | **/\r | |
113 | public static void sortVectorString(Vector<String> v, Vector<Integer> vSequence) {\r | |
114 | if (v != null && vSequence != null && v.size() == vSequence.size()) {\r | |
115 | Vector<String> tempV = new Vector<String>();\r | |
116 | for (int index = 0; index < v.size(); index++) {\r | |
117 | tempV.addElement(v.get(index));\r | |
118 | }\r | |
119 | for (int index = 0; index < v.size(); index++) {\r | |
120 | v.setElementAt(tempV.get(vSequence.get(index)), index);\r | |
121 | }\r | |
122 | }\r | |
123 | }\r | |
124 | \r | |
125 | /**\r | |
126 | Sort all modules\r | |
127 | \r | |
128 | @param v\r | |
129 | @param mode\r | |
130 | \r | |
131 | **/\r | |
132 | public static void sortModules(Vector<ModuleIdentification> v, int mode) {\r | |
133 | if (v != null) {\r | |
134 | //\r | |
135 | // sort by name\r | |
136 | //\r | |
137 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
138 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
139 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
140 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
141 | ModuleIdentification temp = v.get(indexI);\r | |
142 | v.setElementAt(v.get(indexJ), indexI);\r | |
143 | v.setElementAt(temp, indexJ);\r | |
144 | }\r | |
145 | }\r | |
146 | }\r | |
147 | }\r | |
148 | }\r | |
149 | \r | |
150 | /**\r | |
151 | Sort all packages\r | |
152 | \r | |
153 | @param v\r | |
154 | @param mode\r | |
155 | \r | |
156 | **/\r | |
157 | public static void sortPackages(Vector<PackageIdentification> v, int mode) {\r | |
158 | if (v != null) {\r | |
159 | //\r | |
160 | // sort by name\r | |
161 | //\r | |
162 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
163 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
164 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
165 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
166 | PackageIdentification temp = v.get(indexI);\r | |
167 | v.setElementAt(v.get(indexJ), indexI);\r | |
168 | v.setElementAt(temp, indexJ);\r | |
169 | }\r | |
170 | }\r | |
171 | }\r | |
172 | }\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | Sort all platforms\r | |
177 | \r | |
178 | @param v\r | |
179 | @param mode\r | |
180 | \r | |
181 | **/\r | |
182 | public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {\r | |
183 | if (v != null) {\r | |
184 | //\r | |
185 | // sort by name\r | |
186 | //\r | |
187 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
188 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
189 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
190 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
191 | PlatformIdentification temp = v.get(indexI);\r | |
192 | v.setElementAt(v.get(indexJ), indexI);\r | |
193 | v.setElementAt(temp, indexJ);\r | |
194 | }\r | |
195 | }\r | |
196 | }\r | |
197 | }\r | |
198 | }\r | |
199 | \r | |
200 | /**\r | |
201 | Sort all pcd entries\r | |
202 | \r | |
203 | @param v\r | |
204 | @param mode\r | |
205 | \r | |
206 | **/\r | |
207 | public static void sortPcds(PcdVector v, int mode) {\r | |
208 | if (v != null) {\r | |
209 | //\r | |
210 | // sort by name\r | |
211 | //\r | |
212 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
213 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
214 | if ((v.getPcd(indexJ).getName().compareTo(v.getPcd(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
215 | || (v.getPcd(indexI).getName().compareTo(v.getPcd(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
216 | PcdIdentification temp = v.getPcd(indexI);\r | |
217 | v.setPcd(v.getPcd(indexJ), indexI);\r | |
218 | v.setPcd(temp, indexJ);\r | |
219 | }\r | |
55e83b18 | 220 | }\r |
221 | }\r | |
222 | }\r | |
223 | }\r | |
224 | \r | |
225 | /**\r | |
226 | Sort all ppi entries\r | |
227 | \r | |
228 | @param v\r | |
229 | @param mode\r | |
230 | \r | |
231 | **/\r | |
232 | public static void sortPpis(PpisVector v, int mode) {\r | |
233 | if (v != null) {\r | |
234 | //\r | |
235 | // sort by name\r | |
236 | //\r | |
237 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
238 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
239 | if ((v.getPpis(indexJ).getName().compareTo(v.getPpis(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
240 | || (v.getPpis(indexI).getName().compareTo(v.getPpis(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
241 | PpisIdentification temp = v.getPpis(indexI);\r | |
242 | v.setPpis(v.getPpis(indexJ), indexI);\r | |
243 | v.setPpis(temp, indexJ);\r | |
244 | }\r | |
245 | }\r | |
246 | }\r | |
247 | }\r | |
248 | }\r | |
249 | \r | |
250 | /**\r | |
251 | Sort all protocol entries\r | |
252 | \r | |
253 | @param v\r | |
254 | @param mode\r | |
255 | \r | |
256 | **/\r | |
257 | public static void sortProtocols(ProtocolsVector v, int mode) {\r | |
258 | if (v != null) {\r | |
259 | //\r | |
260 | // sort by name\r | |
261 | //\r | |
262 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
263 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
264 | if ((v.getProtocols(indexJ).getName().compareTo(v.getProtocols(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
265 | || (v.getProtocols(indexI).getName().compareTo(v.getProtocols(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
266 | ProtocolsIdentification temp = v.getProtocols(indexI);\r | |
267 | v.setProtocols(v.getProtocols(indexJ), indexI);\r | |
268 | v.setProtocols(temp, indexJ);\r | |
269 | }\r | |
270 | }\r | |
271 | }\r | |
272 | }\r | |
273 | }\r | |
274 | \r | |
275 | /**\r | |
276 | Sort all guid entries\r | |
277 | \r | |
278 | @param v\r | |
279 | @param mode\r | |
280 | \r | |
281 | **/\r | |
282 | public static void sortGuids(GuidsVector v, int mode) {\r | |
283 | if (v != null) {\r | |
284 | //\r | |
285 | // sort by name\r | |
286 | //\r | |
287 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
288 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
289 | if ((v.getGuids(indexJ).getName().compareTo(v.getGuids(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
290 | || (v.getGuids(indexI).getName().compareTo(v.getGuids(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
291 | GuidsIdentification temp = v.getGuids(indexI);\r | |
292 | v.setGuids(v.getGuids(indexJ), indexI);\r | |
293 | v.setGuids(temp, indexJ);\r | |
294 | }\r | |
295 | }\r | |
296 | }\r | |
297 | }\r | |
298 | }\r | |
299 | \r | |
300 | /**\r | |
301 | Sort all pcd coded entries\r | |
302 | \r | |
303 | @param v\r | |
304 | @param mode\r | |
305 | \r | |
306 | **/\r | |
307 | public static void sortPcdCodeds(PcdCodedVector v, int mode) {\r | |
308 | if (v != null) {\r | |
309 | //\r | |
310 | // sort by name\r | |
311 | //\r | |
312 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
313 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
314 | if ((v.getPcdCoded(indexJ).getName().compareTo(v.getPcdCoded(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
315 | || (v.getPcdCoded(indexI).getName().compareTo(v.getPcdCoded(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
316 | PcdCodedIdentification temp = v.getPcdCoded(indexI);\r | |
317 | v.setPcdCoded(v.getPcdCoded(indexJ), indexI);\r | |
318 | v.setPcdCoded(temp, indexJ);\r | |
319 | }\r | |
320 | }\r | |
321 | }\r | |
322 | }\r | |
323 | }\r | |
324 | \r | |
325 | /**\r | |
326 | Sort all pcd coded entries\r | |
327 | \r | |
328 | @param v\r | |
329 | @param mode\r | |
330 | \r | |
331 | **/\r | |
332 | public static void sortLibraryClass(LibraryClassVector v, int mode) {\r | |
333 | if (v != null) {\r | |
334 | //\r | |
335 | // sort by name\r | |
336 | //\r | |
337 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
338 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
339 | if ((v.getLibraryClass(indexJ).getLibraryClassName().compareTo(\r | |
340 | v.getLibraryClass(indexI)\r | |
341 | .getLibraryClassName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
342 | || (v.getLibraryClass(indexI).getLibraryClassName().compareTo(\r | |
343 | v.getLibraryClass(indexJ)\r | |
344 | .getLibraryClassName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
345 | LibraryClassIdentification temp = v.getLibraryClass(indexI);\r | |
346 | v.setLibraryClass(v.getLibraryClass(indexJ), indexI);\r | |
347 | v.setLibraryClass(temp, indexJ);\r | |
348 | }\r | |
2003a22e | 349 | }\r |
350 | }\r | |
351 | }\r | |
352 | }\r | |
353 | \r | |
354 | /**\r | |
355 | Sort all objects of a vector based on the object's "toString"\r | |
356 | \r | |
357 | @param v\r | |
358 | @param mode\r | |
359 | \r | |
360 | **/\r | |
361 | public static void sortObjectVector(Vector<Object> v, int mode) {\r | |
362 | if (v != null) {\r | |
363 | //\r | |
364 | // sort by name\r | |
365 | //\r | |
366 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
367 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
368 | if ((v.get(indexJ).toString().compareTo(v.get(indexI).toString()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
369 | || (v.get(indexI).toString().compareTo(v.get(indexJ).toString()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
370 | Object temp = v.get(indexI);\r | |
371 | v.setElementAt(v.get(indexJ), indexI);\r | |
372 | v.setElementAt(temp, indexJ);\r | |
373 | }\r | |
374 | }\r | |
375 | }\r | |
376 | }\r | |
377 | }\r | |
378 | }\r |