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 / ObjectVector.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 public final class ObjectVector {
14
15         static int INITIAL_SIZE = 10;
16
17         public int size;
18
19         int maxSize;
20
21         Object[] elements;
22
23         public ObjectVector() {
24
25                 this.maxSize = INITIAL_SIZE;
26                 this.size = 0;
27                 this.elements = new Object[this.maxSize];
28         }
29
30         public void add(Object newElement) {
31
32                 if (this.size == this.maxSize) // knows that size starts <= maxSize
33                         System.arraycopy(this.elements, 0,
34                                         (this.elements = new Object[this.maxSize *= 2]), 0,
35                                         this.size);
36                 this.elements[this.size++] = newElement;
37         }
38
39         public void addAll(Object[] newElements) {
40
41                 if (this.size + newElements.length >= this.maxSize) {
42                         maxSize = this.size + newElements.length; // assume no more
43                                                                                                                 // elements will be
44                                                                                                                 // added
45                         System.arraycopy(this.elements, 0,
46                                         (this.elements = new Object[this.maxSize]), 0, this.size);
47                 }
48                 System.arraycopy(newElements, 0, this.elements, size,
49                                 newElements.length);
50                 this.size += newElements.length;
51         }
52
53         public void addAll(ObjectVector newVector) {
54
55                 if (this.size + newVector.size >= this.maxSize) {
56                         maxSize = this.size + newVector.size; // assume no more elements
57                                                                                                         // will be added
58                         System.arraycopy(this.elements, 0,
59                                         (this.elements = new Object[this.maxSize]), 0, this.size);
60                 }
61                 System.arraycopy(newVector.elements, 0, this.elements, size,
62                                 newVector.size);
63                 this.size += newVector.size;
64         }
65
66         /**
67          * Identity check
68          */
69         public boolean containsIdentical(Object element) {
70
71                 for (int i = this.size; --i >= 0;)
72                         if (element == this.elements[i])
73                                 return true;
74                 return false;
75         }
76
77         /**
78          * Equality check
79          */
80         public boolean contains(Object element) {
81
82                 for (int i = this.size; --i >= 0;)
83                         if (element.equals(this.elements[i]))
84                                 return true;
85                 return false;
86         }
87
88         public void copyInto(Object[] targetArray) {
89
90                 this.copyInto(targetArray, 0);
91         }
92
93         public void copyInto(Object[] targetArray, int index) {
94
95                 System.arraycopy(this.elements, 0, targetArray, index, this.size);
96         }
97
98         public Object elementAt(int index) {
99
100                 return this.elements[index];
101         }
102
103         public Object find(Object element) {
104
105                 for (int i = this.size; --i >= 0;)
106                         if (element.equals(this.elements[i]))
107                                 return element;
108                 return null;
109         }
110
111         public Object remove(Object element) {
112
113                 // assumes only one occurrence of the element exists
114                 for (int i = this.size; --i >= 0;)
115                         if (element.equals(this.elements[i])) {
116                                 // shift the remaining elements down one spot
117                                 System.arraycopy(this.elements, i + 1, this.elements, i,
118                                                 --this.size - i);
119                                 this.elements[this.size] = null;
120                                 return element;
121                         }
122                 return null;
123         }
124
125         public void removeAll() {
126
127                 for (int i = this.size; --i >= 0;)
128                         this.elements[i] = null;
129                 this.size = 0;
130         }
131
132         public int size() {
133
134                 return this.size;
135         }
136
137         public String toString() {
138
139                 String s = ""; //$NON-NLS-1$
140                 for (int i = 0; i < this.size; i++)
141                         s += this.elements[i].toString() + "\n"; //$NON-NLS-1$
142                 return s;
143         }
144 }