KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
15
16 public final class HashtableOfPackage {
17     // to avoid using Enumerations, walk the individual tables skipping nulls
18
public char[] keyTable[];
19     public PackageBinding valueTable[];
20
21     public int elementSize; // number of elements in the table
22
int threshold;
23 public HashtableOfPackage() {
24     this(3); // usually not very large
25
}
26 public HashtableOfPackage(int size) {
27     this.elementSize = 0;
28     this.threshold = size; // size represents the expected number of elements
29
int extraRoom = (int) (size * 1.75f);
30     if (this.threshold == extraRoom)
31         extraRoom++;
32     this.keyTable = new char[extraRoom][];
33     this.valueTable = new PackageBinding[extraRoom];
34 }
35 public boolean containsKey(char[] key) {
36     int length = keyTable.length,
37         index = CharOperation.hashCode(key) % length;
38     int keyLength = key.length;
39     char[] currentKey;
40     while ((currentKey = keyTable[index]) != null) {
41         if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
42             return true;
43         if (++index == length) {
44             index = 0;
45         }
46     }
47     return false;
48 }
49 public PackageBinding get(char[] key) {
50     int length = keyTable.length,
51         index = CharOperation.hashCode(key) % length;
52     int keyLength = key.length;
53     char[] currentKey;
54     while ((currentKey = keyTable[index]) != null) {
55         if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
56             return valueTable[index];
57         if (++index == length) {
58             index = 0;
59         }
60     }
61     return null;
62 }
63 public PackageBinding put(char[] key, PackageBinding value) {
64     int length = keyTable.length,
65         index = CharOperation.hashCode(key) % length;
66     int keyLength = key.length;
67     char[] currentKey;
68     while ((currentKey = keyTable[index]) != null) {
69         if (currentKey.length == keyLength && CharOperation.equals(currentKey, key))
70             return valueTable[index] = value;
71         if (++index == length) {
72             index = 0;
73         }
74     }
75     keyTable[index] = key;
76     valueTable[index] = value;
77
78     // assumes the threshold is never equal to the size of the table
79
if (++elementSize > threshold)
80         rehash();
81     return value;
82 }
83 private void rehash() {
84     HashtableOfPackage newHashtable = new HashtableOfPackage(elementSize * 2); // double the number of expected elements
85
char[] currentKey;
86     for (int i = keyTable.length; --i >= 0;)
87         if ((currentKey = keyTable[i]) != null)
88             newHashtable.put(currentKey, valueTable[i]);
89
90     this.keyTable = newHashtable.keyTable;
91     this.valueTable = newHashtable.valueTable;
92     this.threshold = newHashtable.threshold;
93 }
94 public int size() {
95     return elementSize;
96 }
97 public String JavaDoc toString() {
98     String JavaDoc s = ""; //$NON-NLS-1$
99
PackageBinding pkg;
100     for (int i = 0, length = valueTable.length; i < length; i++)
101         if ((pkg = valueTable[i]) != null)
102             s += pkg.toString() + "\n"; //$NON-NLS-1$
103
return s;
104 }
105 }
106
Popular Tags