KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > util > ObjectVector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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 JavaDoc[] elements;
20     
21     public ObjectVector() {
22         this(INITIAL_SIZE);
23     }
24     
25     public ObjectVector(int initialSize) {
26         this.maxSize = initialSize > 0 ? initialSize : INITIAL_SIZE;
27         this.size = 0;
28         this.elements = new Object JavaDoc[this.maxSize];
29     }
30
31     public void add(Object JavaDoc newElement) {
32
33         if (this.size == this.maxSize) // knows that size starts <= maxSize
34
System.arraycopy(this.elements, 0, (this.elements = new Object JavaDoc[this.maxSize *= 2]), 0, this.size);
35         this.elements[this.size++] = newElement;
36     }
37
38     public void addAll(Object JavaDoc[] newElements) {
39
40         if (this.size + newElements.length >= this.maxSize) {
41             maxSize = this.size + newElements.length; // assume no more elements will be added
42
System.arraycopy(this.elements, 0, (this.elements = new Object JavaDoc[this.maxSize]), 0, this.size);
43         }
44         System.arraycopy(newElements, 0, this.elements, size, newElements.length);
45         this.size += newElements.length;
46     }
47
48     public void addAll(ObjectVector newVector) {
49
50         if (this.size + newVector.size >= this.maxSize) {
51             maxSize = this.size + newVector.size; // assume no more elements will be added
52
System.arraycopy(this.elements, 0, (this.elements = new Object JavaDoc[this.maxSize]), 0, this.size);
53         }
54         System.arraycopy(newVector.elements, 0, this.elements, size, newVector.size);
55         this.size += newVector.size;
56     }
57
58     /**
59      * Identity check
60      */

61     public boolean containsIdentical(Object JavaDoc element) {
62
63         for (int i = this.size; --i >= 0;)
64             if (element == this.elements[i])
65                 return true;
66         return false;
67     }
68
69     /**
70      * Equality check
71      */

72     public boolean contains(Object JavaDoc element) {
73
74         for (int i = this.size; --i >= 0;)
75             if (element.equals(this.elements[i]))
76                 return true;
77         return false;
78     }
79
80     public void copyInto(Object JavaDoc[] targetArray){
81         
82         this.copyInto(targetArray, 0);
83     }
84     
85     public void copyInto(Object JavaDoc[] targetArray, int index){
86         
87         System.arraycopy(this.elements, 0, targetArray, index, this.size);
88     }
89     
90     public Object JavaDoc elementAt(int index) {
91
92         return this.elements[index];
93     }
94
95     public Object JavaDoc find(Object JavaDoc element) {
96
97         for (int i = this.size; --i >= 0;)
98             if (element.equals(this.elements[i]))
99                 return element;
100         return null;
101     }
102
103     public Object JavaDoc remove(Object JavaDoc element) {
104
105         // assumes only one occurrence of the element exists
106
for (int i = this.size; --i >= 0;)
107             if (element.equals(this.elements[i])) {
108                 // shift the remaining elements down one spot
109
System.arraycopy(this.elements, i + 1, this.elements, i, --this.size - i);
110                 this.elements[this.size] = null;
111                 return element;
112             }
113         return null;
114     }
115
116     public void removeAll() {
117         
118         for (int i = this.size; --i >= 0;)
119             this.elements[i] = null;
120         this.size = 0;
121     }
122
123     public int size(){
124         
125         return this.size;
126     }
127     
128     public String JavaDoc toString() {
129         
130         String JavaDoc s = ""; //$NON-NLS-1$
131
for (int i = 0; i < this.size; i++)
132             s += this.elements[i].toString() + "\n"; //$NON-NLS-1$
133
return s;
134     }
135 }
136
Popular Tags