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