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 size = this.elementData.length;
00101 }
00102
00110 public void ensureCapacity(int minCapacity) {
00111 int oldCapacity = elementData.length;
00112 if (minCapacity >= oldCapacity) {
00113 Object oldData[] = elementData;
00114 int newCapacity = (oldCapacity * 3) / 2 + 1;
00115 if (newCapacity < minCapacity)
00116 newCapacity = minCapacity;
00117 elementData = new Object[newCapacity];
00118 System.arraycopy(oldData, 0, elementData, 0, size);
00119 }
00120 }
00121
00127 public int size() {
00128 return size;
00129 }
00130
00134 public void setSize(int size) {
00135 this.size = size;
00136 }
00137
00144 public boolean isEmpty() {
00145 return size == 0;
00146 }
00147
00155 public Object[] toArray() {
00156 Object[] result = new Object[size];
00157 System.arraycopy(elementData, 0, result, 0, size);
00158 return result;
00159 }
00160
00180 public Object[] toArray(Object a[]) {
00181 if (a.length < size)
00182 a = (Object[]) java.lang.reflect.Array.newInstance(a.getClass()
00183 .getComponentType(), size);
00184
00185 System.arraycopy(elementData, 0, a, 0, size);
00186
00187 if (a.length > size)
00188 a[size] = null;
00189
00190 return a;
00191 }
00192
00193
00194
00203 public Object get(int index) {
00204 RangeCheck(index);
00205
00206 return elementData[index];
00207 }
00208
00215 public boolean add(Object o) {
00216 ensureCapacity(size + 1);
00217 elementData[size++] = o;
00218 return true;
00219 }
00220
00229 public boolean replace(int index, Object o) {
00230 if (index >= size) {
00231 return this.add(o);
00232 }
00233 elementData[index] = o;
00234 return true;
00235 }
00236
00243 public boolean add(int index, Object o) {
00244 ensureCapacity(index);
00245 elementData[index] = o;
00246 if (index >= size)
00247 size = index + 1;
00248 return true;
00249 }
00250
00257 public Object getLastAdded() {
00258 if (size > 0) {
00259 return elementData[size - 1];
00260 }
00261 return null;
00262 }
00263
00270 public boolean contains(Object o) {
00271 for (int i = 0; i < size; i++) {
00272 if (elementData[i].equals(o)) {
00273 return true;
00274 }
00275 }
00276 return false;
00277 }
00278
00286 public int indexOf(Object o) {
00287 for (int i = 0; i < size; i++) {
00288 if (elementData[i].equals(o)) {
00289 return i;
00290 }
00291 }
00292 return -1;
00293 }
00294
00301 public boolean addArray(Array a) {
00302 int newSize = size + a.size;
00303 Object[] newArray = new Object[newSize];
00304 System.arraycopy(elementData, 0, newArray, 0, size);
00305 System.arraycopy(a.elementData, 0, newArray, size, a.size);
00306 this.size = newSize;
00307 this.elementData = newArray;
00308 return true;
00309 }
00310
00321 public Object remove(int index) {
00322 RangeCheck(index);
00323 Object oldValue = elementData[index];
00324
00325 int numMoved = size - index - 1;
00326 if (numMoved > 0)
00327 System.arraycopy(elementData, index + 1, elementData, index,
00328 numMoved);
00329 elementData[--size] = null;
00330
00331 return oldValue;
00332 }
00333
00341 public Object remove(Object element) {
00342 Object oldValue = null;
00343 for (int i = 0; i < elementData.length; i++) {
00344 if (elementData[i] == null)
00345 break;
00346 if (elementData[i].equals(element)) {
00347 oldValue = remove(i);
00348 break;
00349 }
00350 }
00351 return oldValue;
00352 }
00353
00354 public void removeAll() {
00355 size = 0;
00356 }
00357
00362 private void RangeCheck(int index) {
00363 if (index >= size || index < 0)
00364 throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
00365 + size);
00366 }
00367
00368
00369 public static void main(String[] args) {
00370 Array array = new Array(2);
00371 array.add("Chaine 1");
00372 array.add("chaine2");
00373 array.add("chaine3");
00374 array.add(2, "Chaine 4");
00375 System.out.println("Value at 2 " + array.get(2));
00376 System.out.println("Value at 3 " + array.get(3));
00377 array.add(2, "chaine2bis");
00378 System.out.println("Value at 2 " + array.get(2));
00379 array.remove(0);
00380 for (int i = 0; i < array.size; i++) {
00381 System.out.println("Value at " + i + " : " + array.get(i));
00382 }
00383
00384 String[] temp = (String[]) array.toArray(new String[array.size]);
00385 for (int i = 0; i < temp.length; i++) {
00386 System.out.println("Value at " + i + " : " + temp[i]);
00387 }
00388 }
00389
00390 public Object clone() {
00391 return new Array(this);
00392
00393 }
00394
00395 public boolean equals(Object obj) {
00396 if (obj == this)
00397 return true;
00398
00399 if (!(obj instanceof Array))
00400 return false;
00401 Array t = (Array) obj;
00402 if (t.size() != size)
00403 return false;
00404
00405 Object[] arrayObj = t.getArrayElements();
00406
00407 for (int i = 0; i < size; i++) {
00408 if (elementData[i] == null) {
00409 if (arrayObj[i] != null) {
00410 return false;
00411 }
00412 } else {
00413 if (!elementData[i].equals(arrayObj[i])) {
00414 return false;
00415 }
00416 }
00417 }
00418
00419 return true;
00420 }
00421
00422 }