1 17 package org.apache.commons.collections.primitives; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 24 34 public class ArrayIntList extends RandomAccessIntList implements IntList, Serializable { 35 36 39 43 public ArrayIntList() { 44 this(8); 45 } 46 47 52 public ArrayIntList(int initialCapacity) { 53 if(initialCapacity < 0) { 54 throw new IllegalArgumentException ("capacity " + initialCapacity); 55 } 56 _data = new int[initialCapacity]; 57 _size = 0; 58 } 59 60 69 public ArrayIntList(IntCollection that) { 70 this(that.size()); 71 addAll(that); 72 } 73 74 80 public ArrayIntList(int[] array) { 81 this(array.length); 82 System.arraycopy(array, 0, _data, 0, array.length); 83 _size = array.length; 84 } 85 86 89 public int get(int index) { 90 checkRange(index); 91 return _data[index]; 92 } 93 94 public int size() { 95 return _size; 96 } 97 98 111 public int removeElementAt(int index) { 112 checkRange(index); 113 incrModCount(); 114 int oldval = _data[index]; 115 int numtomove = _size - index - 1; 116 if(numtomove > 0) { 117 System.arraycopy(_data,index+1,_data,index,numtomove); 118 } 119 _size--; 120 return oldval; 121 } 122 123 136 public int set(int index, int element) { 137 checkRange(index); 138 incrModCount(); 139 int oldval = _data[index]; 140 _data[index] = element; 141 return oldval; 142 } 143 144 159 public void add(int index, int element) { 160 checkRangeIncludingEndpoint(index); 161 incrModCount(); 162 ensureCapacity(_size+1); 163 int numtomove = _size-index; 164 System.arraycopy(_data,index,_data,index+1,numtomove); 165 _data[index] = element; 166 _size++; 167 } 168 169 public void clear() { 170 incrModCount(); 171 _size = 0; 172 } 173 174 public boolean addAll(IntCollection collection) { 175 return addAll(size(), collection); 176 } 177 178 public boolean addAll(int index, IntCollection collection) { 179 if (collection.size() == 0) { 180 return false; 181 } 182 checkRangeIncludingEndpoint(index); 183 incrModCount(); 184 ensureCapacity(_size + collection.size()); 185 if (index != _size) { 186 System.arraycopy(_data, index, _data, index + collection.size(), _size - index); 188 } 189 for (IntIterator it = collection.iterator(); it.hasNext();) { 190 _data[index] = it.next(); 191 index++; 192 } 193 _size += collection.size(); 194 return true; 195 } 196 197 200 205 public void ensureCapacity(int mincap) { 206 incrModCount(); 207 if(mincap > _data.length) { 208 int newcap = (_data.length * 3)/2 + 1; 209 int[] olddata = _data; 210 _data = new int[newcap < mincap ? mincap : newcap]; 211 System.arraycopy(olddata,0,_data,0,_size); 212 } 213 } 214 215 219 public void trimToSize() { 220 incrModCount(); 221 if(_size < _data.length) { 222 int[] olddata = _data; 223 _data = new int[_size]; 224 System.arraycopy(olddata,0,_data,0,_size); 225 } 226 } 227 228 231 private void writeObject(ObjectOutputStream out) throws IOException { 232 out.defaultWriteObject(); 233 out.writeInt(_data.length); 234 for(int i=0;i<_size;i++) { 235 out.writeInt(_data[i]); 236 } 237 } 238 239 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 240 in.defaultReadObject(); 241 _data = new int[in.readInt()]; 242 for(int i=0;i<_size;i++) { 243 _data[i] = in.readInt(); 244 } 245 } 246 247 private final void checkRange(int index) { 248 if(index < 0 || index >= _size) { 249 throw new IndexOutOfBoundsException ("Should be at least 0 and less than " + _size + ", found " + index); 250 } 251 } 252 253 private final void checkRangeIncludingEndpoint(int index) { 254 if(index < 0 || index > _size) { 255 throw new IndexOutOfBoundsException ("Should be at least 0 and at most " + _size + ", found " + index); 256 } 257 } 258 259 262 private transient int[] _data = null; 263 private int _size = 0; 264 265 } 266 | Popular Tags |