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