summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/toolchain/ToolChainKey.java
blob: 1bedf3c6854e32b3e8c54e5269023a154c5adbf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/** @file
ToolChainKey class

ToolChainKey class is representing the "name" part of tool chain definition.

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

package org.tianocore.build.toolchain;

import org.tianocore.build.exception.GenBuildException;

/**
   ToolChainKey class is the java class form of the "name" of tool chain definition.
   It's primarily for the key of a Map data structure.
 **/
public class ToolChainKey implements java.io.Serializable, Comparable<ToolChainKey> {
    static final long serialVersionUID = -8034897190740066933L;

    ///
    /// The part number of key. Currently we only support fixed five parts.
    /// 
    public final static int keyLength = 5;

    //
    // Default delimiter which is used for concatenating the parts of key
    // 
    private String delimiter = "_";

    //
    // Key value in string array form
    // 
    private String[] keySet = null;

    //
    // Key value in one string form
    // 
    private String keyString = null;

    //
    // Key hash value used for hash table 
    // 
    private int hashValue = 0;

    /**
       Public constructor which can override default delimiter.

       @param keyString     The key string value
       @param delimiter     Delimiter charater concatenating the key parts
     **/
    public ToolChainKey(String keyString, String delimiter) throws GenBuildException {
        setKey(keyString, delimiter);
    }

    /**
       Public constructor which uses default delimiter.

       @param keyString     The key string value
     **/
    public ToolChainKey(String keyString) throws GenBuildException {
        setKey(keyString);
    }

    /**
       Public constructor which doesn't use any delimiter.

       @param keySet
     **/
    public ToolChainKey(String[] keySet) throws GenBuildException {
        setKey(keySet);
    }

    /**
       Calculate hash value of the key string (without the delimiter). It's used
       for Hash Table kind of Map.

       @return int      The hash value
     **/
    public int hashCode() {
        if (hashValue != 0) {
            return hashValue;
        }

        for (int i = 0; i < keySet.length; ++i) {
            char[] keyStringValue = new char[keySet[i].length()];
            this.keySet[i].getChars(0, keyStringValue.length, keyStringValue, 0);

            for (int j = 0; j < keyStringValue.length; ++j) {
                hashValue = keyStringValue[j] + hashValue * 31;
            }
        }

        return hashValue;
    }

    /**
       Compare the string value of two keys . It's used for Tree kind of Map.
       
       @param dstKey    Another key to compare to.
       
       @retval  0       Two keys are equal
       @retval  >0      This key is after the given key
       @retval  <0      This key is before the given key
     **/
    public int compareTo(ToolChainKey dstKey) {
        String[] dstKeySet = dstKey.getKeySet();
        int result = 0;
        for (int i = 0; i < ToolChainKey.keyLength; ++i) {
            result = this.keySet[i].compareToIgnoreCase(dstKeySet[i]);
            if (result != 0) {
                break;
            }
        }

        return result;
    }

    /**
       Check if this key is the same as the given key.

       @param o     Another key to compare to
       
       @return boolean
     **/
    public boolean equals(Object o) {
        ToolChainKey dstKey = (ToolChainKey)o;
        String[] dstKeySet = dstKey.getKeySet();

        if (this == dstKey) {
            return true;
        }

        if (dstKeySet.length != ToolChainKey.keyLength) {
            return false;
        }

        for (int i = 0; i < ToolChainKey.keyLength; ++i) {
            if (!this.keySet[i].equalsIgnoreCase(dstKeySet[i])) {
                return false;
            }
        }

        return true;
    }

    /**
       Set the key value in form of string array.

       @param keySet    The string array of key value
     **/
    public void setKey(String[] keySet) throws GenBuildException {
        if (keySet.length != ToolChainKey.keyLength) {
            throw new GenBuildException("Invalid ToolChain key");
        }

        //
        // Clone the string array because we don't want to change original one
        // 
        this.keySet = new String[ToolChainKey.keyLength];
        System.arraycopy(keySet, 0, this.keySet, 0, ToolChainKey.keyLength);
        for (int i = 0; i < ToolChainKey.keyLength; ++i) {
            if (this.keySet[i] == null || this.keySet[i].length() == 0) {
                this.keySet[i] = "*";
            }
        }

        //
        // We need to re-generate the single key string and hash value.
        // 
        this.keyString = null;
        this.hashValue = 0;
    }

    /**
       Set key value at the specified key part .
       
       @param keySetString      The new value of "index" part of key
       @param index             The key part index
     **/
    public void setKey(String keySetString, int index) throws GenBuildException {
        if (index >= ToolChainKey.keyLength) {
            throw new GenBuildException("Invalid ToolChain key index");
        }

        //
        // Allow wildcard in key string
        // 
        if (keySetString == null || keySetString.length() == 0) {
            keySetString = "*";
        }
        this.keySet[index] = keySetString;

        //
        // We need to re-generate the single key string and hash value.
        // 
        this.keyString = null;
        this.hashValue = 0;
    }

    /**
       Set key value in the form of single string.
       
       @param keyString     The key value string
     **/
    public void setKey(String keyString) throws GenBuildException {
        this.keySet = keyString.split(this.delimiter);

        if (this.keySet.length != ToolChainKey.keyLength) {
            throw new GenBuildException("Invalid ToolChain key");
        }

        this.keyString = keyString;
        //
        // We need to re-generate hash value.
        // 
        this.hashValue = 0;
    }

    /**
       Set key value in the form of single string with specified delimiter.
       
       @param keyString     The key value string
       @param delimiter     The delimiter concatenating the key string
     **/
    public void setKey(String keyString, String delimiter) throws GenBuildException {
        this.keySet = keyString.split(delimiter);

        if (this.keySet.length != ToolChainKey.keyLength) {
            throw new GenBuildException("Invalid ToolChain key");
        }

        this.keyString = keyString;
        this.delimiter = delimiter;
        //
        // We need to re-generate hash value.
        // 
        this.hashValue = 0;
    }

    /**
       Return the string array form of key

       @return String[]
     **/
    public String[] getKeySet() {
        return keySet;
    }

    /**
       Return the single string form of key.

       @return String
     **/
    public String toString() {
        if (this.keyString == null) {
            StringBuffer keyStringBuf = new StringBuffer(64);

            keyStringBuf.append(this.keySet[0]);
            for (int i = 1; i < ToolChainKey.keyLength; ++i) {
                keyStringBuf.append(this.delimiter);
                keyStringBuf.append(this.keySet[i]);
            }

            this.keyString = keyStringBuf.toString();
        }

        return this.keyString;
    }
}