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

org.kohsuke.github.GraphQLPagedSearchIterable Maven / Gradle / Ivy

package org.kohsuke.github;

import java.util.Iterator;
import java.util.function.Function;

/**
 * {@link PagedIterable} enhanced to report search result specific information.
 *
 * @param  the type parameter
 * @author Hiroyuki Wada
 */
public class GraphQLPagedSearchIterable extends PagedIterable {
    private final transient GitHub root;

    private final GitHubRequest request;

    private final Class> receiverType;

    /**
     * As soon as we have any result fetched, it's set here so that we can report the total count.
     */
    private GraphQLSearchResult result;

    private final GraphQLSearchVariables variables;
    private final Function, U[]> adaptor;
    private final Function, GraphQLPageInfo> nextFinder;

    public GraphQLPagedSearchIterable(GitHub root, GitHubRequest request, Class> receiverType,
                                      GraphQLSearchVariables variables,
                                      Function, U[]> adaptor,
                                      Function, GraphQLPageInfo> nextFinder) {
        this.root = root;
        this.request = request;
        this.receiverType = receiverType;
        this.variables = variables;
        this.adaptor = adaptor;
        this.nextFinder = nextFinder;
    }

    @Override
    public GraphQLPagedSearchIterable withPageSize(int size) {
        return (GraphQLPagedSearchIterable) super.withPageSize(size);
    }

    @Override
    public PagedIterator _iterator(int pageSize) {
        variables.first = pageSize;
        final Iterator adapter = adapt(
                GraphQLPageIterator.create(root.getClient(), receiverType, request, variables, nextFinder));
        return new PagedIterator(adapter, null);
    }

    /**
     * Adapts {@link Iterator}.
     *
     * @param base the base
     * @return the iterator
     */
    protected Iterator[]> adapt(final Iterator> base) {
        return new Iterator[]>() {
            public boolean hasNext() {
                return base.hasNext();
            }

            public GraphQLEdge[] next() {
                GraphQLSearchResult v = base.next();
                if (result == null)
                    result = v;
                return adaptor.apply(v);
            }
        };
    }
}