1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.compiler.util;
15 
16 import java.util.Iterator;
17 
18 public final class ObjectVector implements Iterable<Object> {
19 
20 	static int INITIAL_SIZE = 10;
21 
22 	public int size;
23 	int maxSize;
24 	Object[] elements;
25 
ObjectVector()26 	public ObjectVector() {
27 		this(INITIAL_SIZE);
28 	}
29 
ObjectVector(int initialSize)30 	public ObjectVector(int initialSize) {
31 		this.maxSize = initialSize > 0 ? initialSize : INITIAL_SIZE;
32 		this.size = 0;
33 		this.elements = new Object[this.maxSize];
34 	}
35 
add(Object newElement)36 	public void add(Object newElement) {
37 
38 		if (this.size == this.maxSize) // knows that size starts <= maxSize
39 			System.arraycopy(this.elements, 0, (this.elements = new Object[this.maxSize *= 2]), 0, this.size);
40 		this.elements[this.size++] = newElement;
41 	}
42 
addAll(Object[] newElements)43 	public void addAll(Object[] newElements) {
44 
45 		if (this.size + newElements.length >= this.maxSize) {
46 			this.maxSize = this.size + newElements.length; // assume no more elements will be added
47 			System.arraycopy(this.elements, 0, (this.elements = new Object[this.maxSize]), 0, this.size);
48 		}
49 		System.arraycopy(newElements, 0, this.elements, this.size, newElements.length);
50 		this.size += newElements.length;
51 	}
52 
addAll(ObjectVector newVector)53 	public void addAll(ObjectVector newVector) {
54 
55 		if (this.size + newVector.size >= this.maxSize) {
56 			this.maxSize = this.size + newVector.size; // assume no more elements will be added
57 			System.arraycopy(this.elements, 0, (this.elements = new Object[this.maxSize]), 0, this.size);
58 		}
59 		System.arraycopy(newVector.elements, 0, this.elements, this.size, newVector.size);
60 		this.size += newVector.size;
61 	}
62 
63 	/**
64 	 * Identity check
65 	 */
containsIdentical(Object element)66 	public boolean containsIdentical(Object element) {
67 
68 		for (int i = this.size; --i >= 0;)
69 			if (element == this.elements[i])
70 				return true;
71 		return false;
72 	}
73 
74 	/**
75 	 * Equality check
76 	 */
contains(Object element)77 	public boolean contains(Object element) {
78 
79 		for (int i = this.size; --i >= 0;)
80 			if (element.equals(this.elements[i]))
81 				return true;
82 		return false;
83 	}
84 
copyInto(Object[] targetArray)85 	public void copyInto(Object[] targetArray){
86 
87 		this.copyInto(targetArray, 0);
88 	}
89 
copyInto(Object[] targetArray, int index)90 	public void copyInto(Object[] targetArray, int index){
91 
92 		System.arraycopy(this.elements, 0, targetArray, index, this.size);
93 	}
94 
elementAt(int index)95 	public Object elementAt(int index) {
96 
97 		return this.elements[index];
98 	}
99 
find(Object element)100 	public Object find(Object element) {
101 
102 		for (int i = this.size; --i >= 0;)
103 			if (element.equals(this.elements[i]))
104 				return this.elements[i];
105 		return null;
106 	}
107 
remove(Object element)108 	public Object remove(Object element) {
109 
110 		// assumes only one occurrence of the element exists
111 		for (int i = this.size; --i >= 0;)
112 			if (element.equals(this.elements[i])) {
113 				// shift the remaining elements down one spot
114 				System.arraycopy(this.elements, i + 1, this.elements, i, --this.size - i);
115 				this.elements[this.size] = null;
116 				return element;
117 			}
118 		return null;
119 	}
120 
removeAll()121 	public void removeAll() {
122 
123 		for (int i = this.size; --i >= 0;)
124 			this.elements[i] = null;
125 		this.size = 0;
126 	}
127 
size()128 	public int size(){
129 
130 		return this.size;
131 	}
132 
133 	@Override
toString()134 	public String toString() {
135 
136 		String s = ""; //$NON-NLS-1$
137 		for (int i = 0; i < this.size; i++)
138 			s += this.elements[i].toString() + "\n"; //$NON-NLS-1$
139 		return s;
140 	}
141 
142 	@Override
iterator()143 	public Iterator<Object> iterator() {
144 		return new Iterator<Object>() {
145 			int i=0;
146 			@Override
147 			public boolean hasNext() {
148 				return this.i < ObjectVector.this.size;
149 			}
150 			@Override
151 			public Object next() {
152 				return ObjectVector.this.elementAt(this.i++);
153 			}
154 		};
155 	}
156 }
157