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

com.github.vincentrussell.query.mongodb.sql.converter.QueryResultIterator Maven / Gradle / Ivy

Go to download

sql-to-mongo-db-query-converter helps you build quieres for MongoDb based on Queries provided in SQL.

There is a newer version: 1.8
Show newest version
package com.github.vincentrussell.query.mongodb.sql.converter;

import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoIterable;
import org.calrissian.mango.collect.AbstractCloseableIterator;

import java.io.IOException;

/**
 * Wrapper {@link java.util.Iterator} around the {@link MongoCursor}.
 * @param  the type of elements returned by this iterator
 */
public class QueryResultIterator extends AbstractCloseableIterator {

    private final MongoCursor mongoCursor;

    /**
     * Default constructor.
     * @param mongoIterable the wrapped {@link MongoIterable}
     */
    public QueryResultIterator(final MongoIterable mongoIterable) {
        this.mongoCursor = mongoIterable.iterator();
    }

    /**
     * {@inheritDoc}
     * @return the next element if there was one. If {@code endOfData} was called during execution,
     * the return value will be ignored.
     */
    @Override
    protected T computeNext() {
        if (mongoCursor.hasNext()) {
            return mongoCursor.next();
        } else {
            mongoCursor.close();
        }
        return endOfData();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void close() throws IOException {
        mongoCursor.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy