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

syncloud.google.docs.DocumentIterator Maven / Gradle / Ivy

package syncloud.google.docs;

import com.google.api.client.http.HttpRequestFactory;
import syncloud.core.log.Logger;
import syncloud.google.docs.model.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class DocumentIterator implements Iterator {

    private static Logger logger = Logger.getLogger(GDocsFolder.class);

    private HttpRequestFactory requestFactory;
    private DocsUrl url;
    private List docs = null;

    private int position = 0;
    private Link next = null;

    public DocumentIterator(HttpRequestFactory requestFactory, DocsUrl url) {
        this.requestFactory = requestFactory;
        this.url = url;
    }


    @Override
    public boolean hasNext() {
        if (docs != null)
            if (position < docs.size())
                return true;
            else if (next == null)
                return false;

        try {

            Feed feed = null;
            if (next == null)
                feed = Feed.first100Entries(requestFactory, url);
            else
                feed = Feed.first100Entries(requestFactory, new DocsUrl(next));

            next = feed.getLink(LinkRel.NEXT);

            if (feed.docs != null && feed.docs.size() > 0) {
                docs = feed.docs;
                position = 0;
                return true;
            }
        } catch (IOException e) {
            logger.error("unable to send request to google documents", e);
        }

        return false;
    }

    @Override
    public DocumentListEntry next() {
        return docs.get(position++);
    }

    @Override
    public void remove() {
        throw new RuntimeException("not implemented");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy