]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/toolchain/ToolChainKey.java
f6e1e067f898e6e47b98d5020c15e7f2e8a1ed4d
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / toolchain / ToolChainKey.java
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 --*/
13
14 package org.tianocore.build.toolchain;
15
16 import org.tianocore.exception.EdkException;
17
18 public class ToolChainKey implements java.io.Serializable, Comparable<ToolChainKey> {
19 static final long serialVersionUID = -8034897190740066933L;
20 private String delimiter = "_";
21
22 public final static int keyLength = 5;
23
24 private String[] keySet = null;
25
26 private String keyString = null;
27
28 private int hashValue = 0;
29
30 public ToolChainKey(String keyString, String delimiter) throws Exception {
31 setKey(keyString, delimiter);
32 }
33
34 public ToolChainKey(String keyString) throws EdkException {
35 setKey(keyString);
36 }
37
38 public ToolChainKey(String[] keySet) throws EdkException {
39 setKey(keySet);
40 }
41
42 public int hashCode() {
43 if (hashValue != 0) {
44 return hashValue;
45 }
46
47 for (int i = 0; i < keySet.length; ++i) {
48 char[] keyStringValue = new char[keySet[i].length()];
49 this.keySet[i].getChars(0, keyStringValue.length, keyStringValue, 0);
50
51 for (int j = 0; j < keyStringValue.length; ++j) {
52 hashValue = keyStringValue[j] + hashValue * 31;
53 }
54 }
55
56 return hashValue;
57 }
58
59 public int compareTo(ToolChainKey dstKey) {
60 String[] dstKeySet = dstKey.getKeySet();
61 int result = 0;
62 for (int i = 0; i < this.keyLength; ++i) {
63 result = this.keySet[i].compareToIgnoreCase(dstKeySet[i]);
64 if (result != 0) {
65 break;
66 }
67 }
68
69 return result;
70 }
71
72 public boolean equals(Object o) {
73 ToolChainKey dstKey = (ToolChainKey)o;
74 String[] dstKeySet = dstKey.getKeySet();
75
76 if (this == dstKey) {
77 return true;
78 }
79
80 if (dstKeySet.length != this.keyLength) {
81 return false;
82 }
83
84 for (int i = 0; i < this.keyLength; ++i) {
85 if (!this.keySet[i].equalsIgnoreCase(dstKeySet[i])) {
86 return false;
87 }
88 }
89
90 return true;
91 }
92
93 public void setKey(String[] keySet) throws EdkException {
94 if (keySet.length != this.keyLength) {
95 throw new EdkException("Invalid ToolChain key");
96 }
97
98 this.keySet = new String[this.keyLength];
99 System.arraycopy(keySet, 0, this.keySet, 0, this.keyLength);
100 for (int i = 0; i < this.keyLength; ++i) {
101 if (this.keySet[i] == null || this.keySet[i].length() == 0) {
102 this.keySet[i] = "*";
103 }
104 }
105 this.keyString = null;
106 this.hashValue = 0;
107 }
108
109 public void setKey(String keySetString, int index) throws EdkException {
110 if (index >= this.keyLength) {
111 throw new EdkException("Invalid ToolChain key index");
112 }
113
114 if (keySetString == null || keySetString.length() == 0) {
115 keySetString = "*";
116 }
117 this.keySet[index] = keySetString;
118 this.keyString = null;
119 this.hashValue = 0;
120 }
121
122 public void setKey(String keyString) throws EdkException {
123 this.keySet = keyString.split(this.delimiter);
124
125 if (this.keySet.length != this.keyLength) {
126 throw new EdkException("Invalid ToolChain key");
127 }
128
129 this.keyString = keyString;
130 this.hashValue = 0;
131 }
132
133 public void setKey(String keyString, String delimiter) throws Exception {
134 this.keySet = keyString.split(delimiter);
135
136 if (this.keySet.length != this.keyLength) {
137 throw new Exception("Invalid ToolChain key");
138 }
139
140 this.keyString = keyString;
141 this.delimiter = delimiter;
142 this.hashValue = 0;
143 }
144
145 public String[] getKeySet() {
146 return keySet;
147 }
148
149 public String toString() {
150 if (this.keyString == null) {
151 StringBuffer keyStringBuf = new StringBuffer(64);
152 int i = 0;
153
154 keyStringBuf.append(this.keySet[i++]);
155 for (; i < this.keyLength; ++i) {
156 keyStringBuf.append(this.delimiter);
157 keyStringBuf.append(this.keySet[i]);
158 }
159
160 this.keyString = keyStringBuf.toString();
161 }
162
163 return this.keyString;
164 }
165 }
166