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
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;
23 public ObjectVector() {
25 this.maxSize = INITIAL_SIZE;
27 this.elements = new Object[this.maxSize];
30 public void add(Object newElement) {
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,
36 this.elements[this.size++] = newElement;
39 public void addAll(Object[] newElements) {
41 if (this.size + newElements.length >= this.maxSize) {
42 maxSize = this.size + newElements.length; // assume no more
45 System.arraycopy(this.elements, 0,
46 (this.elements = new Object[this.maxSize]), 0, this.size);
48 System.arraycopy(newElements, 0, this.elements, size,
50 this.size += newElements.length;
53 public void addAll(ObjectVector newVector) {
55 if (this.size + newVector.size >= this.maxSize) {
56 maxSize = this.size + newVector.size; // assume no more elements
58 System.arraycopy(this.elements, 0,
59 (this.elements = new Object[this.maxSize]), 0, this.size);
61 System.arraycopy(newVector.elements, 0, this.elements, size,
63 this.size += newVector.size;
69 public boolean containsIdentical(Object element) {
71 for (int i = this.size; --i >= 0;)
72 if (element == this.elements[i])
80 public boolean contains(Object element) {
82 for (int i = this.size; --i >= 0;)
83 if (element.equals(this.elements[i]))
88 public void copyInto(Object[] targetArray) {
90 this.copyInto(targetArray, 0);
93 public void copyInto(Object[] targetArray, int index) {
95 System.arraycopy(this.elements, 0, targetArray, index, this.size);
98 public Object elementAt(int index) {
100 return this.elements[index];
103 public Object find(Object element) {
105 for (int i = this.size; --i >= 0;)
106 if (element.equals(this.elements[i]))
111 public Object remove(Object element) {
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,
119 this.elements[this.size] = null;
125 public void removeAll() {
127 for (int i = this.size; --i >= 0;)
128 this.elements[i] = null;
137 public String toString() {
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$