Class PaginationUtil

java.lang.Object
com.github.twitch4j.util.PaginationUtil

public final class PaginationUtil extends Object
  • Method Details

    • getPaginated

      public static <T, K, C extends Collection<T>, P> C getPaginated(Function<P,K> callByCursor, Function<K,Collection<T>> getResult, Function<K,P> getNext, int maxPages, int maxUnits, Supplier<C> collector, boolean strict, P first, Predicate<P> valid)
      Obtains a full (user-specified) collection of results from paginated calls with arbitrary cursors, up to a max number of pages and elements, while optionally avoiding duplicate cursors.

      Implementation notes:

      • strict mode involves more memory usage but can avoid unnecessary loops when max pages and elements are high and the call can yield undesirable duplicate cursors
      • even without strict mode, this checks that the next cursor does not equal the previous cursor to avoid some loops while reducing memory footprint
      • the max elements constraint can be marginally violated when the final page of results had enough elements to go beyond this threshold
      • pagination stops if any threshold is met (it does not wait for both the max pages and max elements thresholds to be exceeded)
      • null cursors yielded from a call end further pagination
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      C - The type of collection that should be used to store the result units
      P - The type of the cursor object; should implement equals and hashcode
      Parameters:
      callByCursor - Performs a query based on the cursor
      getResult - Yields the results contained in the executed call
      getNext - Yields the next cursor to paginate on (or null to cease pagination)
      maxPages - The maximum number of pages to paginate over
      maxUnits - The (approximate) maximum number of elements that can be queried
      collector - The supplier of the collection to store the paginated results
      strict - Whether pagination should stop upon encountering an already-seen cursor
      first - The initial cursor to use in the first call
      valid - Whether a cursor is valid to be used in a subsequent call
      Returns:
      the paginated results
    • getPaginated

      public static <T, K, C extends Collection<T>> C getPaginated(Function<String,K> callByCursor, Function<K,Collection<T>> extractResult, Function<K,String> extractCursor, int maxPages, int maxElements, Supplier<C> createCollection, boolean strict)
      Obtains a full (user-specified) collection of results from paginated calls, up to a max number of pages and elements, while optionally avoiding duplicate cursors.

      Implementation notes:

      • null or empty cursors yielded from a call end further pagination
      • the first call executed is done with a null cursor
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      C - The type of collection that should be used to store the result units
      Parameters:
      callByCursor - Performs a query based on the string cursor (which is initially null)
      extractResult - Yields the results contained in the executed call
      extractCursor - Yields the next cursor to paginate on (or null to cease pagination)
      maxPages - The maximum number of pages to paginate over
      maxElements - The (approximate) maximum number of elements that can be queried
      createCollection - The supplier of the collection to store the paginated results
      strict - Whether pagination should stop upon encountering an already-seen cursor
      Returns:
      the paginated results
      See Also:
    • getPaginated

      public static <T, K, C extends Collection<T>> C getPaginated(Function<String,K> callByCursor, Function<K,Collection<T>> resultsFromCall, Function<K,String> nextCursorFromCall, int maxPages, int maxElements, Supplier<C> createCollection)
      Obtains a full (user-specified) collection of results from paginated calls, up to a max number of pages and elements.
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      C - The type of collection that should be used to store the result units
      Parameters:
      callByCursor - Performs a query based on the string cursor (which is initially null)
      resultsFromCall - Yields the results contained in the executed call
      nextCursorFromCall - Yields the next cursor to paginate on (or null to cease pagination)
      maxPages - The maximum number of pages to paginate over
      maxElements - The (approximate) maximum number of elements that can be queried
      createCollection - The supplier of the collection to store the paginated results
      Returns:
      the paginated results
      See Also:
    • getPaginated

      public static <T, K> List<T> getPaginated(Function<String,K> callByCursor, Function<K,Collection<T>> resultsFromCall, Function<K,String> nextCursorFromCall, int maxPages, int maxElements)
      Obtains a full list of results from paginated calls, up to a max number of pages and elements.
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      Parameters:
      callByCursor - Performs a query based on the string cursor (which is initially null)
      resultsFromCall - Yields the results contained in the executed call
      nextCursorFromCall - Yields the next cursor to paginate on (or null to cease pagination)
      maxPages - The maximum number of pages to paginate over
      maxElements - The (approximate) maximum number of elements that can be queried
      Returns:
      the paginated results
      See Also:
    • getPaginated

      public static <T, K> List<T> getPaginated(Function<String,K> callByCursor, Function<K,Collection<T>> resultsFromCall, Function<K,String> nextCursorFromCall, int maxPages)
      Obtains a full list of results from paginated calls, up to a max number of pages.
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      Parameters:
      callByCursor - Performs a query based on the string cursor (which is initially null)
      resultsFromCall - Yields the results contained in the executed call
      nextCursorFromCall - Yields the next cursor to paginate on (or null to cease pagination)
      maxPages - The maximum number of pages to paginate over
      Returns:
      the paginated results
      See Also:
    • getPaginated

      public static <T, K> List<T> getPaginated(Function<String,K> callByCursor, Function<K,Collection<T>> resultsFromCall, Function<K,String> nextCursorFromCall)
      Obtains a full list of results from paginated calls.
      Type Parameters:
      T - Result unit class
      K - Container class of results and pagination information
      Parameters:
      callByCursor - Performs a query based on the string cursor (which is initially null)
      resultsFromCall - Yields the results contained in the executed call
      nextCursorFromCall - Yields the next cursor to paginate on (or null to cease pagination)
      Returns:
      the paginated results
      See Also: