Class Pager<T>

  • Type Parameters:
    T - the GitLab4J type contained in the List.
    All Implemented Interfaces:
    java.util.Iterator<java.util.List<T>>, Constants

    public class Pager<T>
    extends java.lang.Object
    implements java.util.Iterator<java.util.List<T>>, Constants

    This class defines an Iterator implementation that is used as a paging iterator for all API methods that return a List of objects. It hides the details of interacting with the GitLab API when paging is involved simplifying accessing large lists of objects.

    Example usage:

       // Get a Pager instance that will page through the projects with 10 projects per page
       Pager<Project> projectPager = gitlabApi.getProjectsApi().getProjectsPager(10);
    
       // Iterate through the pages and print out the name and description
       while (projectsPager.hasNext())) {
           List<Project> projects = projectsPager.next();
           for (Project project : projects) {
               System.out.println(project.getName() + " : " + project.getDescription());
           }
       }
     
    • Constructor Detail

      • Pager

        public Pager​(AbstractApi api,
                     java.lang.Class<T> type,
                     int itemsPerPage,
                     jakarta.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> queryParams,
                     java.lang.Object... pathArgs)
              throws GitLabApiException
        Creates a Pager instance to access the API through the specified path and query parameters.
        Parameters:
        api - the AbstractApi implementation to communicate through
        type - the GitLab4J type that will be contained in the List
        itemsPerPage - items per page
        queryParams - HTTP query params
        pathArgs - HTTP path arguments
        Throws:
        GitLabApiException - if any error occurs
    • Method Detail

      • getItemsPerPage

        public int getItemsPerPage()
        Get the items per page value.
        Returns:
        the items per page value
      • getTotalPages

        public int getTotalPages()
        Get the total number of pages returned by the GitLab API.
        Returns:
        the total number of pages returned by the GitLab API, or -1 if the Kaminari limit of 10,000 has been exceeded
      • getTotalItems

        public int getTotalItems()
        Get the total number of items (T instances) returned by the GitLab API.
        Returns:
        the total number of items (T instances) returned by the GitLab API, or -1 if the Kaminari limit of 10,000 has been exceeded
      • getCurrentPage

        public int getCurrentPage()
        Get the current page of the iteration.
        Returns:
        the current page of the iteration
      • hasNext

        public boolean hasNext()
        Returns the true if there are additional pages to iterate over, otherwise returns false.
        Specified by:
        hasNext in interface java.util.Iterator<T>
        Returns:
        true if there are additional pages to iterate over, otherwise returns false
      • next

        public java.util.List<T> next()
        Returns the next List in the iteration containing the next page of objects.
        Specified by:
        next in interface java.util.Iterator<T>
        Returns:
        the next List in the iteration
        Throws:
        java.util.NoSuchElementException - if the iteration has no more elements
        java.lang.RuntimeException - if a GitLab API error occurs, will contain a wrapped GitLabApiException with the details of the error
      • remove

        public void remove()
        This method is not implemented and will throw an UnsupportedOperationException if called.
        Specified by:
        remove in interface java.util.Iterator<T>
        Throws:
        java.lang.UnsupportedOperationException - when invoked
      • first

        public java.util.List<T> first()
        Returns the first page of List. Will rewind the iterator.
        Returns:
        the first page of List
      • last

        public java.util.List<T> last()
                               throws GitLabApiException
        Returns the last page of List. Will set the iterator to the end.
        Returns:
        the last page of List
        Throws:
        GitLabApiException - if any error occurs
      • previous

        public java.util.List<T> previous()
        Returns the previous page of List. Will set the iterator to the previous page.
        Returns:
        the previous page of List
      • current

        public java.util.List<T> current()
        Returns the current page of List.
        Returns:
        the current page of List
      • page

        public java.util.List<T> page​(int pageNumber)
        Returns the specified page of List.
        Parameters:
        pageNumber - the page to get
        Returns:
        the specified page of List
        Throws:
        java.util.NoSuchElementException - if the iteration has no more elements
        java.lang.RuntimeException - if a GitLab API error occurs, will contain a wrapped GitLabApiException with the details of the error
      • all

        public java.util.List<T> all()
                              throws GitLabApiException
        Gets all the items from each page as a single List instance.
        Returns:
        all the items from each page as a single List instance
        Throws:
        GitLabApiException - if any error occurs
      • stream

        public java.util.stream.Stream<T> stream()
                                          throws GitLabApiException,
                                                 java.lang.IllegalStateException
        Builds and returns a Stream instance which is pre-populated with all items from all pages.
        Returns:
        a Stream instance which is pre-populated with all items from all pages
        Throws:
        java.lang.IllegalStateException - if Stream has already been issued
        GitLabApiException - if any other error occurs
      • lazyStream

        public java.util.stream.Stream<T> lazyStream()
                                              throws java.lang.IllegalStateException
        Creates a Stream instance for lazily streaming items from the GitLab server.
        Returns:
        a Stream instance for lazily streaming items from the GitLab server
        Throws:
        java.lang.IllegalStateException - if Stream has already been issued