A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / codegen / ObjectCache.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.codegen;
12
13 public class ObjectCache {
14         public Object keyTable[];
15
16         public int valueTable[];
17
18         int elementSize;
19
20         int threshold;
21
22         /**
23          * Constructs a new, empty hashtable. A default capacity is used. Note that
24          * the hashtable will automatically grow when it gets full.
25          */
26         public ObjectCache() {
27                 this(13);
28         }
29
30         /**
31          * Constructs a new, empty hashtable with the specified initial capacity.
32          * 
33          * @param initialCapacity
34          *            int the initial number of buckets
35          */
36         public ObjectCache(int initialCapacity) {
37                 this.elementSize = 0;
38                 this.threshold = (int) (initialCapacity * 0.66f);
39                 this.keyTable = new Object[initialCapacity];
40                 this.valueTable = new int[initialCapacity];
41         }
42
43         /**
44          * Clears the hash table so that it has no more elements in it.
45          */
46         public void clear() {
47                 for (int i = keyTable.length; --i >= 0;) {
48                         keyTable[i] = null;
49                         valueTable[i] = 0;
50                 }
51                 elementSize = 0;
52         }
53
54         /**
55          * Returns true if the collection contains an element for the key.
56          * 
57          * @param char[]
58          *            key the key that we are looking for
59          * @return boolean
60          */
61         public boolean containsKey(Object key) {
62                 int index = hashCode(key);
63                 while (keyTable[index] != null) {
64                         if (keyTable[index] == key)
65                                 return true;
66                         index = (index + 1) % keyTable.length;
67                 }
68                 return false;
69         }
70
71         /**
72          * Gets the object associated with the specified key in the hashtable.
73          * 
74          * @param key
75          *            <CODE>char[]</CODE> the specified key
76          * @return int the element for the key or -1 if the key is not defined in
77          *         the hash table.
78          */
79         public int get(Object key) {
80                 int index = hashCode(key);
81                 while (keyTable[index] != null) {
82                         if (keyTable[index] == key)
83                                 return valueTable[index];
84                         index = (index + 1) % keyTable.length;
85                 }
86                 return -1;
87         }
88
89         /**
90          * Return the hashcode for the key parameter
91          * 
92          * @param key
93          *            net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding
94          * @return int
95          */
96         public int hashCode(Object key) {
97                 return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
98         }
99
100         /**
101          * Puts the specified element into the hashtable, using the specified key.
102          * The element may be retrieved by doing a get() with the same key. The key
103          * and the element cannot be null.
104          * 
105          * @param key
106          *            <CODE>Object</CODE> the specified key in the hashtable
107          * @param value
108          *            <CODE>int</CODE> the specified element
109          * @return int the old value of the key, or -1 if it did not have one.
110          */
111         public int put(Object key, int value) {
112                 int index = hashCode(key);
113                 while (keyTable[index] != null) {
114                         if (keyTable[index] == key)
115                                 return valueTable[index] = value;
116                         index = (index + 1) % keyTable.length;
117                 }
118                 keyTable[index] = key;
119                 valueTable[index] = value;
120
121                 // assumes the threshold is never equal to the size of the table
122                 if (++elementSize > threshold)
123                         rehash();
124                 return value;
125         }
126
127         /**
128          * Rehashes the content of the table into a bigger table. This method is
129          * called automatically when the hashtable's size exceeds the threshold.
130          */
131         private void rehash() {
132                 ObjectCache newHashtable = new ObjectCache(keyTable.length * 2);
133                 for (int i = keyTable.length; --i >= 0;)
134                         if (keyTable[i] != null)
135                                 newHashtable.put(keyTable[i], valueTable[i]);
136
137                 this.keyTable = newHashtable.keyTable;
138                 this.valueTable = newHashtable.valueTable;
139                 this.threshold = newHashtable.threshold;
140         }
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 elementSize;
149         }
150
151         /**
152          * Converts to a rather lengthy String.
153          * 
154          * @return String the ascii representation of the receiver
155          */
156         public String toString() {
157                 int max = size();
158                 StringBuffer buf = new StringBuffer();
159                 buf.append("{"); //$NON-NLS-1$
160                 for (int i = 0; i < max; ++i) {
161                         if (keyTable[i] != null) {
162                                 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
163                         }
164                         if (i < max) {
165                                 buf.append(", "); //$NON-NLS-1$
166                         }
167                 }
168                 buf.append("}"); //$NON-NLS-1$
169                 return buf.toString();
170         }
171 }