Skip to content

Commit

Permalink
Move SeqLike::iterator(int) to OrderedTraversable
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed May 23, 2024
1 parent 0642576 commit 3c2276a
Show file tree
Hide file tree
Showing 3 changed files with 318 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@
*/
public interface OrderedTraversable<E> extends Traversable<E> {

default @NotNull Iterator<E> iterator(int beginIndex) {
if (beginIndex < 0) {
throw new IndexOutOfBoundsException("beginIndex(" + beginIndex + ") < 0");
}
final int knownSize = knownSize();
if (knownSize >= 0) {
if (beginIndex > knownSize) {
throw new IndexOutOfBoundsException("beginIndex(" + beginIndex + ") > size(" + knownSize + ")");
}
if (beginIndex == knownSize) {
return Iterators.empty();
}
}

final Iterator<E> it = iterator();
for (int i = 0; i < beginIndex; i++) {
if (!it.hasNext()) {
throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
}
it.next();
}
return it;
}

default @NotNull Iterator<E> reverseIterator() {
final int ks = this.knownSize();
if (ks == 0) {
Expand Down
Loading

0 comments on commit 3c2276a

Please sign in to comment.