1 package antlr.collections.impl; 2 3 9 10 import java.util.Enumeration ; 11 import java.util.NoSuchElementException ; 12 13 import antlr.collections.Enumerator; 14 15 public class Vector implements Cloneable { 16 protected Object [] data; 17 protected int lastElement = -1; 18 19 public Vector() { 20 this(10); 21 } 22 23 public Vector(int size) { 24 data = new Object [size]; 25 } 26 27 public synchronized void appendElement(Object o) { 28 ensureCapacity(lastElement + 2); 29 data[++lastElement] = o; 30 } 31 32 35 public int capacity() { 36 return data.length; 37 } 38 39 public Object clone() { 40 Vector v = null; 41 try { 42 v = (Vector)super.clone(); 43 } 44 catch (CloneNotSupportedException e) { 45 System.err.println("cannot clone Vector.super"); 46 return null; 47 } 48 v.data = new Object [size()]; 49 System.arraycopy(data, 0, v.data, 0, size()); 50 return v; 51 } 52 53 59 public synchronized Object elementAt(int i) { 60 if (i >= data.length) { 61 throw new ArrayIndexOutOfBoundsException (i + " >= " + data.length); 62 } 63 if (i < 0) { 64 throw new ArrayIndexOutOfBoundsException (i + " < 0 "); 65 } 66 return data[i]; 67 } 68 69 public synchronized Enumeration elements() { 70 return new VectorEnumerator(this); 71 } 72 73 public synchronized void ensureCapacity(int minIndex) { 74 if (minIndex + 1 > data.length) { 75 Object oldData[] = data; 76 int n = data.length * 2; 77 if (minIndex + 1 > n) { 78 n = minIndex + 1; 79 } 80 data = new Object [n]; 81 System.arraycopy(oldData, 0, data, 0, oldData.length); 82 } 83 } 84 85 public synchronized boolean removeElement(Object o) { 86 int i; 88 for (i = 0; i <= lastElement && data[i] != o; i++) { 89 ; 90 } 91 if (i <= lastElement) { data[i] = null; int above = lastElement - i; 94 if (above > 0) { 95 System.arraycopy(data, i + 1, data, i, above); 96 } 97 lastElement--; 98 return true; 99 } 100 else { 101 return false; 102 } 103 } 104 105 public synchronized void setElementAt(Object obj, int i) { 106 if (i >= data.length) { 107 throw new ArrayIndexOutOfBoundsException (i + " >= " + data.length); 108 } 109 data[i] = obj; 110 if (i > lastElement) { 112 lastElement = i; 113 } 114 } 115 116 public int size() { 119 return lastElement + 1; 120 } 121 } 122 | Popular Tags |