Class Pager<T>

java.lang.Object
org.gitlab4j.api.Pager<T>
Type Parameters:
T - the GitLab4J type contained in the List.
All Implemented Interfaces:
Iterator<List<T>>, Constants

public class Pager<T> extends Object implements Iterator<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 Details

    • Pager

      public Pager(AbstractApi api, Class<T> type, int itemsPerPage, jakarta.ws.rs.core.MultivaluedMap<String,String> queryParams, 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 Details

    • 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 Iterator<T>
      Returns:
      true if there are additional pages to iterate over, otherwise returns false
    • next

      public List<T> next()
      Returns the next List in the iteration containing the next page of objects.
      Specified by:
      next in interface Iterator<T>
      Returns:
      the next List in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
      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 Iterator<T>
      Throws:
      UnsupportedOperationException - when invoked
    • first

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

      public 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 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 List<T> current()
      Returns the current page of List.
      Returns:
      the current page of List
    • page

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

      public 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

      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:
      IllegalStateException - if Stream has already been issued
      GitLabApiException - if any other error occurs
    • lazyStream

      public Stream<T> lazyStream() throws 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:
      IllegalStateException - if Stream has already been issued