a) Cleaned tools_def.template
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / toolchain / ToolChainMap.java
1 /** @file
2 ToolChainMap class
3
4 ToolChainMap class is used for storing tool chain configurations.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 package org.tianocore.build.toolchain;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22
23 /**
24 ToolChainMap is a wrapper class for a generic Map class which uses ToolChainKey
25 class as its key. It's used to store and retrieve tool chain configuration
26 information.
27 **/
28 public class ToolChainMap {
29 //
30 // From which part of key can be used to match "*"
31 //
32 private int matchLevel = ToolChainKey.keyLength - 1;
33
34 //
35 // A Map object in which tool chain configuration information will be stored
36 //
37 private Map<ToolChainKey, String> map = null;
38
39 /**
40 Public constructor. It just initializes the private Map object.
41 **/
42 public ToolChainMap() {
43 this.map = new HashMap<ToolChainKey, String>();
44 }
45
46 /**
47 Wrapper function for Map.put(). It's used when default delimiter of
48 ToolChainKey is not wanted and will be overrided by "delimiter" parameter.
49
50 @param key Key string which is concatenated with "delimiter"
51 @param delimiter The delimiter string in the key string
52 @param value Value string associated with the "key"
53
54 @retval String The "value" string if the "key" is valid.
55 @retval null if the "key" is invalid
56 **/
57 public String put(String key, String delimiter, String value) {
58 ToolChainKey toolChainKey;
59
60 try {
61 toolChainKey = new ToolChainKey(key, delimiter);
62 } catch (Exception e) {
63 return null;
64 }
65 return (String)map.put(toolChainKey, value);
66 }
67
68 /**
69 Wrapper function for Map.put().
70
71 @param key Key string which is concatenated with default "delimiter"
72 @param value Value string associated with the "key"
73
74 @retval String The "value" string if the "key" is valid.
75 @retval null if the "key" is invalid
76 **/
77 public String put(String key, String value) {
78 ToolChainKey toolChainKey;
79
80 try {
81 toolChainKey = new ToolChainKey(key);
82 } catch (Exception e) {
83 return null;
84 }
85 return (String)map.put(toolChainKey, value);
86 }
87
88 /**
89 Wrapper function for Map.put(). The key is given in the form of string
90 array.
91
92 @param key Key string array
93 @param value Value string associated with the "key"
94
95 @retval String The "value" string if the "key" is valid.
96 @retval null if the "key" is invalid
97 **/
98 public String put(String[] key, String value) {
99 ToolChainKey toolChainKey;
100
101 try {
102 toolChainKey = new ToolChainKey(key);
103 } catch (Exception e) {
104 return null;
105 }
106 return (String)map.put(toolChainKey, value);
107 }
108
109 /**
110 Wrapper function for Map.put(). The key is given in ToolChainKey class.
111
112 @param key ToolChainKey class
113 @param value Value string associated with the "key"
114
115 @retval String The "value" string if the "key" is valid.
116 @retval null if the "key" is invalid
117 **/
118 public String put(ToolChainKey key, String value) {
119 return (String)map.put(key, value);
120 }
121
122 /**
123 Wrapper function for Map.get().
124
125 @param key Key string which is concatenated with default "delimiter"
126
127 @return String
128 **/
129 public String get(String key) {
130 ToolChainKey toolChainKey;
131
132 try {
133 toolChainKey = new ToolChainKey(key);
134 } catch (Exception e) {
135 return null;
136 }
137 return get(toolChainKey);
138 }
139
140 /**
141 Wrapper function for Map.get(). It's used when default delimiter of
142 ToolChainKey is not wanted and will be overrided by "delimiter" parameter.
143
144 @param key Key string which is concatenated with "delimiter"
145 @param delimiter The delimiter string in the key string
146
147 @return String
148 **/
149 public String get(String key, String delimiter) {
150 ToolChainKey toolChainKey;
151
152 try {
153 toolChainKey = new ToolChainKey(key, delimiter);
154 } catch (Exception e) {
155 return null;
156 }
157 return get(toolChainKey);
158 }
159
160 /**
161 Wrapper function for Map.get(). The key is given in the form of string
162 array.
163
164 @param key Key string array
165
166 @return String
167 **/
168 public String get(String[] key) {
169 ToolChainKey toolChainKey;
170
171 try {
172 toolChainKey = new ToolChainKey(key);
173 } catch (Exception e) {
174 return null;
175 }
176 return get(toolChainKey);
177 }
178
179 /**
180 Wrapper function for Map.get(). The key is given in ToolChainKey class.
181 All other form of get() method will eventually call this form of get. It
182 will do real job of finding the value associated with the given key. Most
183 of the job is to try to match the key with "wildcard".
184
185 @param key ToolChainKey class
186
187 @return String The value associated with the key
188 **/
189 public String get(ToolChainKey key) {
190 ///
191 /// First, we'll try to get the value through the exact given key
192 ///
193 String result = map.get(key);
194 if (result != null || map.containsKey(key)) {
195 return result;
196 }
197
198 ///
199 /// If nothing is found, then, we'll try all possible keys combined with
200 /// wildcard "*". In order not to change the original key value, we have
201 /// to clone one for later use.
202 ///
203 String[] keySet = key.getKeySet();
204 ToolChainKey tmpKey;
205 try {
206 tmpKey = new ToolChainKey(keySet);
207 } catch (Exception e) {
208 return null;
209 }
210
211 ///
212 /// In the current tool chain definition format (in name/value pair),
213 /// there're five parts in the "name". The last part of the "name" must
214 /// not be "wildcard". We should start combining "*" from left to right.
215 /// We'll try all the possible combinations until the value can be fetched.
216 ///
217 /// The following code implements the logic which will try to use, for example,
218 /// following key parts combinations sequentially to get the value.
219 ///
220 /// TARGET_TOOLCHAIN_ARCH_TOOLCODE_ATTRIBUTE
221 /// ******_TOOLCHAIN_ARCH_TOOLCODE_ATTRIBUTE
222 /// TARGET_*********_ARCH_TOOLCODE_ATTRIBUTE
223 /// ******_*********_ARCH_TOOLCODE_ATTRIBUTE
224 /// TARGET_TOOLCHAIN_****_TOOLCODE_ATTRIBUTE
225 /// ******_TOOLCHAIN_****_TOOLCODE_ATTRIBUTE
226 /// TARGET_*********_****_TOOLCODE_ATTRIBUTE
227 /// ******_*********_****_TOOLCODE_ATTRIBUTE
228 /// TARGET_TOOLCHAIN_ARCH_********_ATTRIBUTE
229 /// ******_TOOLCHAIN_ARCH_********_ATTRIBUTE
230 /// TARGET_*********_ARCH_********_ATTRIBUTE
231 /// ******_*********_ARCH_********_ATTRIBUTE
232 /// TARGET_TOOLCHAIN_****_********_ATTRIBUTE
233 /// ******_TOOLCHAIN_****_********_ATTRIBUTE
234 /// TARGET_*********_****_********_ATTRIBUTE
235 /// ******_*********_****_********_ATTRIBUTE
236 ///
237
238 //
239 // The wildcard "*" appears regularly (2^n). "*" in TARGET appears 2^0
240 // times at every 2^0 TARGET, "*" in TOOLCHAIN appears 2^1 times at
241 // every 2^1 TOOLCHAIN, and "*" in TOOLCODE appears 2^3 times at every
242 // 2^3 TOOLCODE. We're going to use this to form all the combinations of key.
243 //
244 int[] combinations = new int[matchLevel];
245 for (int i = 0; i < matchLevel; ++i) {
246 //
247 // initialize the array with 2^n
248 //
249 combinations[i] = 1 << (i + 1);
250 }
251
252 //
253 // when last part goes down to zero, we tried all combinations of key
254 //
255 int lastIndex = matchLevel - 1;
256 while (combinations[lastIndex] > 0) {
257 //
258 // form the key which has "*" in it
259 //
260 for (int i = 0; i < matchLevel; ++i) {
261 //
262 // start again if not finished
263 //
264 if (combinations[i] == 0) {
265 combinations[i] = 1 << (i + 1);
266 }
267
268 //
269 // half of 2^n is "*", the rest is non-*
270 //
271 try {
272 if (combinations[i] > (1 << i)) {
273 tmpKey.setKey(keySet[i], i);
274 } else {
275 tmpKey.setKey("*", i);
276 }
277 } catch (Exception e) {
278 return null;
279 }
280
281 combinations[i] -= 1;
282 }
283
284 //
285 // Try get the value from the map
286 //
287 result = map.get(tmpKey);
288 if (result != null) {
289 //
290 // The map actually has no exact key as the given "key",
291 // putting it back into map can speed up the get() next time
292 //
293 map.put(key, result);
294 return result;
295 }
296 }
297
298 //
299 // The map actually has no exact key as the given "key", putting it back
300 // into map can speed up the get() next time even we got nothing.
301 //
302 map.put(key, null);
303 return null;
304 }
305
306 /**
307 Wrapper function for Map.size().
308
309 @return int The size of map
310 **/
311 public int size() {
312 return map.size();
313 }
314
315 /**
316 Wrapper function for Map.keySet().
317
318 @return Set<ToolChainKey> A set of ToolChainKey objects
319 */
320 public Set<ToolChainKey> keySet() {
321 return (Set<ToolChainKey>)map.keySet();
322 }
323 }
324