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

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

The newest version!
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Iterator;

import javax.annotation.Nonnull;

// TODO: Auto-generated Javadoc
/**
 * {@link PagedIterable} enhanced to report search result specific information.
 *
 * @author Kohsuke Kawaguchi
 * @param 
 *            the type parameter
 */
@SuppressFBWarnings(
        value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
                "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
        justification = "Constructed by JSON API")
public class PagedSearchIterable 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 SearchResult result;

    /**
     * Instantiates a new paged search iterable.
     *
     * @param root
     *            the root
     * @param request
     *            the request
     * @param receiverType
     *            the receiver type
     */
    PagedSearchIterable(GitHub root, GitHubRequest request, Class> receiverType) {
        this.root = root;
        this.request = request;
        this.receiverType = receiverType;
    }

    /**
     * With page size.
     *
     * @param size
     *            the size
     * @return the paged search iterable
     */
    @Override
    public PagedSearchIterable withPageSize(int size) {
        return (PagedSearchIterable) super.withPageSize(size);
    }

    /**
     * Returns the total number of hit, including the results that's not yet fetched.
     *
     * @return the total count
     */
    public int getTotalCount() {
        populate();
        return result.total_count;
    }

    /**
     * Is incomplete boolean.
     *
     * @return the boolean
     */
    public boolean isIncomplete() {
        populate();
        return result.incomplete_results;
    }

    private void populate() {
        if (result == null)
            iterator().hasNext();
    }

    /**
     * Iterator.
     *
     * @param pageSize
     *            the page size
     * @return the paged iterator
     */
    @Nonnull
    @Override
    public PagedIterator _iterator(int pageSize) {
        final Iterator adapter = adapt(
                GitHubPageIterator.create(root.getClient(), receiverType, request, pageSize));
        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 T[] next() {
                SearchResult v = base.next();
                if (result == null)
                    result = v;
                return v.getItems(root);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy