com.vladsch.flexmark.util.collection.iteration.IndexedIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flexmark-util-collection Show documentation
Show all versions of flexmark-util-collection Show documentation
flexmark-java collection utility classes
The newest version!
package com.vladsch.flexmark.util.collection.iteration;
import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import org.jetbrains.annotations.NotNull;
public class IndexedIterator>
implements ReversibleIndexedIterator {
private final I iterator;
private final Indexed items;
private int lastIndex;
private int modificationCount;
public IndexedIterator(@NotNull Indexed items, @NotNull I iterator) {
this.items = items;
this.iterator = iterator;
this.lastIndex = -1;
this.modificationCount = items.modificationCount();
}
@Override
public boolean isReversed() {
return iterator.isReversed();
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public @NotNull R next() {
if (modificationCount != items.modificationCount()) {
throw new ConcurrentModificationException();
}
lastIndex = iterator.next();
return (R) items.get(lastIndex);
}
@Override
public void remove() {
if (lastIndex == -1) {
throw new NoSuchElementException();
}
if (modificationCount != items.modificationCount()) {
throw new ConcurrentModificationException();
}
items.removeAt(lastIndex);
lastIndex = -1;
modificationCount = items.modificationCount();
}
@Override
public int getIndex() {
if (lastIndex < 0) {
throw new NoSuchElementException();
}
return lastIndex;
}
@Override
public void forEachRemaining(@NotNull Consumer super R> consumer) {
while (hasNext()) {
consumer.accept(next());
}
}
}