KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class IntegerCache {
14     public int keyTable[];
15     public int valueTable[];
16     int elementSize;
17     int threshold;
18 /**
19  * Constructs a new, empty hashtable. A default capacity and
20  * load factor is used. Note that the hashtable will automatically
21  * grow when it gets full.
22  */

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

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

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

53 public boolean containsKey(int key) {
54     int index = hash(key), length = this.keyTable.length;
55     while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) &&(this.valueTable[index] != 0))) {
56         if (this.keyTable[index] == key)
57             return true;
58         if (++index == length) {
59             index = 0;
60         }
61     }
62     return false;
63 }
64 /**
65  * Return a hashcode for the value of the key parameter.
66  * @param key int
67  * @return int the hash code corresponding to the key value
68  */

69 public int hash(int key) {
70     return (key & 0x7FFFFFFF) % this.keyTable.length;
71 }
72 /**
73  * Puts the specified element into the hashtable, using the specified
74  * key. The element may be retrieved by doing a get() with the same key.
75  *
76  * @param key <CODE>int</CODE> the specified key in the hashtable
77  * @param value <CODE>int</CODE> the specified element
78  * @return int value
79  */

80 public int put(int key, int value) {
81     int index = hash(key), length = this.keyTable.length;
82     while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) && (this.valueTable[index] != 0))) {
83         if (this.keyTable[index] == key)
84             return this.valueTable[index] = value;
85         if (++index == length) {
86             index = 0;
87         }
88     }
89     this.keyTable[index] = key;
90     this.valueTable[index] = value;
91
92     // assumes the threshold is never equal to the size of the table
93
if (++this.elementSize > this.threshold) {
94         rehash();
95     }
96     return value;
97 }
98 /**
99  * Puts the specified element into the hashtable if absent, using the specified
100  * key. The element may be retrieved by doing a get() with the same key.
101  *
102  * @param key <CODE>int</CODE> the specified key in the hashtable
103  * @param value <CODE>int</CODE> the specified element
104  * @return int value
105  */

106 public int putIfAbsent(int key, int value) {
107     int index = hash(key), length = this.keyTable.length;
108     while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) && (this.valueTable[index] != 0))) {
109         if (this.keyTable[index] == key)
110             return this.valueTable[index];
111         if (++index == length) {
112             index = 0;
113         }
114     }
115     this.keyTable[index] = key;
116     this.valueTable[index] = value;
117
118     // assumes the threshold is never equal to the size of the table
119
if (++this.elementSize > this.threshold) {
120         rehash();
121     }
122     return -value; // negative when added, assumes value is > 0
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     IntegerCache newHashtable = new IntegerCache(this.keyTable.length * 2);
131     for (int i = this.keyTable.length; --i >= 0;) {
132         int key = this.keyTable[i];
133         int value = this.valueTable[i];
134         if ((key != 0) || ((key == 0) && (value != 0))) {
135             newHashtable.put(key, value);
136         }
137     }
138     this.keyTable = newHashtable.keyTable;
139     this.valueTable = newHashtable.valueTable;
140     this.threshold = newHashtable.threshold;
141 }
142 /**
143  * Returns the number of elements contained in the hashtable.
144  *
145  * @return <CODE>int</CODE> The size of the table
146  */

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

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