KickJava   Java API By Example, From Geeks To Geeks.

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


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 public final class SimpleNameVector {
16     
17     static int INITIAL_SIZE = 10;
18
19     public int size;
20     int maxSize;
21     char[][] elements;
22     
23     public SimpleNameVector() {
24         
25         this.maxSize = INITIAL_SIZE;
26         this.size = 0;
27         this.elements = new char[this.maxSize][];
28     }
29     
30     public void add(char[] newElement) {
31         
32         if (this.size == this.maxSize) // knows that size starts <= maxSize
33
System.arraycopy(this.elements, 0, (this.elements = new char[this.maxSize *= 2][]), 0, this.size);
34         this.elements[size++] = newElement;
35     }
36     
37     public void addAll(char[][] newElements) {
38         
39         if (this.size + newElements.length >= this.maxSize) {
40             this.maxSize = this.size + newElements.length; // assume no more elements will be added
41
System.arraycopy(this.elements, 0, (this.elements = new char[this.maxSize][]), 0, this.size);
42         }
43         System.arraycopy(newElements, 0, this.elements, this.size, newElements.length);
44         this.size += newElements.length;
45     }
46     
47     public void copyInto(Object JavaDoc[] targetArray){
48         
49         System.arraycopy(this.elements, 0, targetArray, 0, this.size);
50     }
51     
52     public boolean contains(char[] element) {
53         
54         for (int i = this.size; --i >= 0;)
55             if (CharOperation.equals(element, this.elements[i]))
56                 return true;
57         return false;
58     }
59     
60     public char[] elementAt(int index) {
61         return this.elements[index];
62     }
63     
64     public char[] remove(char[] element) {
65         
66         // assumes only one occurrence of the element exists
67
for (int i = this.size; --i >= 0;)
68             if (element == this.elements[i]) {
69                 // shift the remaining elements down one spot
70
System.arraycopy(this.elements, i + 1, this.elements, i, --this.size - i);
71                 this.elements[this.size] = null;
72                 return element;
73             }
74         return null;
75     }
76     
77     public void removeAll() {
78         
79         for (int i = this.size; --i >= 0;)
80             this.elements[i] = null;
81         this.size = 0;
82     }
83     
84     public int size(){
85         
86         return this.size;
87     }
88     
89     public String JavaDoc toString() {
90         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
91         for (int i = 0; i < this.size; i++) {
92             buffer.append(this.elements[i]).append("\n"); //$NON-NLS-1$
93
}
94         return buffer.toString();
95     }
96 }
97
Popular Tags