KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

52 public boolean containsKey(Object JavaDoc key) {
53     int index = hashCode(key), length = this.keyTable.length;
54     while (this.keyTable[index] != null) {
55         if (this.keyTable[index] == key)
56             return true;
57         if (++index == length) {
58             index = 0;
59         }
60     }
61     return false;
62 }
63 /** Gets the object associated with the specified key in the
64  * hashtable.
65  * @param key <CODE>char[]</CODE> the specified key
66  * @return int the element for the key or -1 if the key is not
67  * defined in the hash table.
68  */

69 public int get(Object JavaDoc key) {
70     int index = hashCode(key), length = this.keyTable.length;
71     while (this.keyTable[index] != null) {
72         if (this.keyTable[index] == key)
73             return this.valueTable[index];
74         if (++index == length) {
75             index = 0;
76         }
77     }
78     return -1;
79 }
80 /**
81  * Return the hashcode for the key parameter
82  *
83  * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding
84  * @return int
85  */

86 public int hashCode(Object JavaDoc key) {
87     return (key.hashCode() & 0x7FFFFFFF) % this.keyTable.length;
88 }
89 /**
90  * Puts the specified element into the hashtable, using the specified
91  * key. The element may be retrieved by doing a get() with the same key.
92  * The key and the element cannot be null.
93  *
94  * @param key <CODE>Object</CODE> the specified key in the hashtable
95  * @param value <CODE>int</CODE> the specified element
96  * @return int the old value of the key, or -1 if it did not have one.
97  */

98 public int put(Object JavaDoc key, int value) {
99     int index = hashCode(key), length = this.keyTable.length;
100     while (this.keyTable[index] != null) {
101         if (this.keyTable[index] == key)
102             return this.valueTable[index] = value;
103         if (++index == length) {
104             index = 0;
105         }
106     }
107     this.keyTable[index] = key;
108     this.valueTable[index] = value;
109
110     // assumes the threshold is never equal to the size of the table
111
if (++this.elementSize > this.threshold)
112         rehash();
113     return value;
114 }
115 /**
116  * Rehashes the content of the table into a bigger table.
117  * This method is called automatically when the hashtable's
118  * size exceeds the threshold.
119  */

120 private void rehash() {
121     ObjectCache newHashtable = new ObjectCache(this.keyTable.length * 2);
122     for (int i = this.keyTable.length; --i >= 0;)
123         if (this.keyTable[i] != null)
124             newHashtable.put(this.keyTable[i], this.valueTable[i]);
125
126     this.keyTable = newHashtable.keyTable;
127     this.valueTable = newHashtable.valueTable;
128     this.threshold = newHashtable.threshold;
129 }
130 /**
131  * Returns the number of elements contained in the hashtable.
132  *
133  * @return <CODE>int</CODE> The size of the table
134  */

135 public int size() {
136     return this.elementSize;
137 }
138 /**
139  * Converts to a rather lengthy String.
140  *
141  * @return String the ascii representation of the receiver
142  */

143 public String JavaDoc toString() {
144     int max = size();
145     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
146     buf.append("{"); //$NON-NLS-1$
147
for (int i = 0; i < max; ++i) {
148         if (this.keyTable[i] != null) {
149             buf.append(this.keyTable[i]).append("->").append(this.valueTable[i]); //$NON-NLS-1$
150
}
151         if (i < max) {
152             buf.append(", "); //$NON-NLS-1$
153
}
154     }
155     buf.append("}"); //$NON-NLS-1$
156
return buf.toString();
157 }
158 }
159
Popular Tags