]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Sort.java
88fe64e4d93cf2853a5cd75a14e9203a0136ddd3
[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.PcdCoded.PcdIdentification;
22 import org.tianocore.frameworkwizard.module.Identifications.PcdCoded.PcdVector;
23 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
24 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
25
26 public class Sort {
27
28 /**
29 Sort all elements in the vector as the specific sort type
30
31 @param v The vector need to be sorted
32 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
33
34 **/
35 public static void sortVectorString(Vector<String> v, int mode) {
36 if (v != null) {
37 for (int indexI = 0; indexI < v.size(); indexI++) {
38 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
39 if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
40 || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
41 String temp = v.get(indexI);
42 v.setElementAt(v.get(indexJ), indexI);
43 v.setElementAt(temp, indexJ);
44 }
45 }
46 }
47 }
48 }
49
50 /**
51 Sort all elements of vector and return sorted sequence
52
53 @param v The vector need to be sorted
54 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
55 @return Vector<Integer> The sorted sequence
56
57 **/
58 public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {
59 Vector<Integer> vSequence = new Vector<Integer>();
60 //
61 // Init sequence
62 //
63 if (v != null) {
64 for (int index = 0; index < v.size(); index++) {
65 vSequence.addElement(index);
66 }
67 }
68
69 //
70 // sort and get new sequence
71 //
72 for (int indexI = 0; indexI < v.size(); indexI++) {
73 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
74 if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
75 || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
76 //
77 // Swap strings
78 //
79 String tempStr = v.get(indexI);
80 v.setElementAt(v.get(indexJ), indexI);
81 v.setElementAt(tempStr, indexJ);
82
83 //
84 // Swap sequences
85 //
86 int tempInt = vSequence.get(indexI);
87 vSequence.setElementAt(vSequence.get(indexJ), indexI);
88 vSequence.setElementAt(tempInt, indexJ);
89 }
90 }
91 }
92
93 return vSequence;
94 }
95
96 /**
97 Sort all elements of vector as input sequence
98
99 @param v The vector need to be sorted
100 @param vSequence The sort sequence should be followed
101
102 **/
103 public static void sortVectorString(Vector<String> v, Vector<Integer> vSequence) {
104 if (v != null && vSequence != null && v.size() == vSequence.size()) {
105 Vector<String> tempV = new Vector<String>();
106 for (int index = 0; index < v.size(); index++) {
107 tempV.addElement(v.get(index));
108 }
109 for (int index = 0; index < v.size(); index++) {
110 v.setElementAt(tempV.get(vSequence.get(index)), index);
111 }
112 }
113 }
114
115 /**
116 Sort all modules
117
118 @param v
119 @param mode
120
121 **/
122 public static void sortModules(Vector<ModuleIdentification> v, int mode) {
123 if (v != null) {
124 //
125 // sort by name
126 //
127 for (int indexI = 0; indexI < v.size(); indexI++) {
128 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
129 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
130 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
131 ModuleIdentification temp = v.get(indexI);
132 v.setElementAt(v.get(indexJ), indexI);
133 v.setElementAt(temp, indexJ);
134 }
135 }
136 }
137 }
138 }
139
140 /**
141 Sort all packages
142
143 @param v
144 @param mode
145
146 **/
147 public static void sortPackages(Vector<PackageIdentification> v, int mode) {
148 if (v != null) {
149 //
150 // sort by name
151 //
152 for (int indexI = 0; indexI < v.size(); indexI++) {
153 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
154 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
155 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
156 PackageIdentification temp = v.get(indexI);
157 v.setElementAt(v.get(indexJ), indexI);
158 v.setElementAt(temp, indexJ);
159 }
160 }
161 }
162 }
163 }
164
165 /**
166 Sort all platforms
167
168 @param v
169 @param mode
170
171 **/
172 public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {
173 if (v != null) {
174 //
175 // sort by name
176 //
177 for (int indexI = 0; indexI < v.size(); indexI++) {
178 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
179 if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
180 || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
181 PlatformIdentification temp = v.get(indexI);
182 v.setElementAt(v.get(indexJ), indexI);
183 v.setElementAt(temp, indexJ);
184 }
185 }
186 }
187 }
188 }
189
190 /**
191 Sort all pcd entries
192
193 @param v
194 @param mode
195
196 **/
197 public static void sortPcds(PcdVector v, int mode) {
198 if (v != null) {
199 //
200 // sort by name
201 //
202 for (int indexI = 0; indexI < v.size(); indexI++) {
203 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
204 if ((v.getPcd(indexJ).getName().compareTo(v.getPcd(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
205 || (v.getPcd(indexI).getName().compareTo(v.getPcd(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
206 PcdIdentification temp = v.getPcd(indexI);
207 v.setPcd(v.getPcd(indexJ), indexI);
208 v.setPcd(temp, indexJ);
209 }
210 }
211 }
212 }
213 }
214
215 /**
216 Sort all objects of a vector based on the object's "toString"
217
218 @param v
219 @param mode
220
221 **/
222 public static void sortObjectVector(Vector<Object> v, int mode) {
223 if (v != null) {
224 //
225 // sort by name
226 //
227 for (int indexI = 0; indexI < v.size(); indexI++) {
228 for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {
229 if ((v.get(indexJ).toString().compareTo(v.get(indexI).toString()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)
230 || (v.get(indexI).toString().compareTo(v.get(indexJ).toString()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {
231 Object temp = v.get(indexI);
232 v.setElementAt(v.get(indexJ), indexI);
233 v.setElementAt(temp, indexJ);
234 }
235 }
236 }
237 }
238 }
239 }