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

apoc.bolt.ClosedAwareDelegatingIterator Maven / Gradle / Ivy

package apoc.bolt;

import org.neo4j.driver.exceptions.ResultConsumedException;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * if we would pass through a bolt result stream directly as a procedure result stream, there's an issue regarding
 * hasNext() throwing an exception.
 * This wrapper class catches the exception and deals with it gracefully
 * @param 
 */
public class ClosedAwareDelegatingIterator implements Iterator {
    private final Iterator delegate;
    private boolean closed = false;

    public ClosedAwareDelegatingIterator(Iterator delegate) {
        this.delegate = delegate;
    }

    @Override
    public boolean hasNext() {
        if (closed) {
            return false;
        }
        try {
            return delegate.hasNext();
        } catch (ResultConsumedException e) {
            closed = true;
            return false;
        }
    }

    @Override
    public T next() {
        try {

            return delegate.next();
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void forEachRemaining(Consumer action) {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy