4 ToolChainMap class is used for storing tool chain configurations.
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
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.
17 package org
.tianocore
.build
.toolchain
;
19 import java
.util
.HashMap
;
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
28 public class ToolChainMap
{
30 // From which part of key can be used to match "*"
32 private int matchLevel
= ToolChainKey
.keyLength
- 2;
35 // A Map object in which tool chain configuration information will be stored
37 private Map
<ToolChainKey
, String
> map
= null;
40 Public constructor. It just initializes the private Map object.
42 public ToolChainMap() {
43 this.map
= new HashMap
<ToolChainKey
, String
>();
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.
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"
54 @retval String The "value" string if the "key" is valid.
55 @retval null if the "key" is invalid
57 public String
put(String key
, String delimiter
, String value
) {
58 ToolChainKey toolChainKey
;
61 toolChainKey
= new ToolChainKey(key
, delimiter
);
62 } catch (Exception e
) {
65 return (String
)map
.put(toolChainKey
, value
);
69 Wrapper function for Map.put().
71 @param key Key string which is concatenated with default "delimiter"
72 @param value Value string associated with the "key"
74 @retval String The "value" string if the "key" is valid.
75 @retval null if the "key" is invalid
77 public String
put(String key
, String value
) {
78 ToolChainKey toolChainKey
;
81 toolChainKey
= new ToolChainKey(key
);
82 } catch (Exception e
) {
85 return (String
)map
.put(toolChainKey
, value
);
89 Wrapper function for Map.put(). The key is given in the form of string
92 @param key Key string array
93 @param value Value string associated with the "key"
95 @retval String The "value" string if the "key" is valid.
96 @retval null if the "key" is invalid
98 public String
put(String
[] key
, String value
) {
99 ToolChainKey toolChainKey
;
102 toolChainKey
= new ToolChainKey(key
);
103 } catch (Exception e
) {
106 return (String
)map
.put(toolChainKey
, value
);
110 Wrapper function for Map.put(). The key is given in ToolChainKey class.
112 @param key ToolChainKey class
113 @param value Value string associated with the "key"
115 @retval String The "value" string if the "key" is valid.
116 @retval null if the "key" is invalid
118 public String
put(ToolChainKey key
, String value
) {
119 return (String
)map
.put(key
, value
);
123 Wrapper function for Map.get().
125 @param key Key string which is concatenated with default "delimiter"
129 public String
get(String key
) {
130 ToolChainKey toolChainKey
;
133 toolChainKey
= new ToolChainKey(key
);
134 } catch (Exception e
) {
137 return get(toolChainKey
);
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.
144 @param key Key string which is concatenated with "delimiter"
145 @param delimiter The delimiter string in the key string
149 public String
get(String key
, String delimiter
) {
150 ToolChainKey toolChainKey
;
153 toolChainKey
= new ToolChainKey(key
, delimiter
);
154 } catch (Exception e
) {
157 return get(toolChainKey
);
161 Wrapper function for Map.get(). The key is given in the form of string
164 @param key Key string array
168 public String
get(String
[] key
) {
169 ToolChainKey toolChainKey
;
172 toolChainKey
= new ToolChainKey(key
);
173 } catch (Exception e
) {
176 return get(toolChainKey
);
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".
185 @param key ToolChainKey class
187 @return String The value associated with the key
189 public String
get(ToolChainKey key
) {
191 /// First, we'll try to get the value through the exact given key
193 String result
= map
.get(key
);
194 if (result
!= null || map
.containsKey(key
)) {
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.
203 String
[] keySet
= key
.getKeySet();
206 tmpKey
= new ToolChainKey(keySet
);
207 } catch (Exception e
) {
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.
217 /// The following code implements the logic which will try to use, for example,
218 /// following key parts combinations sequentially to get the value.
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
239 // level is used to control if all parts of "name" have been "wildcarded"
241 int level
= matchLevel
;
244 // tmplevel is used to control if all parts of "name" between first
245 // "*" and fourth name part have been "wildcarded".
247 int tmpLevel
= level
;
248 while (tmpLevel
>= level
) {
249 String
[] tmpKeySet
= tmpKey
.getKeySet();
251 if (!tmpKeySet
[tmpLevel
].equals("*")) {
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
258 tmpKey
.setKey("*", tmpLevel
);
260 // We'll try all possible combinations between current
261 // part and the fourth part.
263 tmpLevel
= matchLevel
;
266 // Restore original value of key if "*" at "tmplevel"
267 // part of "name" has been checked
269 tmpKey
.setKey(keySet
[tmpLevel
], tmpLevel
);
271 // Try "*" at part left to "tmplevel" part of "name"
276 } catch (Exception e
) {
281 // Try get the value from the map
283 result
= map
.get(tmpKey
);
284 if (result
!= null) {
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
289 map
.put(key
, result
);
294 /// If all possible combinations of "wildcard" between "level" and
295 /// the fourth part of "name" have been tried, try the left part
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.
304 map
.put(key
, result
);
309 Wrapper function for Map.size().
311 @return int The size of map
318 Wrapper function for Map.keySet().
320 @return Set<ToolChainKey> A set of ToolChainKey objects
322 public Set
<ToolChainKey
> keySet() {
323 return (Set
<ToolChainKey
>)map
.keySet();