src.org.python.core.WrappedIterIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jython Show documentation
Show all versions of jython Show documentation
Jython is an implementation of the high-level, dynamic, object-oriented
language Python written in 100% Pure Java, and seamlessly integrated with
the Java platform. It thus allows you to run Python on any Java platform.
package org.python.core;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Exposes a Python iter as a Java Iterator.
*/
public abstract class WrappedIterIterator implements Iterator {
private final PyObject iter;
private PyObject next;
private boolean checkedForNext;
public WrappedIterIterator(PyObject iter) {
this.iter = iter;
}
public boolean hasNext() {
if (!checkedForNext) {
next = iter.__iternext__();
checkedForNext = true;
}
return next != null;
}
/**
* Subclasses must implement this to turn the type returned by the iter to the type expected by
* Java.
*/
public abstract E next();
public PyObject getNext() {
if (!hasNext()) {
throw new NoSuchElementException("End of the line, bub");
}
PyObject toReturn = next;
checkedForNext = false;
next = null;
return toReturn;
}
public void remove() {
throw new UnsupportedOperationException("Can't remove from a Python iterator");
}
}