All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.org.python.core.WrappedIterIterator Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.7.4
Show newest version
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");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy