b9d24eb0b95d987b34a3489b9572820cc21c6d43
[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 - 2;
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". So we should start combining "*" from the fourth part.
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 /// TARGET_TOOLCHAIN_ARCH_*_ATTRIBUTE
222 /// TARGET_TOOLCHAIN_*_TOOLCODE_ATTRIBUTE
223 /// TARGET_TOOLCHAIN_*_*_ATTRIBUTE
224 /// TARGET_*_ARCH_TOOLCODE_ATTRIBUTE
225 /// TARGET_*_ARCH_*_ATTRIBUTE
226 /// TARGET_*_*_TOOLCODE_ATTRIBUTE
227 /// TARGET_*_*_*_ATTRIBUTE
228 /// *_TOOLCHAIN_ARCH_TOOLCODE_ATTRIBUTE
229 /// *_TOOLCHAIN_ARCH_*_ATTRIBUTE
230 /// *_TOOLCHAIN_*_TOOLCODE_ATTRIBUTE
231 /// *_TOOLCHAIN_*_*_ATTRIBUTE
232 /// *_*_ARCH_TOOLCODE_ATTRIBUTE
233 /// *_*_ARCH_*_ATTRIBUTE
234 /// *_*_*_TOOLCODE_ATTRIBUTE
235 /// *_*_*_*_ATTRIBUTE
236 ///
237
238 //
239 // level is used to control if all parts of "name" have been "wildcarded"
240 //
241 int level = matchLevel;
242 while (level >= 0) {
243 //
244 // tmplevel is used to control if all parts of "name" between first
245 // "*" and fourth name part have been "wildcarded".
246 //
247 int tmpLevel = level;
248 while (tmpLevel >= level) {
249 String[] tmpKeySet = tmpKey.getKeySet();
250 try {
251 if (!tmpKeySet[tmpLevel].equals("*")) {
252 //
253 // If "tmplevel" part is not "*", set it to "*".
254 // For example, at first loop, the key will become
255 // TARGET_TOOLCHAIN_ARCH_*_ATTRIBUTE, and at next loop,
256 // become TARGET_TOOLCHAIN_*_ARCH_ATTRIBUTE
257 //
258 tmpKey.setKey("*", tmpLevel);
259 //
260 // We'll try all possible combinations between current
261 // part and the fourth part.
262 //
263 tmpLevel = matchLevel;
264 } else {
265 //
266 // Restore original value of key if "*" at "tmplevel"
267 // part of "name" has been checked
268 //
269 tmpKey.setKey(keySet[tmpLevel], tmpLevel);
270 //
271 // Try "*" at part left to "tmplevel" part of "name"
272 //
273 --tmpLevel;
274 continue;
275 }
276 } catch (Exception e) {
277 return null;
278 }
279
280 //
281 // Try get the value from the map
282 //
283 result = map.get(tmpKey);
284 if (result != null) {
285 //
286 // The map actually has no exact key as the given "key",
287 // putting it back into map can speed up the get() next time
288 //
289 map.put(key, result);
290 return result;
291 }
292 }
293 ///
294 /// If all possible combinations of "wildcard" between "level" and
295 /// the fourth part of "name" have been tried, try the left part
296 ///
297 --level;
298 }
299
300 //
301 // The map actually has no exact key as the given "key", putting it back
302 // into map can speed up the get() next time even we got nothing.
303 //
304 map.put(key, result);
305 return result;
306 }
307
308 /**
309 Wrapper function for Map.size().
310
311 @return int The size of map
312 **/
313 public int size() {
314 return map.size();
315 }
316
317 /**
318 Wrapper function for Map.keySet().
319
320 @return Set<ToolChainKey> A set of ToolChainKey objects
321 */
322 public Set<ToolChainKey> keySet() {
323 return (Set<ToolChainKey>)map.keySet();
324 }
325 }
326