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 31 public class ArrayBooleanList extends RandomAccessBooleanList 32 implements BooleanList, Serializable { 33 34 37 41 public ArrayBooleanList() { 42 this(8); 43 } 44 45 50 public ArrayBooleanList(int initialCapacity) { 51 if(initialCapacity < 0) { 52 throw new IllegalArgumentException ("capacity " + initialCapacity); 53 } 54 _data = new boolean[initialCapacity]; 55 _size = 0; 56 } 57 58 67 public ArrayBooleanList(BooleanCollection that) { 68 this(that.size()); 69 addAll(that); 70 } 71 72 78 public ArrayBooleanList(boolean[] array) { 79 this(array.length); 80 System.arraycopy(array, 0, _data, 0, array.length); 81 _size = array.length; 82 } 83 84 87 public boolean get(int index) { 88 checkRange(index); 89 return _data[index]; 90 } 91 92 public int size() { 93 return _size; 94 } 95 96 108 public boolean removeElementAt(int index) { 109 checkRange(index); 110 incrModCount(); 111 boolean oldval = _data[index]; 112 int numtomove = _size - index - 1; 113 if(numtomove > 0) { 114 System.arraycopy(_data,index+1,_data,index,numtomove); 115 } 116 _size--; 117 return oldval; 118 } 119 120 132 public boolean set(int index, boolean element) { 133 checkRange(index); 134 incrModCount(); 135 boolean oldval = _data[index]; 136 _data[index] = element; 137 return oldval; 138 } 139 140 155 public void add(int index, boolean element) { 156 checkRangeIncludingEndpoint(index); 157 incrModCount(); 158 ensureCapacity(_size+1); 159 int numtomove = _size-index; 160 System.arraycopy(_data,index,_data,index+1,numtomove); 161 _data[index] = element; 162 _size++; 163 } 164 165 public void clear() { 166 incrModCount(); 167 _size = 0; 168 } 169 170 public boolean addAll(BooleanCollection collection) { 171 return addAll(size(), collection); 172 } 173 174 public boolean addAll(int index, BooleanCollection collection) { 175 if (collection.size() == 0) { 176 return false; 177 } 178 checkRangeIncludingEndpoint(index); 179 incrModCount(); 180 ensureCapacity(_size + collection.size()); 181 if (index != _size) { 182 System.arraycopy(_data, index, _data, index + collection.size(), _size - index); 184 } 185 for (BooleanIterator it = collection.iterator(); it.hasNext();) { 186 _data[index] = it.next(); 187 index++; 188 } 189 _size += collection.size(); 190 return true; 191 } 192 193 196 201 public void ensureCapacity(int mincap) { 202 incrModCount(); 203 if(mincap > _data.length) { 204 int newcap = (_data.length * 3)/2 + 1; 205 boolean[] olddata = _data; 206 _data = new boolean[newcap < mincap ? mincap : newcap]; 207 System.arraycopy(olddata,0,_data,0,_size); 208 } 209 } 210 211 215 public void trimToSize() { 216 incrModCount(); 217 if(_size < _data.length) { 218 boolean[] olddata = _data; 219 _data = new boolean[_size]; 220 System.arraycopy(olddata,0,_data,0,_size); 221 } 222 } 223 224 227 private void writeObject(ObjectOutputStream out) throws IOException { 228 out.defaultWriteObject(); 229 out.writeInt(_data.length); 230 for(int i=0;i<_size;i++) { 231 out.writeBoolean(_data[i]); 232 } 233 } 234 235 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 236 in.defaultReadObject(); 237 _data = new boolean[in.readInt()]; 238 for(int i=0;i<_size;i++) { 239 _data[i] = in.readBoolean(); 240 } 241 } 242 243 private final void checkRange(int index) { 244 if(index < 0 || index >= _size) { 245 throw new IndexOutOfBoundsException ( 246 "Should be at least 0 and less than " 247 + _size + ", found " + index); 248 } 249 } 250 251 private final void checkRangeIncludingEndpoint(int index) { 252 if(index < 0 || index > _size) { 253 throw new IndexOutOfBoundsException ( 254 "Should be at least 0 and at most " + _size 255 + ", found " + index); 256 } 257 } 258 259 262 private transient boolean[] _data = null; 263 private int _size = 0; 264 265 } 266 | Popular Tags |