KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > util > HashtableOfObject


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.util;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14
15 /**
16  * Hashtable of {char[] --> Object }
17  */

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