1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
13 public final class ObjectVector {
15 static int INITIAL_SIZE = 10;
21 public ObjectVector() {
23 this.maxSize = INITIAL_SIZE;
25 this.elements = new Object[this.maxSize];
28 public void add(Object newElement) {
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;
35 public void addAll(Object[] newElements) {
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);
41 System.arraycopy(newElements, 0, this.elements, size, newElements.length);
42 this.size += newElements.length;
45 public void addAll(ObjectVector newVector) {
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);
51 System.arraycopy(newVector.elements, 0, this.elements, size, newVector.size);
52 this.size += newVector.size;
58 public boolean containsIdentical(Object element) {
60 for (int i = this.size; --i >= 0;)
61 if (element == this.elements[i])
69 public boolean contains(Object element) {
71 for (int i = this.size; --i >= 0;)
72 if (element.equals(this.elements[i]))
77 public void copyInto(Object[] targetArray){
79 this.copyInto(targetArray, 0);
82 public void copyInto(Object[] targetArray, int index){
84 System.arraycopy(this.elements, 0, targetArray, index, this.size);
87 public Object elementAt(int index) {
89 return this.elements[index];
92 public Object find(Object element) {
94 for (int i = this.size; --i >= 0;)
95 if (element.equals(this.elements[i]))
100 public Object remove(Object element) {
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;
113 public void removeAll() {
115 for (int i = this.size; --i >= 0;)
116 this.elements[i] = null;
125 public String toString() {
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$