Package com.github.twitch4j.util
Class PaginationUtil
java.lang.Object
com.github.twitch4j.util.PaginationUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.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.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.static <T,
K, C extends Collection<T>>
CgetPaginated
(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.static <T,
K, C extends Collection<T>>
CgetPaginated
(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.static <T,
K, C extends Collection<T>, P>
CgetPaginated
(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.
-
Method Details
-
getPaginated
public static <T,K, C getPaginatedC extends Collection<T>, P> (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 classK
- Container class of results and pagination informationC
- The type of collection that should be used to store the result unitsP
- The type of the cursor object; should implement equals and hashcode- Parameters:
callByCursor
- Performs a query based on the cursorgetResult
- Yields the results contained in the executed callgetNext
- Yields the next cursor to paginate on (or null to cease pagination)maxPages
- The maximum number of pages to paginate overmaxUnits
- The (approximate) maximum number of elements that can be queriedcollector
- The supplier of the collection to store the paginated resultsstrict
- Whether pagination should stop upon encountering an already-seen cursorfirst
- The initial cursor to use in the first callvalid
- Whether a cursor is valid to be used in a subsequent call- Returns:
- the paginated results
-
getPaginated
public static <T,K, C getPaginatedC extends Collection<T>> (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 classK
- Container class of results and pagination informationC
- 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 callextractCursor
- Yields the next cursor to paginate on (or null to cease pagination)maxPages
- The maximum number of pages to paginate overmaxElements
- The (approximate) maximum number of elements that can be queriedcreateCollection
- The supplier of the collection to store the paginated resultsstrict
- Whether pagination should stop upon encountering an already-seen cursor- Returns:
- the paginated results
- See Also:
-
getPaginated
public static <T,K, C getPaginatedC extends Collection<T>> (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 classK
- Container class of results and pagination informationC
- 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 callnextCursorFromCall
- Yields the next cursor to paginate on (or null to cease pagination)maxPages
- The maximum number of pages to paginate overmaxElements
- The (approximate) maximum number of elements that can be queriedcreateCollection
- 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 classK
- 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 callnextCursorFromCall
- Yields the next cursor to paginate on (or null to cease pagination)maxPages
- The maximum number of pages to paginate overmaxElements
- 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 classK
- 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 callnextCursorFromCall
- 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 classK
- 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 callnextCursorFromCall
- Yields the next cursor to paginate on (or null to cease pagination)- Returns:
- the paginated results
- See Also:
-