dimas-b commented on code in PR #1838:
URL: https://github.com/apache/polaris/pull/1838#discussion_r2144161141


##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/Page.java:
##########
@@ -18,25 +18,96 @@
  */
 package org.apache.polaris.core.persistence.pagination;
 
+import static java.util.Spliterators.iterator;
+
+import jakarta.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
- * An immutable page of items plus their paging cursor. The {@link PageToken} 
here can be used to
- * continue the listing operation that generated the `items`.
+ * An immutable page of items plus their paging cursor. The {@link 
#encodedResponseToken()} here can
+ * be used to continue the listing operation that generated the `items`.
  */
 public class Page<T> {
-  public final PageToken pageToken;
-  public final List<T> items;
+  private final PageToken request;
+  private final List<T> items;
+  @Nullable private final String encodedDataReference;
 
-  public Page(PageToken pageToken, List<T> items) {
-    this.pageToken = pageToken;
+  private Page(PageToken request, @Nullable String encodedDataReference, 
List<T> items) {
+    this.request = request;
+    this.encodedDataReference = encodedDataReference;
     this.items = items;
   }
 
   /**
-   * Used to wrap a {@link List<T>} of items into a {@link Page <T>} when 
there are no more pages
+   * Builds a complete response page for the full list of relevant items. No 
subsequence pages of
+   * related data exist.
    */
   public static <T> Page<T> fromItems(List<T> items) {
-    return new Page<>(new DonePageToken(), items);
+    return new Page<>(PageToken.readEverything(), null, items);
+  }
+
+  /**
+   * Produces a response page by consuming the number of items from the 
provided stream according to
+   * the {@code request} parameter. Source items can be converted to a 
different type by providing a
+   * {@code mapper} function. The page token for the response will be produced 
from the request data
+   * combined with the pointer to the next page of data provided by the {@code 
dataPointer}
+   * function.
+   *
+   * @param request defines pagination parameters that were uses to produce 
this page of data.
+   * @param items stream of source data
+   * @param mapper converter from source data types to response data types.
+   * @param dataPointer determines the internal pointer to the next page of 
data given the last item
+   *     from the previous page. The output of this function will be available 
from {@link
+   *     PageToken#encodedDataReference()} associated with the request for the 
next page.
+   */
+  public static <R, T> Page<R> mapped(
+      PageToken request, Stream<T> items, Function<T, R> mapper, Function<T, 
String> dataPointer) {
+    List<R> data;
+    if (request.paginationRequested()) {
+      data = new ArrayList<>(request.pageSize());
+    } else {
+      data = new ArrayList<>();
+    }
+
+    T last = null;
+    Iterator<T> it = iterator(items.spliterator());
+    while (it.hasNext() && (!request.paginationRequested() || data.size() < 
request.pageSize())) {
+      last = it.next();
+      data.add(mapper.apply(last));
+    }
+
+    if (request.paginationRequested() && data.size() < request.pageSize()) {
+      // the page was not filled, inform the client that there's no next page
+      last = null;
+    }
+
+    return new Page<>(request, dataPointer.apply(last), data);
+  }
+
+  public List<T> items() {
+    return items;
+  }
+
+  /**
+   * Returns a page token in encoded form suitable for returning to API 
clients. The string returned
+   * from this method is expected to be parsed by {@link 
PageToken#decode(String, Integer)} when
+   * servicing the request for the next page of related data.
+   */
+  public @Nullable String encodedResponseToken() {
+    return PageTokenUtil.encodePageToken(request, encodedDataReference);

Review Comment:
   Calling `PageTokenUtil.encodePageToken(Page)` from a place that has a 
reference to a `Page` is less elegant than calling 
`Page.encodedResponseToken()`... This is a matter of opinion, of course :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to