00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.openmobileis.common.util.collection;
00031
00040 public final class IntArray implements java.io.Serializable {
00041 protected static final long serialVersionUID = 5521257935120563452L;
00042
00047 private int elementData[];
00048
00052 private int size;
00053
00061 public IntArray(int initialCapacity) {
00062 super();
00063 if (initialCapacity < 0)
00064 throw new IllegalArgumentException("Illegal Capacity: "+
00065 initialCapacity);
00066 this.elementData = new int[initialCapacity];
00067 }
00068
00072 public IntArray() {
00073 this(10);
00074 }
00075
00076 public void clear() {
00077 this.elementData = new int[this.elementData.length];
00078 size = 0;
00079 }
00080
00081 protected int[] getArrayElements() {
00082 return elementData;
00083 }
00084
00085 protected void setArrayElements(int[] array) {
00086 elementData = array;
00087 }
00088
00089
00097 public void ensureCapacity(int minCapacity) {
00098 int oldCapacity = elementData.length;
00099 if (minCapacity > oldCapacity) {
00100 int oldData[] = elementData;
00101 int newCapacity = (oldCapacity * 3)/2 + 1;
00102 if (newCapacity < minCapacity)
00103 newCapacity = minCapacity;
00104 elementData = new int[newCapacity];
00105 System.arraycopy(oldData, 0, elementData, 0, size);
00106 }
00107 }
00108
00114 public int size() {
00115 return size;
00116 }
00117
00124 public boolean isEmpty() {
00125 return size == 0;
00126 }
00127
00134 public boolean contains(int o) {
00135 for (int i=0; i<size; i++) {
00136 if (elementData[i] == o) {
00137 return true;
00138 }
00139 }
00140 return false;
00141 }
00142
00143
00151 public int[] toArray() {
00152 int[] result = new int[size];
00153 System.arraycopy(elementData, 0, result, 0, size);
00154 return result;
00155 }
00156
00157
00158
00159
00168 public int get(int index) {
00169 RangeCheck(index);
00170
00171 return elementData[index];
00172 }
00173
00174
00181 public boolean add(int o) {
00182 ensureCapacity(size + 1);
00183 elementData[size++] = o;
00184 return true;
00185 }
00186
00193 public boolean add(IntArray array) {
00194 ensureCapacity(size + array.size());
00195 System.arraycopy(array.getArrayElements(), 0, elementData, size,
00196 array.size());
00197 size+= array.size();
00198 return true;
00199 }
00200
00209 public boolean replace(int index, int o) {
00210 if (index >= size) {
00211 return this.add(o);
00212 }
00213 elementData[index] = o;
00214 return true;
00215 }
00216
00223 public boolean add(int[] array) {
00224 ensureCapacity(size + array.length);
00225 System.arraycopy(array, 0, elementData, size,
00226 array.length);
00227 size+= array.length;
00228 return true;
00229 }
00230
00237 public boolean add(int index, int o) {
00238 ensureCapacity(index);
00239 elementData[index] = o;
00240 if (index >= size)
00241 size = index+1;
00242 return true;
00243 }
00244
00245
00246
00257 public long remove(int index) {
00258 RangeCheck(index);
00259 long oldValue = elementData[index];
00260
00261 int numMoved = size - index - 1;
00262 if (numMoved > 0)
00263 System.arraycopy(elementData, index+1, elementData, index,
00264 numMoved);
00265 elementData[--size] = 0;
00266
00267 return oldValue;
00268 }
00269
00270
00271
00272
00277 private void RangeCheck(int index) {
00278 if (index >= size || index < 0)
00279 throw new IndexOutOfBoundsException(
00280 "Index: "+index+", Size: "+size);
00281 }
00282 }