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