1 21 package org.jacorb.collection.util; 22 23 import java.util.*; 24 25 public class DynArray 26 { 27 protected Object elementData[]; 28 protected int elementCount = 0; 29 public DynArray(int initialCapacity) { 30 this.elementData = ArrayFactory.get_array( initialCapacity ); 31 } 32 public DynArray() { 33 this.elementData = new Object [16]; 34 } 35 public void copyInto(Object anArray[]) { 36 System.arraycopy( elementData, 0, anArray, 0, elementCount ); 37 } 38 39 public void ensureCapacity(int minCapacity) { 40 if (minCapacity > elementData.length) { 41 ensureCapacityHelper(minCapacity); 42 } 43 } 44 45 private void ensureCapacityHelper(int minCapacity) { 46 Object [] oldData = elementData; 47 elementData = ArrayFactory.get_array( minCapacity ); 48 System.arraycopy(oldData, 0, elementData, 0, elementCount); 49 ArrayFactory.free_array( oldData ); 50 } 51 52 public void setSize(int newSize) { 53 if (newSize > elementCount) { 54 if (newSize > elementData.length) { 55 ensureCapacityHelper(newSize); 56 } 57 for (int i = newSize ; i >= elementCount ;) { 58 elementData[--i] = null; 59 } 60 } else { 61 for(int i = newSize; i<elementCount; i++ ){ 62 elementData[i] = null; 63 } 64 } 65 elementCount = newSize; 66 } 67 68 public int capacity() { 69 return elementData.length; 70 } 71 72 public int size() { 73 return elementCount; 74 } 75 76 public boolean isEmpty() { 77 return elementCount == 0; 78 } 79 80 public Enumeration elements() { 81 return new DynArrayEnumerator(this); 82 } 83 84 public boolean contains(Object elem) { 85 return indexOf(elem, 0) >= 0; 86 } 87 88 public int indexOf(Object elem) { 89 return indexOf(elem, 0); 90 } 91 92 public int indexOf(Object elem, int index) { 93 if (index >= elementCount) { 94 throw new ArrayIndexOutOfBoundsException (index); 95 } 96 for (int i = index ; i < elementCount ; i++) { 97 if (elem.equals(elementData[i])) { 98 return i; 99 } 100 } 101 return -1; 102 } 103 104 public Object elementAt(int index) { 105 return elementData[index]; 106 } 107 108 public void setElementAt(Object obj, int index) { 109 if (index >= elementCount) { 110 throw new ArrayIndexOutOfBoundsException (index + " >= " + 111 elementCount); 112 } 113 elementData[index] = obj; 114 } 115 116 public void removeElementAt(int index) { 117 if (index >= elementCount || index < 0) { 118 throw new ArrayIndexOutOfBoundsException (index); 119 } 120 int j = elementCount - index - 1; 121 if (j > 0) { 122 System.arraycopy(elementData, index + 1, elementData, index, j); 123 } 124 elementCount--; 125 elementData[elementCount] = null; 126 } 127 128 public void insertElementAt(Object obj, int index) { 129 int newcount = elementCount + 1; 130 if (index >= newcount) { 131 throw new ArrayIndexOutOfBoundsException (index); 132 } 133 if (newcount > elementData.length) { 134 ensureCapacityHelper(newcount); 135 } 136 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); 137 elementData[index] = obj; 138 elementCount++; 139 } 140 141 public void addElement(Object obj) { 142 int newcount = elementCount + 1; 143 if (newcount > elementData.length) { 144 ensureCapacityHelper(newcount); 145 } 146 elementData[elementCount++] = obj; 147 } 148 149 public boolean removeElement(Object obj) { 150 int i = indexOf(obj); 151 if (i >= 0) { 152 removeElementAt(i); 153 return true; 154 } 155 return false; 156 } 157 158 public void removeAllElements() { 159 for (int i = 0; i < elementCount; i++) { 160 elementData[i] = null; 161 } 162 elementCount = 0; 163 } 164 165 public Object firstElement() { 166 if (elementCount == 0) { 167 throw new NoSuchElementException(); 168 } 169 return elementData[0]; 170 } 171 172 public Object lastElement() { 173 if (elementCount == 0) { 174 throw new NoSuchElementException(); 175 } 176 return elementData[elementCount - 1]; 177 } 178 179 protected void finalize(){ 180 if( elementData != null ){ 181 ArrayFactory.free_array( elementData ); 182 } 183 } 184 } 185 186 final 187 class DynArrayEnumerator implements java.util.Enumeration { 188 Object [] data; 189 int count; 190 int elementCount; 191 DynArrayEnumerator(DynArray da) { 192 data = ArrayFactory.get_array( da.elementCount ); 193 elementCount = da.elementCount; 194 System.arraycopy(da.elementData, 0, data, 0, da.elementCount ); 195 count = 0; 196 } 197 198 public boolean hasMoreElements() { 199 return count < elementCount; 200 } 201 202 public Object nextElement() { 203 if (count < elementCount) { 204 return data[count++]; 205 } 206 if( data != null ){ 207 ArrayFactory.free_array( data ); 208 } 209 throw new NoSuchElementException("DynArrayEnumerator"); 210 } 211 protected void finalize(){ 212 if( data != null ){ 213 ArrayFactory.free_array( data ); 214 } 215 } 216 } 217 218 219 220 221 222 223 | Popular Tags |