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