I need an iterable data structure that provides the possibilities of the standard
Iterator<>
interface (next()
,hasNext()
, etc.).Additionally it should be possible to reposition the iterator at a certain position.
Is there any class that already provides the mentioned possibilities?
Otherwise I would implement something like the following:
public class PositionableIterator<E> implements Iterator<E> {
private final List<E> delegate;
private Iterator<E> iterator;
public PositionableIterator(List<E> delegate) {
this.delegate = delegate;
this.iterator = delegate.iterator();
}
public void moveTo(int index) {
this.iterator = delegate.listIterator(index);
}
public void moveTo(E element) {
this.iterator = delegate.listIterator(delegate.indexOf(element));
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public E next() {
return iterator.next();
}
}
Do you see any drawbacks using this approach?