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