1. Update to just keep several line JAVA related msg; 2. Remove file PropertyManager...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / toolchain / ToolChainKey.java
CommitLineData
d2059d05 1/** @file\r
2ToolChainKey class\r
3\r
4ToolChainKey class is representing the "name" part of tool chain definition.\r
a29c47e0 5\r
6Copyright (c) 2006, Intel Corporation\r
7All rights reserved. This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
d2059d05 15**/\r
a29c47e0 16\r
17package org.tianocore.build.toolchain;\r
18\r
2279b26c 19import org.tianocore.build.exception.GenBuildException;\r
a29c47e0 20\r
d2059d05 21/**\r
22 ToolChainKey class is the java class form of the "name" of tool chain definition.\r
23 It's primarily for the key of a Map data structure.\r
24 **/\r
a29c47e0 25public class ToolChainKey implements java.io.Serializable, Comparable<ToolChainKey> {\r
a84091c4 26 static final long serialVersionUID = -8034897190740066933L;\r
a29c47e0 27\r
d2059d05 28 ///\r
29 /// The part number of key. Currently we only support fixed five parts.\r
30 /// \r
a29c47e0 31 public final static int keyLength = 5;\r
32\r
d2059d05 33 //\r
34 // Default delimiter which is used for concatenating the parts of key\r
35 // \r
36 private String delimiter = "_";\r
37\r
38 //\r
39 // Key value in string array form\r
40 // \r
a29c47e0 41 private String[] keySet = null;\r
42\r
d2059d05 43 //\r
44 // Key value in one string form\r
45 // \r
a29c47e0 46 private String keyString = null;\r
47\r
d2059d05 48 //\r
49 // Key hash value used for hash table \r
50 // \r
a29c47e0 51 private int hashValue = 0;\r
52\r
d2059d05 53 /**\r
54 Public constructor which can override default delimiter.\r
55\r
56 @param keyString The key string value\r
57 @param delimiter Delimiter charater concatenating the key parts\r
58 **/\r
2279b26c 59 public ToolChainKey(String keyString, String delimiter) throws GenBuildException {\r
a29c47e0 60 setKey(keyString, delimiter);\r
61 }\r
62\r
d2059d05 63 /**\r
64 Public constructor which uses default delimiter.\r
65\r
66 @param keyString The key string value\r
67 **/\r
2279b26c 68 public ToolChainKey(String keyString) throws GenBuildException {\r
a29c47e0 69 setKey(keyString);\r
70 }\r
71\r
d2059d05 72 /**\r
73 Public constructor which doesn't use any delimiter.\r
74\r
75 @param keySet\r
76 **/\r
2279b26c 77 public ToolChainKey(String[] keySet) throws GenBuildException {\r
a29c47e0 78 setKey(keySet);\r
79 }\r
80\r
d2059d05 81 /**\r
82 Calculate hash value of the key string (without the delimiter). It's used\r
83 for Hash Table kind of Map.\r
84\r
85 @return int The hash value\r
86 **/\r
a29c47e0 87 public int hashCode() {\r
88 if (hashValue != 0) {\r
89 return hashValue;\r
90 }\r
91\r
92 for (int i = 0; i < keySet.length; ++i) {\r
93 char[] keyStringValue = new char[keySet[i].length()];\r
94 this.keySet[i].getChars(0, keyStringValue.length, keyStringValue, 0);\r
95\r
96 for (int j = 0; j < keyStringValue.length; ++j) {\r
97 hashValue = keyStringValue[j] + hashValue * 31;\r
98 }\r
99 }\r
100\r
101 return hashValue;\r
102 }\r
103\r
d2059d05 104 /**\r
105 Compare the string value of two keys . It's used for Tree kind of Map.\r
106 \r
107 @param dstKey Another key to compare to.\r
108 \r
109 @retval 0 Two keys are equal\r
110 @retval >0 This key is after the given key\r
111 @retval <0 This key is before the given key\r
112 **/\r
a29c47e0 113 public int compareTo(ToolChainKey dstKey) {\r
114 String[] dstKeySet = dstKey.getKeySet();\r
115 int result = 0;\r
4a6a5026 116 for (int i = 0; i < ToolChainKey.keyLength; ++i) {\r
a29c47e0 117 result = this.keySet[i].compareToIgnoreCase(dstKeySet[i]);\r
118 if (result != 0) {\r
119 break;\r
120 }\r
121 }\r
122\r
123 return result;\r
124 }\r
125\r
d2059d05 126 /**\r
127 Check if this key is the same as the given key.\r
128\r
129 @param o Another key to compare to\r
130 \r
131 @return boolean\r
132 **/\r
a29c47e0 133 public boolean equals(Object o) {\r
134 ToolChainKey dstKey = (ToolChainKey)o;\r
135 String[] dstKeySet = dstKey.getKeySet();\r
136\r
137 if (this == dstKey) {\r
138 return true;\r
139 }\r
140\r
4a6a5026 141 if (dstKeySet.length != ToolChainKey.keyLength) {\r
a29c47e0 142 return false;\r
143 }\r
144\r
4a6a5026 145 for (int i = 0; i < ToolChainKey.keyLength; ++i) {\r
a29c47e0 146 if (!this.keySet[i].equalsIgnoreCase(dstKeySet[i])) {\r
147 return false;\r
148 }\r
149 }\r
150\r
151 return true;\r
152 }\r
153\r
d2059d05 154 /**\r
155 Set the key value in form of string array.\r
156\r
157 @param keySet The string array of key value\r
158 **/\r
2279b26c 159 public void setKey(String[] keySet) throws GenBuildException {\r
4a6a5026 160 if (keySet.length != ToolChainKey.keyLength) {\r
2279b26c 161 throw new GenBuildException("Invalid ToolChain key");\r
a29c47e0 162 }\r
163\r
d2059d05 164 //\r
165 // Clone the string array because we don't want to change original one\r
166 // \r
4a6a5026 167 this.keySet = new String[ToolChainKey.keyLength];\r
168 System.arraycopy(keySet, 0, this.keySet, 0, ToolChainKey.keyLength);\r
169 for (int i = 0; i < ToolChainKey.keyLength; ++i) {\r
a29c47e0 170 if (this.keySet[i] == null || this.keySet[i].length() == 0) {\r
171 this.keySet[i] = "*";\r
172 }\r
173 }\r
d2059d05 174\r
175 //\r
176 // We need to re-generate the single key string and hash value.\r
177 // \r
a29c47e0 178 this.keyString = null;\r
179 this.hashValue = 0;\r
180 }\r
181\r
d2059d05 182 /**\r
183 Set key value at the specified key part .\r
184 \r
185 @param keySetString The new value of "index" part of key\r
186 @param index The key part index\r
187 **/\r
2279b26c 188 public void setKey(String keySetString, int index) throws GenBuildException {\r
4a6a5026 189 if (index >= ToolChainKey.keyLength) {\r
2279b26c 190 throw new GenBuildException("Invalid ToolChain key index");\r
a29c47e0 191 }\r
192\r
d2059d05 193 //\r
194 // Allow wildcard in key string\r
195 // \r
a29c47e0 196 if (keySetString == null || keySetString.length() == 0) {\r
197 keySetString = "*";\r
198 }\r
199 this.keySet[index] = keySetString;\r
d2059d05 200\r
201 //\r
202 // We need to re-generate the single key string and hash value.\r
203 // \r
a29c47e0 204 this.keyString = null;\r
205 this.hashValue = 0;\r
206 }\r
207\r
d2059d05 208 /**\r
209 Set key value in the form of single string.\r
210 \r
211 @param keyString The key value string\r
212 **/\r
2279b26c 213 public void setKey(String keyString) throws GenBuildException {\r
a29c47e0 214 this.keySet = keyString.split(this.delimiter);\r
215\r
4a6a5026 216 if (this.keySet.length != ToolChainKey.keyLength) {\r
2279b26c 217 throw new GenBuildException("Invalid ToolChain key");\r
a29c47e0 218 }\r
219\r
220 this.keyString = keyString;\r
d2059d05 221 //\r
222 // We need to re-generate hash value.\r
223 // \r
a29c47e0 224 this.hashValue = 0;\r
225 }\r
226\r
d2059d05 227 /**\r
228 Set key value in the form of single string with specified delimiter.\r
229 \r
230 @param keyString The key value string\r
231 @param delimiter The delimiter concatenating the key string\r
232 **/\r
2279b26c 233 public void setKey(String keyString, String delimiter) throws GenBuildException {\r
a29c47e0 234 this.keySet = keyString.split(delimiter);\r
235\r
4a6a5026 236 if (this.keySet.length != ToolChainKey.keyLength) {\r
2279b26c 237 throw new GenBuildException("Invalid ToolChain key");\r
a29c47e0 238 }\r
239\r
240 this.keyString = keyString;\r
241 this.delimiter = delimiter;\r
d2059d05 242 //\r
243 // We need to re-generate hash value.\r
244 // \r
a29c47e0 245 this.hashValue = 0;\r
246 }\r
247\r
d2059d05 248 /**\r
249 Return the string array form of key\r
250\r
251 @return String[]\r
252 **/\r
a29c47e0 253 public String[] getKeySet() {\r
254 return keySet;\r
255 }\r
256\r
d2059d05 257 /**\r
258 Return the single string form of key.\r
259\r
260 @return String\r
261 **/\r
a29c47e0 262 public String toString() {\r
263 if (this.keyString == null) {\r
264 StringBuffer keyStringBuf = new StringBuffer(64);\r
a29c47e0 265\r
d2059d05 266 keyStringBuf.append(this.keySet[0]);\r
4a6a5026 267 for (int i = 1; i < ToolChainKey.keyLength; ++i) {\r
a29c47e0 268 keyStringBuf.append(this.delimiter);\r
269 keyStringBuf.append(this.keySet[i]);\r
270 }\r
271\r
272 this.keyString = keyStringBuf.toString();\r
273 }\r
274\r
275 return this.keyString;\r
276 }\r
277}\r
278\r