Freigeben über


PagedIterable<T> Class

Type Parameters

T

The type of value contained in this IterableStream.

public class PagedIterable
extends PagedIterableBase<T,PagedResponse<T>>

This class provides utility to iterate over PagedResponse<T> using Stream and Iterable interfaces.

Code sample using Stream by page

// process the streamByPage
 pagedIterableResponse.streamByPage().forEach(resp -> {
     System.out.printf("Response headers are %s. Url %s  and status code %d %n", resp.getHeaders(),
         resp.getRequest().getUrl(), resp.getStatusCode());
     resp.getElements().forEach(value -> System.out.printf("Response value is %d %n", value));
 });

Code sample using Iterable by page

// process the iterableByPage
 pagedIterableResponse.iterableByPage().forEach(resp -> {
     System.out.printf("Response headers are %s. Url %s  and status code %d %n", resp.getHeaders(),
         resp.getRequest().getUrl(), resp.getStatusCode());
     resp.getElements().forEach(value -> System.out.printf("Response value is %d %n", value));
 });

Code sample using Iterable by page and while loop

// iterate over each page
 for (PagedResponse<Integer> resp : pagedIterableResponse.iterableByPage()) {
     System.out.printf("Response headers are %s. Url %s  and status code %d %n", resp.getHeaders(),
         resp.getRequest().getUrl(), resp.getStatusCode());
     resp.getElements().forEach(value -> System.out.printf("Response value is %d %n", value));
 }

Code sample using Iterable by page and continuation token

String continuationToken = getContinuationToken();
 pagedIterable
     .iterableByPage(continuationToken)
     .forEach(page -> System.out.printf("Processing page containing item values: %s%n",
         page.getElements().stream().map(String::valueOf).collect(Collectors.joining(", "))));

Constructor Summary

Constructor Description
PagedIterable(PagedFlux<T> pagedFlux)

Creates instance given PagedFlux<T>.

PagedIterable(Function<Integer,PagedResponse<T>> firstPageRetriever)

Creates an instance of PagedIterable<T> that consists of only a single page with a given element count.

PagedIterable(Function<Integer,PagedResponse<T>> firstPageRetriever, BiFunction<String,Integer,PagedResponse<T>> nextPageRetriever)

Creates an instance of PagedIterable<T> that is capable of retrieving multiple pages with of a given page size.

PagedIterable(Supplier<PagedResponse<T>> firstPageRetriever)

Creates an instance of PagedIterable<T> that consists of only a single page.

PagedIterable(Supplier<PagedResponse<T>> firstPageRetriever, Function<String,PagedResponse<T>> nextPageRetriever)

Creates an instance of PagedIterable<T>.

Method Summary

Modifier and Type Method and Description
PagedIterable<S> mapPage(Function<T,S> mapper)

Maps this PagedIterable instance of T to a PagedIterable instance of type S as per the provided mapper function.

Methods inherited from IterableStream

Methods inherited from ContinuablePagedIterable

Methods inherited from java.lang.Object

Constructor Details

PagedIterable

public PagedIterable(PagedFlux pagedFlux)

Creates instance given PagedFlux<T>.

Parameters:

pagedFlux - to use as iterable

PagedIterable

public PagedIterable(Function> firstPageRetriever)

Creates an instance of PagedIterable<T> that consists of only a single page with a given element count.

Code sample

// A function that fetches the single page of data from a source/service.
 Function<Integer, Mono<PagedResponse<Integer>>> singlePageRetriever = pageSize ->
     getFirstPageWithSize(pageSize);

 PagedFlux<Integer> singlePageFluxWithPageSize = new PagedFlux<Integer>(singlePageRetriever);

Parameters:

firstPageRetriever - Function that retrieves the first page.

PagedIterable

public PagedIterable(Function> firstPageRetriever, BiFunction> nextPageRetriever)

Creates an instance of PagedIterable<T> that is capable of retrieving multiple pages with of a given page size.

Code sample

// A function that fetches the first page of data from a source/service.
 Function<Integer, PagedResponse<Integer>> firstPageRetriever = pageSize -> getPage(pageSize);

 // A function that fetches subsequent pages of data from a source/service given a continuation token.
 BiFunction<String, Integer, PagedResponse<Integer>> nextPageRetriever = (continuationToken, pageSize) ->
     getPage(continuationToken, pageSize);

 PagedIterable<Integer> pagedIterableWithPageSize = new PagedIterable<>(firstPageRetriever, nextPageRetriever);

Parameters:

firstPageRetriever - Function that retrieves the first page.
nextPageRetriever - BiFunction that retrieves the next page given a continuation token and page size.

PagedIterable

public PagedIterable(Supplier> firstPageRetriever)

Creates an instance of PagedIterable<T> that consists of only a single page. This constructor takes a Supplier that return the single page of T.

Code sample

// A supplier that fetches the first page of data from source/service
 Supplier<PagedResponse<Integer>> firstPageRetrieverFunction = () -> getFirstPage();

 PagedIterable<Integer> pagedIterableInstance = new PagedIterable<>(firstPageRetrieverFunction,
     nextPageRetriever);

Parameters:

firstPageRetriever - Supplier that retrieves the first page.

PagedIterable

public PagedIterable(Supplier> firstPageRetriever, Function> nextPageRetriever)

Creates an instance of PagedIterable<T>. The constructor takes a Supplier and Function. The Supplier returns the first page of T, the Function retrieves subsequent pages of T.

Code sample

// A supplier that fetches the first page of data from source/service
 Supplier<PagedResponse<Integer>> firstPageRetriever = () -> getFirstPage();

 // A function that fetches subsequent pages of data from source/service given a continuation token
 Function<String, PagedResponse<Integer>> nextPageRetriever =
     continuationToken -> getNextPage(continuationToken);

 PagedIterable<Integer> pagedIterable = new PagedIterable<>(firstPageRetriever,
     nextPageRetriever);

Parameters:

firstPageRetriever - Supplier that retrieves the first page
nextPageRetriever - Function that retrieves the next page given a continuation token

Method Details

mapPage

public PagedIterable mapPage(Function mapper)

Maps this PagedIterable instance of T to a PagedIterable instance of type S as per the provided mapper function.

Parameters:

mapper - The mapper function to convert from type T to type S.

Returns:

A PagedIterable of type S.

Applies to