4 ToolChainKey class is representing the "name" part of tool chain definition.
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 org
.tianocore
.build
.exception
.GenBuildException
;
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.
25 public class ToolChainKey
implements java
.io
.Serializable
, Comparable
<ToolChainKey
> {
26 static final long serialVersionUID
= -8034897190740066933L;
29 /// The part number of key. Currently we only support fixed five parts.
31 public final static int keyLength
= 5;
34 // Default delimiter which is used for concatenating the parts of key
36 private String delimiter
= "_";
39 // Key value in string array form
41 private String
[] keySet
= null;
44 // Key value in one string form
46 private String keyString
= null;
49 // Key hash value used for hash table
51 private int hashValue
= 0;
54 Public constructor which can override default delimiter.
56 @param keyString The key string value
57 @param delimiter Delimiter charater concatenating the key parts
59 public ToolChainKey(String keyString
, String delimiter
) throws GenBuildException
{
60 setKey(keyString
, delimiter
);
64 Public constructor which uses default delimiter.
66 @param keyString The key string value
68 public ToolChainKey(String keyString
) throws GenBuildException
{
73 Public constructor which doesn't use any delimiter.
77 public ToolChainKey(String
[] keySet
) throws GenBuildException
{
82 Calculate hash value of the key string (without the delimiter). It's used
83 for Hash Table kind of Map.
85 @return int The hash value
87 public int hashCode() {
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);
96 for (int j
= 0; j
< keyStringValue
.length
; ++j
) {
97 hashValue
= keyStringValue
[j
] + hashValue
* 31;
105 Compare the string value of two keys . It's used for Tree kind of Map.
107 @param dstKey Another key to compare to.
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
113 public int compareTo(ToolChainKey dstKey
) {
114 String
[] dstKeySet
= dstKey
.getKeySet();
116 for (int i
= 0; i
< ToolChainKey
.keyLength
; ++i
) {
117 result
= this.keySet
[i
].compareToIgnoreCase(dstKeySet
[i
]);
127 Check if this key is the same as the given key.
129 @param o Another key to compare to
133 public boolean equals(Object o
) {
134 ToolChainKey dstKey
= (ToolChainKey
)o
;
135 String
[] dstKeySet
= dstKey
.getKeySet();
137 if (this == dstKey
) {
141 if (dstKeySet
.length
!= ToolChainKey
.keyLength
) {
145 for (int i
= 0; i
< ToolChainKey
.keyLength
; ++i
) {
146 if (!this.keySet
[i
].equalsIgnoreCase(dstKeySet
[i
])) {
155 Set the key value in form of string array.
157 @param keySet The string array of key value
159 public void setKey(String
[] keySet
) throws GenBuildException
{
160 if (keySet
.length
!= ToolChainKey
.keyLength
) {
161 throw new GenBuildException("Invalid ToolChain key");
165 // Clone the string array because we don't want to change original one
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
] = "*";
176 // We need to re-generate the single key string and hash value.
178 this.keyString
= null;
183 Set key value at the specified key part .
185 @param keySetString The new value of "index" part of key
186 @param index The key part index
188 public void setKey(String keySetString
, int index
) throws GenBuildException
{
189 if (index
>= ToolChainKey
.keyLength
) {
190 throw new GenBuildException("Invalid ToolChain key index");
194 // Allow wildcard in key string
196 if (keySetString
== null || keySetString
.length() == 0) {
199 this.keySet
[index
] = keySetString
;
202 // We need to re-generate the single key string and hash value.
204 this.keyString
= null;
209 Set key value in the form of single string.
211 @param keyString The key value string
213 public void setKey(String keyString
) throws GenBuildException
{
214 this.keySet
= keyString
.split(this.delimiter
);
216 if (this.keySet
.length
!= ToolChainKey
.keyLength
) {
217 throw new GenBuildException("Invalid ToolChain key");
220 this.keyString
= keyString
;
222 // We need to re-generate hash value.
228 Set key value in the form of single string with specified delimiter.
230 @param keyString The key value string
231 @param delimiter The delimiter concatenating the key string
233 public void setKey(String keyString
, String delimiter
) throws GenBuildException
{
234 this.keySet
= keyString
.split(delimiter
);
236 if (this.keySet
.length
!= ToolChainKey
.keyLength
) {
237 throw new GenBuildException("Invalid ToolChain key");
240 this.keyString
= keyString
;
241 this.delimiter
= delimiter
;
243 // We need to re-generate hash value.
249 Return the string array form of key
253 public String
[] getKeySet() {
258 Return the single string form of key.
262 public String
toString() {
263 if (this.keyString
== null) {
264 StringBuffer keyStringBuf
= new StringBuffer(64);
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
]);
272 this.keyString
= keyStringBuf
.toString();
275 return this.keyString
;