package lc.esp.eo;

import com.mongodb.client.MongoCursor;
import lc.esp.eo.sdk.EO;
import org.bson.Document;

import java.util.Iterator;

public class EOObjectCursor implements Iterable<EO>, AutoCloseable {
    private final MongoCursor<Document> cursor;

    public EOObjectCursor(MongoCursor<Document> cursor) {
        this.cursor = cursor;
    }

    @Override
    public void close() throws Exception {
        cursor.close();
    }

    @Override
    public Iterator<EO> iterator() {
        return new Iterator<>() {
            @Override
            public boolean hasNext() {
                return cursor.hasNext();
            }

            @Override
            public EO next() {
                return EOTransaction.docToObject(cursor.next());
            }
        };
    }
}
