KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > HashtableOfArrayToObject


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.core.util;
12
13 /**
14  * Hashtable of {Object[] --> Object }
15  */

16 public final class HashtableOfArrayToObject implements Cloneable JavaDoc {
17     
18     // to avoid using Enumerations, walk the individual tables skipping nulls
19
public Object JavaDoc[][] keyTable;
20     public Object JavaDoc[] valueTable;
21
22     public int elementSize; // number of elements in the table
23
int threshold;
24
25     public HashtableOfArrayToObject() {
26         this(13);
27     }
28
29     public HashtableOfArrayToObject(int size) {
30
31         this.elementSize = 0;
32         this.threshold = size; // size represents the expected number of elements
33
int extraRoom = (int) (size * 1.75f);
34         if (this.threshold == extraRoom)
35             extraRoom++;
36         this.keyTable = new Object JavaDoc[extraRoom][];
37         this.valueTable = new Object JavaDoc[extraRoom];
38     }
39
40     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
41         HashtableOfArrayToObject result = (HashtableOfArrayToObject) super.clone();
42         result.elementSize = this.elementSize;
43         result.threshold = this.threshold;
44
45         int length = this.keyTable.length;
46         result.keyTable = new Object JavaDoc[length][];
47         System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
48
49         length = this.valueTable.length;
50         result.valueTable = new Object JavaDoc[length];
51         System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
52         return result;
53     }
54
55     public boolean containsKey(Object JavaDoc[] key) {
56         int length = this.keyTable.length;
57         int index = hashCode(key) % length;
58         int keyLength = key.length;
59         Object JavaDoc[] currentKey;
60         while ((currentKey = this.keyTable[index]) != null) {
61             if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key))
62                 return true;
63             if (++index == length) {
64                 index = 0;
65             }
66         }
67         return false;
68     }
69
70     public Object JavaDoc get(Object JavaDoc[] key) {
71         int length = this.keyTable.length;
72         int index = hashCode(key) % length;
73         int keyLength = key.length;
74         Object JavaDoc[] currentKey;
75         while ((currentKey = this.keyTable[index]) != null) {
76             if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key))
77                 return this.valueTable[index];
78             if (++index == length) {
79                 index = 0;
80             }
81         }
82         return null;
83     }
84
85     public Object JavaDoc[] getKey(Object JavaDoc[] key, int keyLength) {
86         int length = this.keyTable.length;
87         int index = hashCode(key, keyLength) % length;
88         Object JavaDoc[] currentKey;
89         while ((currentKey = this.keyTable[index]) != null) {
90             if (currentKey.length == keyLength && Util.equalArrays(currentKey, key, keyLength))
91                 return currentKey;
92             if (++index == length) {
93                 index = 0;
94             }
95         }
96         return null;
97     }
98
99     private int hashCode(Object JavaDoc[] element) {
100         return hashCode(element, element.length);
101     }
102     
103     private int hashCode(Object JavaDoc[] element, int length) {
104         int hash = 0;
105         for (int i = length-1; i >= 0; i--)
106             hash = Util.combineHashCodes(hash, element[i].hashCode());
107         return hash & 0x7FFFFFFF;
108     }
109     
110     public Object JavaDoc put(Object JavaDoc[] key, Object JavaDoc value) {
111         int length = this.keyTable.length;
112         int index = hashCode(key) % length;
113         int keyLength = key.length;
114         Object JavaDoc[] currentKey;
115         while ((currentKey = this.keyTable[index]) != null) {
116             if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key))
117                 return this.valueTable[index] = value;
118             if (++index == length) {
119                 index = 0;
120             }
121         }
122         this.keyTable[index] = key;
123         this.valueTable[index] = value;
124
125         // assumes the threshold is never equal to the size of the table
126
if (++this.elementSize > threshold)
127             rehash();
128         return value;
129     }
130
131     public Object JavaDoc removeKey(Object JavaDoc[] key) {
132         int length = this.keyTable.length;
133         int index = hashCode(key) % length;
134         int keyLength = key.length;
135         Object JavaDoc[] currentKey;
136         while ((currentKey = this.keyTable[index]) != null) {
137             if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key)) {
138                 Object JavaDoc value = this.valueTable[index];
139                 this.elementSize--;
140                 this.keyTable[index] = null;
141                 this.valueTable[index] = null;
142                 rehash();
143                 return value;
144             }
145             if (++index == length) {
146                 index = 0;
147             }
148         }
149         return null;
150     }
151
152     private void rehash() {
153
154         HashtableOfArrayToObject newHashtable = new HashtableOfArrayToObject(elementSize * 2); // double the number of expected elements
155
Object JavaDoc[] currentKey;
156         for (int i = this.keyTable.length; --i >= 0;)
157             if ((currentKey = this.keyTable[i]) != null)
158                 newHashtable.put(currentKey, this.valueTable[i]);
159
160         this.keyTable = newHashtable.keyTable;
161         this.valueTable = newHashtable.valueTable;
162         this.threshold = newHashtable.threshold;
163     }
164
165     public int size() {
166         return elementSize;
167     }
168
169     public String JavaDoc toString() {
170         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
171         Object JavaDoc[] element;
172         for (int i = 0, length = this.keyTable.length; i < length; i++)
173             if ((element = this.keyTable[i]) != null) {
174                 buffer.append('{');
175                 for (int j = 0, length2 = element.length; j < length2; j++) {
176                     buffer.append(element[j]);
177                     if (j != length2-1)
178                         buffer.append(", "); //$NON-NLS-1$
179
}
180                 buffer.append("} -> "); //$NON-NLS-1$
181
buffer.append(this.valueTable[i]);
182                 if (i != length-1)
183                     buffer.append('\n');
184             }
185         return buffer.toString();
186     }
187 }
188
Popular Tags