1 2 17 18 19 package org.apache.poi.util; 20 21 import java.util.*; 22 23 49 50 public class ShortList 51 { 52 private short[] _array; 53 private int _limit; 54 private static final int _default_size = 128; 55 56 59 60 public ShortList() 61 { 62 this(_default_size); 63 } 64 65 70 71 public ShortList(final ShortList list) 72 { 73 this(list._array.length); 74 System.arraycopy(list._array, 0, _array, 0, _array.length); 75 _limit = list._limit; 76 } 77 78 83 84 public ShortList(final int initialCapacity) 85 { 86 _array = new short[ initialCapacity ]; 87 _limit = 0; 88 } 89 90 99 100 public void add(final int index, final short value) 101 { 102 if (index > _limit) 103 { 104 throw new IndexOutOfBoundsException (); 105 } 106 else if (index == _limit) 107 { 108 add(value); 109 } 110 else 111 { 112 113 if (_limit == _array.length) 115 { 116 growArray(_limit * 2); 117 } 118 System.arraycopy(_array, index, _array, index + 1, 119 _limit - index); 120 _array[ index ] = value; 121 _limit++; 122 } 123 } 124 125 133 134 public boolean add(final short value) 135 { 136 if (_limit == _array.length) 137 { 138 growArray(_limit * 2); 139 } 140 _array[ _limit++ ] = value; 141 return true; 142 } 143 144 158 159 public boolean addAll(final ShortList c) 160 { 161 if (c._limit != 0) 162 { 163 if ((_limit + c._limit) > _array.length) 164 { 165 growArray(_limit + c._limit); 166 } 167 System.arraycopy(c._array, 0, _array, _limit, c._limit); 168 _limit += c._limit; 169 } 170 return true; 171 } 172 173 194 195 public boolean addAll(final int index, final ShortList c) 196 { 197 if (index > _limit) 198 { 199 throw new IndexOutOfBoundsException (); 200 } 201 if (c._limit != 0) 202 { 203 if ((_limit + c._limit) > _array.length) 204 { 205 growArray(_limit + c._limit); 206 } 207 208 System.arraycopy(_array, index, _array, index + c._limit, 210 _limit - index); 211 212 System.arraycopy(c._array, 0, _array, index, c._limit); 214 _limit += c._limit; 215 } 216 return true; 217 } 218 219 223 224 public void clear() 225 { 226 _limit = 0; 227 } 228 229 238 239 public boolean contains(final short o) 240 { 241 boolean rval = false; 242 243 for (int j = 0; !rval && (j < _limit); j++) 244 { 245 if (_array[ j ] == o) 246 { 247 rval = true; 248 } 249 } 250 return rval; 251 } 252 253 262 263 public boolean containsAll(final ShortList c) 264 { 265 boolean rval = true; 266 267 if (this != c) 268 { 269 for (int j = 0; rval && (j < c._limit); j++) 270 { 271 if (!contains(c._array[ j ])) 272 { 273 rval = false; 274 } 275 } 276 } 277 return rval; 278 } 279 280 295 296 public boolean equals(final Object o) 297 { 298 boolean rval = this == o; 299 300 if (!rval && (o != null) && (o.getClass() == this.getClass())) 301 { 302 ShortList other = ( ShortList ) o; 303 304 if (other._limit == _limit) 305 { 306 307 rval = true; 309 for (int j = 0; rval && (j < _limit); j++) 310 { 311 rval = _array[ j ] == other._array[ j ]; 312 } 313 } 314 } 315 return rval; 316 } 317 318 328 329 public short get(final int index) 330 { 331 if (index >= _limit) 332 { 333 throw new IndexOutOfBoundsException (); 334 } 335 return _array[ index ]; 336 } 337 338 357 358 public int hashCode() 359 { 360 int hash = 0; 361 362 for (int j = 0; j < _limit; j++) 363 { 364 hash = (31 * hash) + _array[ j ]; 365 } 366 return hash; 367 } 368 369 381 382 public int indexOf(final short o) 383 { 384 int rval = 0; 385 386 for (; rval < _limit; rval++) 387 { 388 if (o == _array[ rval ]) 389 { 390 break; 391 } 392 } 393 if (rval == _limit) 394 { 395 rval = -1; } 397 return rval; 398 } 399 400 405 406 public boolean isEmpty() 407 { 408 return _limit == 0; 409 } 410 411 423 424 public int lastIndexOf(final short o) 425 { 426 int rval = _limit - 1; 427 428 for (; rval >= 0; rval--) 429 { 430 if (o == _array[ rval ]) 431 { 432 break; 433 } 434 } 435 return rval; 436 } 437 438 451 452 public short remove(final int index) 453 { 454 if (index >= _limit) 455 { 456 throw new IndexOutOfBoundsException (); 457 } 458 short rval = _array[ index ]; 459 460 System.arraycopy(_array, index + 1, _array, index, _limit - index); 461 _limit--; 462 return rval; 463 } 464 465 476 477 public boolean removeValue(final short o) 478 { 479 boolean rval = false; 480 481 for (int j = 0; !rval && (j < _limit); j++) 482 { 483 if (o == _array[ j ]) 484 { 485 System.arraycopy(_array, j + 1, _array, j, _limit - j); 486 _limit--; 487 rval = true; 488 } 489 } 490 return rval; 491 } 492 493 502 503 public boolean removeAll(final ShortList c) 504 { 505 boolean rval = false; 506 507 for (int j = 0; j < c._limit; j++) 508 { 509 if (removeValue(c._array[ j ])) 510 { 511 rval = true; 512 } 513 } 514 return rval; 515 } 516 517 528 529 public boolean retainAll(final ShortList c) 530 { 531 boolean rval = false; 532 533 for (int j = 0; j < _limit; ) 534 { 535 if (!c.contains(_array[ j ])) 536 { 537 remove(j); 538 rval = true; 539 } 540 else 541 { 542 j++; 543 } 544 } 545 return rval; 546 } 547 548 560 561 public short set(final int index, final short element) 562 { 563 if (index >= _limit) 564 { 565 throw new IndexOutOfBoundsException (); 566 } 567 short rval = _array[ index ]; 568 569 _array[ index ] = element; 570 return rval; 571 } 572 573 580 581 public int size() 582 { 583 return _limit; 584 } 585 586 594 595 public short [] toArray() 596 { 597 short[] rval = new short[ _limit ]; 598 599 System.arraycopy(_array, 0, rval, 0, _limit); 600 return rval; 601 } 602 603 614 615 public short [] toArray(final short [] a) 616 { 617 short[] rval; 618 619 if (a.length == _limit) 620 { 621 System.arraycopy(_array, 0, a, 0, _limit); 622 rval = a; 623 } 624 else 625 { 626 rval = toArray(); 627 } 628 return rval; 629 } 630 631 private void growArray(final int new_size) 632 { 633 int size = (new_size == _array.length) ? new_size + 1 634 : new_size; 635 short[] new_array = new short[ size ]; 636 637 System.arraycopy(_array, 0, new_array, 0, _limit); 638 _array = new_array; 639 } 640 } 642 | Popular Tags |