KickJava   Java API By Example, From Geeks To Geeks.

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


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