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 package org.openmobileis.common.util.collection;
00030
00031 import org.openmobileis.common.util.OpenMISSerializable;
00032
00040 public class Array implements FilterCollection, OpenMISSerializable {
00041 protected static final long serialVersionUID = 5521257935120563452L;
00042
00047 protected Object elementData[];
00048
00052 protected int size;
00053
00054 private Array(Array array) {
00055 Object[] retElement = new Object[array.elementData.length];
00056 System.arraycopy(array.elementData, 0, retElement, 0,
00057 array.elementData.length);
00058 elementData = retElement;
00059 size = array.size;
00060 }
00061
00062 public Array(Object[] array) {
00063 elementData = array;
00064 size = array.length;
00065 }
00066
00074 public Array(int initialCapacity) {
00075 super();
00076 if (initialCapacity < 0)
00077 throw new IllegalArgumentException("Illegal Capacity: "
00078 + initialCapacity);
00079 this.elementData = new Object[initialCapacity];
00080 }
00081
00085 public Array() {
00086 this(10);
00087 }
00088
00089 public void clear() {
00090 this.elementData = new Object[this.elementData.length];
00091 size = 0;
00092 }
00093
00094 protected Object[] getArrayElements() {
00095 return elementData;
00096 }
00097
00098 protected void setArrayElements(Object[] array) {
00099 elementData = array;
00100 }
00101
00109 public void ensureCapacity(int minCapacity) {
00110 int oldCapacity = elementData.length;
00111 if (minCapacity >= oldCapacity) {
00112 Object oldData[] = elementData;
00113 int newCapacity = (oldCapacity * 3) / 2 + 1;
00114 if (newCapacity < minCapacity)
00115 newCapacity = minCapacity;
00116 elementData = new Object[newCapacity];
00117 System.arraycopy(oldData, 0, elementData, 0, size);
00118 }
00119 }
00120
00126 public int size() {
00127 return size;
00128 }
00129
00133 public void setSize(int size) {
00134 this.size = size;
00135 }
00136
00143 public boolean isEmpty() {
00144 return size == 0;
00145 }
00146
00154 public Object[] toArray() {
00155 Object[] result = new Object[size];
00156 System.arraycopy(elementData, 0, result, 0, size);
00157 return result;
00158 }
00159
00179 public Object[] toArray(Object a[]) {
00180 if (a.length < size)
00181 a = (Object[]) java.lang.reflect.Array.newInstance(a.getClass()
00182 .getComponentType(), size);
00183
00184 System.arraycopy(elementData, 0, a, 0, size);
00185
00186 if (a.length > size)
00187 a[size] = null;
00188
00189 return a;
00190 }
00191
00192
00193
00202 public Object get(int index) {
00203 RangeCheck(index);
00204
00205 return elementData[index];
00206 }
00207
00214 public boolean add(Object o) {
00215 ensureCapacity(size + 1);
00216 elementData[size++] = o;
00217 return true;
00218 }
00219
00228 public boolean replace(int index, Object o) {
00229 if (index >= size) {
00230 return this.add(o);
00231 }
00232 elementData[index] = o;
00233 return true;
00234 }
00235
00242 public boolean add(int index, Object o) {
00243 ensureCapacity(index);
00244 elementData[index] = o;
00245 if (index >= size)
00246 size = index + 1;
00247 return true;
00248 }
00249
00256 public Object getLastAdded() {
00257 if (size > 0) {
00258 return elementData[size - 1];
00259 }
00260 return null;
00261 }
00262
00269 public boolean contains(Object o) {
00270 for (int i = 0; i < size; i++) {
00271 if (elementData[i].equals(o)) {
00272 return true;
00273 }
00274 }
00275 return false;
00276 }
00277
00285 public int indexOf(Object o) {
00286 for (int i = 0; i < size; i++) {
00287 if (elementData[i].equals(o)) {
00288 return i;
00289 }
00290 }
00291 return -1;
00292 }
00293
00300 public boolean addArray(Array a) {
00301 int newSize = size + a.size;
00302 Object[] newArray = new Object[newSize];
00303 System.arraycopy(elementData, 0, newArray, 0, size);
00304 System.arraycopy(a.elementData, 0, newArray, size, a.size);
00305 this.size = newSize;
00306 this.elementData = newArray;
00307 return true;
00308 }
00309
00320 public Object remove(int index) {
00321 RangeCheck(index);
00322 Object oldValue = elementData[index];
00323
00324 int numMoved = size - index - 1;
00325 if (numMoved > 0)
00326 System.arraycopy(elementData, index + 1, elementData, index,
00327 numMoved);
00328 elementData[--size] = null;
00329
00330 return oldValue;
00331 }
00332
00340 public Object remove(Object element) {
00341 Object oldValue = null;
00342 for (int i = 0; i < elementData.length; i++) {
00343 if (elementData[i] == null)
00344 break;
00345 if (elementData[i].equals(element)) {
00346 oldValue = remove(i);
00347 break;
00348 }
00349 }
00350 return oldValue;
00351 }
00352
00353 public void removeAll() {
00354 size = 0;
00355 }
00356
00361 private void RangeCheck(int index) {
00362 if (index >= size || index < 0)
00363 throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
00364 + size);
00365 }
00366
00367
00368 public static void main(String[] args) {
00369 Array array = new Array(2);
00370 array.add("Chaine 1");
00371 array.add("chaine2");
00372 array.add("chaine3");
00373 array.add(2, "Chaine 4");
00374 System.out.println("Value at 2 " + array.get(2));
00375 System.out.println("Value at 3 " + array.get(3));
00376 array.add(2, "chaine2bis");
00377 System.out.println("Value at 2 " + array.get(2));
00378 array.remove(0);
00379 for (int i = 0; i < array.size; i++) {
00380 System.out.println("Value at " + i + " : " + array.get(i));
00381 }
00382
00383 String[] temp = (String[]) array.toArray(new String[array.size]);
00384 for (int i = 0; i < temp.length; i++) {
00385 System.out.println("Value at " + i + " : " + temp[i]);
00386 }
00387 }
00388
00389 public Object clone() {
00390 return new Array(this);
00391
00392 }
00393
00394 public boolean equals(Object obj) {
00395 if (obj == this)
00396 return true;
00397
00398 if (!(obj instanceof Array))
00399 return false;
00400 Array t = (Array) obj;
00401 if (t.size() != size)
00402 return false;
00403
00404 Object[] arrayObj = t.getArrayElements();
00405
00406 for (int i = 0; i < size; i++) {
00407 if (elementData[i] == null) {
00408 if (arrayObj[i] != null) {
00409 return false;
00410 }
00411 } else {
00412 if (!elementData[i].equals(arrayObj[i])) {
00413 return false;
00414 }
00415 }
00416 }
00417
00418 return true;
00419 }
00420
00421 }