001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.util;
011:
012: public final class ObjectVector {
013:
014: static int INITIAL_SIZE = 10;
015:
016: public int size;
017: int maxSize;
018: Object[] elements;
019:
020: public ObjectVector() {
021: this (INITIAL_SIZE);
022: }
023:
024: public ObjectVector(int initialSize) {
025: this .maxSize = initialSize > 0 ? initialSize : INITIAL_SIZE;
026: this .size = 0;
027: this .elements = new Object[this .maxSize];
028: }
029:
030: public void add(Object newElement) {
031:
032: if (this .size == this .maxSize) // knows that size starts <= maxSize
033: System.arraycopy(this .elements, 0,
034: (this .elements = new Object[this .maxSize *= 2]), 0,
035: this .size);
036: this .elements[this .size++] = newElement;
037: }
038:
039: public void addAll(Object[] newElements) {
040:
041: if (this .size + newElements.length >= this .maxSize) {
042: maxSize = this .size + newElements.length; // assume no more elements will be added
043: System.arraycopy(this .elements, 0,
044: (this .elements = new Object[this .maxSize]), 0,
045: this .size);
046: }
047: System.arraycopy(newElements, 0, this .elements, size,
048: newElements.length);
049: this .size += newElements.length;
050: }
051:
052: public void addAll(ObjectVector newVector) {
053:
054: if (this .size + newVector.size >= this .maxSize) {
055: maxSize = this .size + newVector.size; // assume no more elements will be added
056: System.arraycopy(this .elements, 0,
057: (this .elements = new Object[this .maxSize]), 0,
058: this .size);
059: }
060: System.arraycopy(newVector.elements, 0, this .elements, size,
061: newVector.size);
062: this .size += newVector.size;
063: }
064:
065: /**
066: * Identity check
067: */
068: public boolean containsIdentical(Object element) {
069:
070: for (int i = this .size; --i >= 0;)
071: if (element == this .elements[i])
072: return true;
073: return false;
074: }
075:
076: /**
077: * Equality check
078: */
079: public boolean contains(Object element) {
080:
081: for (int i = this .size; --i >= 0;)
082: if (element.equals(this .elements[i]))
083: return true;
084: return false;
085: }
086:
087: public void copyInto(Object[] targetArray) {
088:
089: this .copyInto(targetArray, 0);
090: }
091:
092: public void copyInto(Object[] targetArray, int index) {
093:
094: System.arraycopy(this .elements, 0, targetArray, index,
095: this .size);
096: }
097:
098: public Object elementAt(int index) {
099:
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,
118: i, --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: }
|