KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > QualifiedNameSet


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.builder;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14
15 public class QualifiedNameSet {
16
17 // to avoid using Enumerations, walk the individual values skipping nulls
18
public char[][][] qualifiedNames;
19 public int elementSize; // number of elements in the table
20
public int threshold;
21
22 public QualifiedNameSet(int size) {
23     this.elementSize = 0;
24     this.threshold = size; // size represents the expected number of elements
25
int extraRoom = (int) (size * 1.5f);
26     if (this.threshold == extraRoom)
27         extraRoom++;
28     this.qualifiedNames = new char[extraRoom][][];
29 }
30
31 public char[][] add(char[][] qualifiedName) {
32     int qLength = qualifiedName.length;
33     if (qLength == 0) return CharOperation.NO_CHAR_CHAR;
34
35     int length = qualifiedNames.length;
36     int index = CharOperation.hashCode(qualifiedName[qLength - 1]) % length;
37     char[][] current;
38     while ((current = qualifiedNames[index]) != null) {
39         if (CharOperation.equals(current, qualifiedName)) return current;
40         if (++index == length) index = 0;
41     }
42     qualifiedNames[index] = qualifiedName;
43
44     // assumes the threshold is never equal to the size of the table
45
if (++elementSize > threshold) rehash();
46     return qualifiedName;
47 }
48
49 private void rehash() {
50     QualifiedNameSet newSet = new QualifiedNameSet(elementSize * 2); // double the number of expected elements
51
char[][] current;
52     for (int i = qualifiedNames.length; --i >= 0;)
53         if ((current = qualifiedNames[i]) != null)
54             newSet.add(current);
55
56     this.qualifiedNames = newSet.qualifiedNames;
57     this.elementSize = newSet.elementSize;
58     this.threshold = newSet.threshold;
59 }
60
61 public String JavaDoc toString() {
62     String JavaDoc s = ""; //$NON-NLS-1$
63
char[][] qualifiedName;
64     for (int i = 0, l = qualifiedNames.length; i < l; i++)
65         if ((qualifiedName = qualifiedNames[i]) != null)
66             s += CharOperation.toString(qualifiedName) + "\n"; //$NON-NLS-1$
67
return s;
68 }
69 }
70
Popular Tags