KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > codegen > FieldNameAndTypeCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.codegen;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
15
16 public class FieldNameAndTypeCache {
17     public FieldBinding keyTable[];
18     public int valueTable[];
19     int elementSize;
20     int threshold;
21 /**
22  * Constructs a new, empty hashtable. A default capacity is used.
23  * Note that the hashtable will automatically grow when it gets full.
24  */

25 public FieldNameAndTypeCache() {
26     this(13);
27 }
28 /**
29  * Constructs a new, empty hashtable with the specified initial
30  * capacity.
31  * @param initialCapacity int
32  * the initial number of buckets
33  */

34 public FieldNameAndTypeCache(int initialCapacity) {
35     this.elementSize = 0;
36     this.threshold = (int) (initialCapacity * 0.66f);
37     this.keyTable = new FieldBinding[initialCapacity];
38     this.valueTable = new int[initialCapacity];
39 }
40 /**
41  * Clears the hash table so that it has no more elements in it.
42  */

43 public void clear() {
44     for (int i = keyTable.length; --i >= 0;) {
45         keyTable[i] = null;
46         valueTable[i] = 0;
47     }
48     elementSize = 0;
49 }
50 /** Returns true if the collection contains an element for the key.
51  *
52  * @param key char[] the key that we are looking for
53  * @return boolean
54  */

55 public boolean containsKey(FieldBinding key) {
56     int index = hashCode(key), length = keyTable.length;
57     while (keyTable[index] != null) {
58         if (equalsForNameAndType(keyTable[index], key))
59             return true;
60         if (++index == length) {
61             index = 0;
62         }
63     }
64     return false;
65 }
66 /**
67  * Return true if the two field binding are consider like equals.
68  */

69 public boolean equalsForNameAndType(FieldBinding field1, FieldBinding field2) {
70     return ((field1.type == field2.type) && CharOperation.equals(field1.name, field2.name));
71 }
72 /** Gets the object associated with the specified key in the
73  * hashtable.
74  * @param key <CODE>char[]</CODE> the specified key
75  * @return int the element for the key or -1 if the key is not
76  * defined in the hash table.
77  */

78 public int get(FieldBinding key) {
79     int index = hashCode(key), length = keyTable.length;
80     while (keyTable[index] != null) {
81         if (equalsForNameAndType(keyTable[index], key))
82             return valueTable[index];
83         if (++index == length) {
84             index = 0;
85         }
86     }
87     return -1;
88 }
89 /**
90  * Return the hashcode for the key parameter
91  *
92  * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding
93  * @return int
94  */

95 public int hashCode(FieldBinding key) {
96     return ((CharOperation.hashCode(key.name) + key.type.hashCode()) & 0x7FFFFFFF) % keyTable.length;
97 }
98 /**
99  * Puts the specified element into the hashtable, using the specified
100  * key. The element may be retrieved by doing a get() with the same key.
101  * The key and the element cannot be null.
102  *
103  * @param key <CODE>Object</CODE> the specified key in the hashtable
104  * @param value <CODE>int</CODE> the specified element
105  * @return int the old value of the key, or -1 if it did not have one.
106  */

107 public int put(FieldBinding key, int value) {
108     int index = hashCode(key), length = keyTable.length;
109     while (keyTable[index] != null) {
110         if (equalsForNameAndType(keyTable[index], key))
111             return valueTable[index] = value;
112         if (++index == length) {
113             index = 0;
114         }
115     }
116     keyTable[index] = key;
117     valueTable[index] = value;
118
119     // assumes the threshold is never equal to the size of the table
120
if (++elementSize > threshold)
121         rehash();
122     return value;
123 }
124 /**
125  * Rehashes the content of the table into a bigger table.
126  * This method is called automatically when the hashtable's
127  * size exceeds the threshold.
128  */

129 private void rehash() {
130     FieldNameAndTypeCache newHashtable = new FieldNameAndTypeCache(keyTable.length * 2);
131     for (int i = keyTable.length; --i >= 0;)
132         if (keyTable[i] != null)
133             newHashtable.put(keyTable[i], valueTable[i]);
134
135     this.keyTable = newHashtable.keyTable;
136     this.valueTable = newHashtable.valueTable;
137     this.threshold = newHashtable.threshold;
138 }
139 /**
140  * Returns the number of elements contained in the hashtable.
141  *
142  * @return <CODE>int</CODE> The size of the table
143  */

144 public int size() {
145     return elementSize;
146 }
147 /**
148  * Converts to a rather lengthy String.
149  *
150  * @return String the ascii representation of the receiver
151  */

152 public String JavaDoc toString() {
153     int max = size();
154     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
155     buf.append("{"); //$NON-NLS-1$
156
for (int i = 0; i < max; ++i) {
157         if (keyTable[i] != null) {
158             buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
159
}
160         if (i < max) {
161             buf.append(", "); //$NON-NLS-1$
162
}
163     }
164     buf.append("}"); //$NON-NLS-1$
165
return buf.toString();
166 }
167 }
168
Popular Tags