{@inheritDoc}
(int fromIndex, int toIndex)
| 360 | * {@inheritDoc} |
| 361 | */ |
| 362 | @Override |
| 363 | public List<Comparable> subList(int fromIndex, int toIndex) { |
| 364 | if (fromIndex < 0) { |
| 365 | throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
| 366 | } |
| 367 | if (fromIndex > toIndex) { |
| 368 | throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); |
| 369 | } |
| 370 | if (fromIndex == toIndex) { |
| 371 | return new EmptyRange<Comparable>(from); |
| 372 | } |
| 373 | |
| 374 | // Performance detail: |
| 375 | // not using get(fromIndex), get(toIndex) in the following to avoid stepping over elements twice |
| 376 | final Iterator<Comparable> iter = new StepIterator(this, 1); |
| 377 | |
| 378 | Comparable toValue = iter.next(); |
| 379 | int i = 0; |
| 380 | for (; i < fromIndex; i++) { |
| 381 | if (!iter.hasNext()) { |
| 382 | throw new IndexOutOfBoundsException("Index: " + i + " is too big for range: " + this); |
| 383 | } |
| 384 | toValue = iter.next(); |
| 385 | } |
| 386 | final Comparable fromValue = toValue; |
| 387 | for (; i < toIndex - 1; i++) { |
| 388 | if (!iter.hasNext()) { |
| 389 | throw new IndexOutOfBoundsException("Index: " + i + " is too big for range: " + this); |
| 390 | } |
| 391 | toValue = iter.next(); |
| 392 | } |
| 393 | |
| 394 | return new ObjectRange(fromValue, toValue, reverse); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * {@inheritDoc} |