forked from jython/jython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Iterable in PyIterator. This means generators are usable in…
… foreach loops. The implementation could be moved up to PyObject, but I feel like implementing the interface says that the object will be iterable, and not all PyObjects provide an __iter__.
- Loading branch information
Showing
10 changed files
with
98 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.python.core; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Exposes a Python iter as a Java Iterator. | ||
*/ | ||
public abstract class WrappedIterIterator<E> implements Iterator<E> { | ||
|
||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters