A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / SimpleNameVector.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
12
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14
15 public final class SimpleNameVector {
16
17         static int INITIAL_SIZE = 10;
18
19         public int size;
20
21         int maxSize;
22
23         char[][] elements;
24
25         public SimpleNameVector() {
26
27                 this.maxSize = INITIAL_SIZE;
28                 this.size = 0;
29                 this.elements = new char[this.maxSize][];
30         }
31
32         public void add(char[] newElement) {
33
34                 if (this.size == this.maxSize) // knows that size starts <= maxSize
35                         System.arraycopy(this.elements, 0,
36                                         (this.elements = new char[this.maxSize *= 2][]), 0,
37                                         this.size);
38                 this.elements[size++] = newElement;
39         }
40
41         public void addAll(char[][] newElements) {
42
43                 if (this.size + newElements.length >= this.maxSize) {
44                         this.maxSize = this.size + newElements.length; // assume no more
45                                                                                                                         // elements will be
46                                                                                                                         // added
47                         System.arraycopy(this.elements, 0,
48                                         (this.elements = new char[this.maxSize][]), 0, this.size);
49                 }
50                 System.arraycopy(newElements, 0, this.elements, this.size,
51                                 newElements.length);
52                 this.size += newElements.length;
53         }
54
55         public void copyInto(Object[] targetArray) {
56
57                 System.arraycopy(this.elements, 0, targetArray, 0, this.size);
58         }
59
60         public boolean contains(char[] element) {
61
62                 for (int i = this.size; --i >= 0;)
63                         if (CharOperation.equals(element, this.elements[i]))
64                                 return true;
65                 return false;
66         }
67
68         public char[] elementAt(int index) {
69                 return this.elements[index];
70         }
71
72         public char[] remove(char[] element) {
73
74                 // assumes only one occurrence of the element exists
75                 for (int i = this.size; --i >= 0;)
76                         if (element == this.elements[i]) {
77                                 // shift the remaining elements down one spot
78                                 System.arraycopy(this.elements, i + 1, this.elements, i,
79                                                 --this.size - i);
80                                 this.elements[this.size] = null;
81                                 return element;
82                         }
83                 return null;
84         }
85
86         public void removeAll() {
87
88                 for (int i = this.size; --i >= 0;)
89                         this.elements[i] = null;
90                 this.size = 0;
91         }
92
93         public int size() {
94
95                 return this.size;
96         }
97
98         public String toString() {
99                 StringBuffer buffer = new StringBuffer();
100                 for (int i = 0; i < this.size; i++) {
101                         buffer.append(this.elements[i]).append("\n"); //$NON-NLS-1$
102                 }
103                 return buffer.toString();
104         }
105 }