Class Paginator<ReqT,ProgressParamT>
- java.lang.Object
-
- com.stackone.stackone_client_java.utils.pagination.Paginator<ReqT,ProgressParamT>
-
- Type Parameters:
ReqT- The type of the request objectProgressParamT- The type of the progression parameter (e.g., page number, offset, cursor)
- All Implemented Interfaces:
java.util.Iterator<java.net.http.HttpResponse<java.io.InputStream>>
public class Paginator<ReqT,ProgressParamT> extends java.lang.Object implements java.util.Iterator<java.net.http.HttpResponse<java.io.InputStream>>A generic pagination implementation that handles fetching and iterating through paginated data. This class implements the Iterator interface and can work with any request and response types. It uses a ProgressTrackerStrategy to process pagination metadata from responses and determine when to stop pagination.The pagination flow works as follows: 1. hasNext() checks if there's a current response or tries to fetch the next page 2. fetchNext() builds the request using the output handler's current value 3. The response is processed by the progress tracker to update pagination state 4. next() returns the current response and clears it for the next iteration
-
-
Constructor Summary
Constructors Constructor Description Paginator(ReqT initialRequest, ProgressTrackerStrategy<ProgressParamT> progressTracker, java.util.function.BiFunction<ReqT,ProgressParamT,ReqT> requestModifier, java.util.function.Function<ReqT,java.net.http.HttpResponse<java.io.InputStream>> dataFetcher)Creates a new Paginator instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Optional<java.net.http.HttpResponse<java.io.InputStream>>currentResponse()Returns the current response if one exists.booleanhasNext()Checks if there are more pages to fetch.java.net.http.HttpResponse<java.io.InputStream>next()Returns the next page of data.
-
-
-
Constructor Detail
-
Paginator
public Paginator(ReqT initialRequest, ProgressTrackerStrategy<ProgressParamT> progressTracker, java.util.function.BiFunction<ReqT,ProgressParamT,ReqT> requestModifier, java.util.function.Function<ReqT,java.net.http.HttpResponse<java.io.InputStream>> dataFetcher)
Creates a new Paginator instance.- Parameters:
initialRequest- The initial request to use for the first pageprogressTracker- The handler that processes pagination metadata from responsesrequestModifier- Function that sets the pagination value in the requestdataFetcher- Function that fetches the response for a given request
-
-
Method Detail
-
currentResponse
public java.util.Optional<java.net.http.HttpResponse<java.io.InputStream>> currentResponse()
Returns the current response if one exists. This is useful for peeking at the current response without consuming it.- Returns:
- An Optional containing the current response, or empty if none exists
-
hasNext
public boolean hasNext()
Checks if there are more pages to fetch. If there's no current response, it attempts to fetch the next page. The output handler determines if there are more pages based on the response metadata.- Specified by:
hasNextin interfacejava.util.Iterator<ReqT>- Returns:
- true if there are more pages to fetch, false otherwise
-
next
public java.net.http.HttpResponse<java.io.InputStream> next()
Returns the next page of data. This method returns the current response and clears it, allowing the next call to hasNext() to fetch the next page if needed.- Specified by:
nextin interfacejava.util.Iterator<ReqT>- Returns:
- The next page of data
- Throws:
java.lang.IllegalStateException- if there is no current response available
-
-