ca.odell.glazedlists.impl.SimpleIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glazedlists_java15 Show documentation
Show all versions of glazedlists_java15 Show documentation
Event-driven lists for dynamically filtered and sorted tables
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl;
// for being a list iterator
import java.util.*;
/**
* The SimpleTreeIterator is an iterator that allows the user to iterate
* on a list.
*
* @author Kevin Maltby
*/
public class SimpleIterator implements Iterator {
/** the list being iterated */
private final List source;
/** the index of the next element to view */
private int nextIndex = 0;
/**
* Create a new iterator that iterates over the specified source list.
*
* @param source the list to iterate
*/
public SimpleIterator(List source) {
this.source = source;
}
/**
* Returns true if this iterator has more elements when traversing the
* list in the forward direction.
*/
public boolean hasNext() {
return nextIndex < source.size();
}
/**
* Returns the next element in the list.
*/
public E next() {
// there are no more values
if (nextIndex == source.size())
throw new NoSuchElementException("Cannot retrieve element " + nextIndex + " on a list of size " + source.size());
// a next value exists
return source.get(nextIndex++);
}
/**
* Removes from the list the last element that was returned by next
* or previous.
*/
public void remove() {
if (nextIndex == 0)
throw new IllegalStateException("Cannot remove() without a prior call to next() or previous()");
source.remove(--nextIndex);
}
}