IntArray.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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     // Positional Access Operations
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);  // Increments modCount!!
00183       elementData[size++] = o;
00184       return true;
00185     }
00186 
00193     public boolean add(IntArray  array) {
00194       ensureCapacity(size + array.size());  // Increments modCount!!
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);  // Increments modCount!!
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);  // Increments modCount!!
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; // Let gc do its work
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 }

Generated on Tue May 22 23:01:10 2007 for OpenMobileIS by  doxygen 1.5.1-p1