Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / TypeVector.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.core;
12
13 import net.sourceforge.phpdt.core.IType;
14
15 public final class TypeVector {
16         static int INITIAL_SIZE = 10;
17
18         public int size;
19
20         int maxSize;
21
22         IType[] elements;
23
24         public final static IType[] NoElements = new IType[0];
25
26         public TypeVector() {
27                 maxSize = INITIAL_SIZE;
28                 size = 0;
29                 elements = new IType[maxSize];
30         }
31
32         public TypeVector(IType[] types) {
33                 this.size = types.length;
34                 this.maxSize = this.size + 1; // when an element is added, it assumes
35                                                                                 // that the length is > 0
36                 elements = new IType[this.maxSize];
37                 System.arraycopy(types, 0, elements, 0, this.size);
38         }
39
40         public TypeVector(IType type) {
41                 this.maxSize = INITIAL_SIZE;
42                 this.size = 1;
43                 elements = new IType[this.maxSize];
44                 elements[0] = type;
45         }
46
47         public void add(IType newElement) {
48                 if (size == maxSize) // knows that size starts <= maxSize
49                         System.arraycopy(elements, 0, (elements = new IType[maxSize *= 2]),
50                                         0, size);
51                 elements[size++] = newElement;
52         }
53
54         public void addAll(IType[] newElements) {
55                 if (size + newElements.length >= maxSize) {
56                         maxSize = size + newElements.length; // assume no more elements
57                                                                                                         // will be added
58                         System.arraycopy(elements, 0, (elements = new IType[maxSize]), 0,
59                                         size);
60                 }
61                 System.arraycopy(newElements, 0, elements, size, newElements.length);
62                 size += newElements.length;
63         }
64
65         public boolean contains(IType element) {
66                 for (int i = size; --i >= 0;)
67                         if (element.equals(elements[i]))
68                                 return true;
69                 return false;
70         }
71
72         public TypeVector copy() {
73                 TypeVector clone = new TypeVector();
74                 int length = this.elements.length;
75                 System.arraycopy(this.elements, 0, clone.elements = new IType[length],
76                                 0, length);
77                 clone.size = this.size;
78                 clone.maxSize = this.maxSize;
79                 return clone;
80         }
81
82         public IType elementAt(int index) {
83                 return elements[index];
84         }
85
86         public IType[] elements() {
87
88                 // do not resize to 0 if empty since may add more elements later
89                 if (this.size == 0)
90                         return NoElements;
91
92                 if (this.size < this.maxSize) {
93                         maxSize = size;
94                         System.arraycopy(this.elements, 0,
95                                         (this.elements = new IType[maxSize]), 0, size);
96                 }
97                 return this.elements;
98         }
99
100         public IType find(IType element) {
101                 for (int i = size; --i >= 0;)
102                         if (element == elements[i])
103                                 return elements[i];
104                 return null;
105         }
106
107         public IType remove(IType element) {
108                 // assumes only one occurrence of the element exists
109                 for (int i = size; --i >= 0;)
110                         if (element == elements[i]) {
111                                 // shift the remaining elements down one spot
112                                 System.arraycopy(elements, i + 1, elements, i, --size - i);
113                                 elements[size] = null;
114                                 return element;
115                         }
116                 return null;
117         }
118
119         public void removeAll() {
120                 for (int i = size; --i >= 0;)
121                         elements[i] = null;
122                 size = 0;
123         }
124
125         public String toString() {
126                 StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
127                 for (int i = 0; i < size; i++) {
128                         buffer.append("\n"); //$NON-NLS-1$
129                         buffer.append(elements[i]);
130                 }
131                 buffer.append("\n]"); //$NON-NLS-1$
132                 return buffer.toString();
133         }
134 }