1 19 package bak.pcj.list; 20 21 import bak.pcj.ByteIterator; 22 import bak.pcj.ByteCollection; 23 import bak.pcj.hash.DefaultByteHashFunction; 24 import bak.pcj.util.Exceptions; 25 26 import java.io.Serializable ; 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 31 41 public class ByteArrayList extends AbstractByteList implements Cloneable , Serializable { 42 43 44 private static final int GROWTH_POLICY_RELATIVE = 0; 45 46 47 private static final int GROWTH_POLICY_ABSOLUTE = 1; 48 49 54 private static final int DEFAULT_GROWTH_POLICY = GROWTH_POLICY_RELATIVE; 55 56 57 public static final double DEFAULT_GROWTH_FACTOR = 1.0; 58 59 60 public static final int DEFAULT_GROWTH_CHUNK = 10; 61 62 63 public static final int DEFAULT_CAPACITY = 10; 64 65 66 private transient byte[] data; 67 68 72 private int size; 73 74 78 private int growthPolicy; 79 80 85 private double growthFactor; 86 87 92 private int growthChunk; 93 94 private ByteArrayList(int capacity, int growthPolicy, double growthFactor, int growthChunk) { 95 if (capacity < 0) 96 Exceptions.negativeArgument("capacity", String.valueOf(capacity)); 97 if (growthFactor < 0.0) 98 Exceptions.negativeArgument("growthFactor", String.valueOf(growthFactor)); 99 if (growthChunk < 0) 100 Exceptions.negativeArgument("growthChunk", String.valueOf(growthChunk)); 101 data = new byte[capacity]; 102 size = 0; 103 this.growthPolicy = growthPolicy; 104 this.growthFactor = growthFactor; 105 this.growthChunk = growthChunk; 106 } 107 108 114 public ByteArrayList() { 115 this(DEFAULT_CAPACITY); 116 } 117 118 131 public ByteArrayList(ByteCollection c) { 132 this(c.size()); 133 addAll(c); 134 } 135 136 150 public ByteArrayList(byte[] a) { 151 this(a.length); 152 System.arraycopy(a, 0, data, 0, a.length); 153 size = a.length; 154 } 155 156 168 public ByteArrayList(int capacity) { 169 this(capacity, DEFAULT_GROWTH_FACTOR); 170 } 171 172 191 public ByteArrayList(int capacity, double growthFactor) { 192 this(capacity, GROWTH_POLICY_RELATIVE, growthFactor, DEFAULT_GROWTH_CHUNK); 193 } 194 195 214 public ByteArrayList(int capacity, int growthChunk) { 215 this(capacity, GROWTH_POLICY_ABSOLUTE, DEFAULT_GROWTH_FACTOR, growthChunk); 216 } 217 218 222 231 private int computeCapacity(int capacity) { 232 int newcapacity; 233 if (growthPolicy == GROWTH_POLICY_RELATIVE) 234 newcapacity = (int)(data.length * (1.0 + growthFactor)); 235 else 236 newcapacity = data.length + growthChunk; 237 if (newcapacity < capacity) 238 newcapacity = capacity; 239 return newcapacity; 240 } 241 242 254 public int ensureCapacity(int capacity) { 255 if (capacity > data.length) { 256 byte[] newdata = new byte[capacity = computeCapacity(capacity)]; 257 System.arraycopy(data, 0, newdata, 0, size); 258 data = newdata; 259 } 260 return capacity; 261 } 262 263 272 public int capacity() 273 { return data.length; } 274 275 279 public void add(int index, byte v) { 280 if (index < 0 || index > size) 281 Exceptions.indexOutOfBounds(index, 0, size); 282 ensureCapacity(size+1); 283 int block = size-index; 285 if (block > 0) 286 System.arraycopy(data, index, data, index+1, block); 287 data[index] = v; 288 size++; 289 } 290 291 public byte get(int index) { 292 if (index < 0 || index >= size) 293 Exceptions.indexOutOfBounds(index, 0, size-1); 294 return data[index]; 295 } 296 297 public byte set(int index, byte v) { 298 if (index < 0 || index >= size) 299 Exceptions.indexOutOfBounds(index, 0, size-1); 300 byte result = data[index]; 301 data[index] = v; 302 return result; 303 } 304 305 public byte removeElementAt(int index) { 306 if (index < 0 || index >= size) 307 Exceptions.indexOutOfBounds(index, 0, size-1); 308 byte result = data[index]; 309 int block = size-(index+1); 311 if (block > 0) 312 System.arraycopy(data, index+1, data, index, block); 313 size--; 314 return result; 315 } 316 317 323 public void trimToSize() { 324 if (data.length > size) { 325 byte[] newdata = new byte[size]; 326 System.arraycopy(data, 0, newdata, 0, size); 327 data = newdata; 328 } 329 } 330 331 338 public Object clone() { 339 try { 340 ByteArrayList c = (ByteArrayList)super.clone(); 341 c.data = new byte[data.length]; 342 System.arraycopy(data, 0, c.data, 0, size); 343 return c; 344 } catch (CloneNotSupportedException e) { 345 Exceptions.cloning(); return null; 346 } 347 } 348 349 353 public int size() 354 { return size; } 355 356 public boolean isEmpty() 357 { return size == 0; } 358 359 public void clear() 360 { size = 0; } 361 362 public boolean contains(byte v) { 363 for (int i = 0; i < size; i++) 364 if (data[i] == v) 365 return true; 366 return false; 367 } 368 369 public int indexOf(byte c) { 370 for (int i = 0; i < size; i++) 371 if (data[i] == c) 372 return i; 373 return -1; 374 } 375 376 379 public int indexOf(int index, byte c) { 380 if (index < 0 || index > size) 381 Exceptions.indexOutOfBounds(index, 0, size); 382 for (int i = index; i < size; i++) 383 if (data[i] == c) 384 return i; 385 return -1; 386 } 387 388 389 public int lastIndexOf(byte c) { 390 for (int i = size-1; i >= 0; i--) 391 if (data[i] == c) 392 return i; 393 return -1; 394 } 395 396 public boolean remove(byte v) { 397 int index = indexOf(v); 398 if (index != -1) { 399 removeElementAt(index); 400 return true; 401 } 402 return false; 403 } 404 405 public byte[] toArray() { 406 byte[] a = new byte[size]; 407 System.arraycopy(data, 0, a, 0, size); 408 return a; 409 } 410 411 public byte[] toArray(byte[] a) { 412 if (a == null || a.length < size) 413 a = new byte[size]; 414 System.arraycopy(data, 0, a, 0, size); 415 return a; 416 } 417 418 public boolean equals(Object obj) { 419 if (this == obj) 420 return true; 421 if (!(obj instanceof ByteList)) 422 return false; 423 int i1 = 0; 424 ByteListIterator i2 = ((ByteList)obj).listIterator(); 425 while(i1 < size && i2.hasNext()) 426 if (data[i1++] != i2.next()) 427 return false; 428 return !(i1 < size || i2.hasNext()); 429 } 430 431 public int hashCode() { 432 int h = 1; 433 for (int i = 0; i < size; i++) 434 h = (int)(31*h + DefaultByteHashFunction.INSTANCE.hash(data[i])); 435 return h; 436 } 437 438 442 449 private void writeObject(ObjectOutputStream s) throws IOException { 450 s.defaultWriteObject(); 451 s.writeInt(data.length); 452 for (int i = 0; i < size; i++) 453 s.writeByte(data[i]); 454 } 455 456 459 private void readObject(ObjectInputStream s) throws IOException , ClassNotFoundException { 460 s.defaultReadObject(); 461 data = new byte[s.readInt()]; 462 for (int i = 0; i < size; i++) 463 data[i] = s.readByte(); 464 } 465 466 } | Popular Tags |