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

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

package org.kohsuke.github;

import java.util.Iterator;

/**
 * {@link PagedIterable} enhanced to report search result specific information.
 *
 * @param  the type parameter
 * @author Hiroyuki Wada
 */
public class SCIMPagedSearchIterable 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 SCIMSearchResult result;
    
    private int pageOffset;

    public SCIMPagedSearchIterable(GitHub root, GitHubRequest request, Class> receiverType) {
        this.root = root;
        this.request = request;
        this.receiverType = receiverType;
    }

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

    public SCIMPagedSearchIterable withPageOffset(int pageOffset) {
        this.pageOffset = pageOffset;
        return this;
    }

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

    /**
     * Is incomplete boolean.
     *
     * @return the boolean
     */
    public boolean isIncomplete() {
        populate();
        return result.totalResults <= result.startIndex + result.itemsPerPage;
    }

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

    @Override
    public PagedIterator _iterator(int pageSize) {
        final Iterator adapter = adapt(
                SCIMPageIterator.create(root.getClient(), receiverType, request, pageSize, pageOffset));
        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() {
                SCIMSearchResult v = base.next();
                if (result == null)
                    result = v;
                return v.Resources;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy