Search in sources :

Example 1 with PageParameters

use of org.jboss.pnc.rest.api.parameters.PageParameters in project pnc by project-ncl.

the class RemoteCollectionTest method shouldIterateOverPages.

@Test
public void shouldIterateOverPages() throws Exception {
    AtomicInteger index = new AtomicInteger();
    Function<PageParameters, Page<Entity>> endpoint = (parameters) -> {
        Collection<Entity> collection = new ArrayList();
        collection.add(new Entity(index.getAndIncrement()));
        collection.add(new Entity(index.getAndIncrement()));
        return new Page<>(parameters.getPageIndex(), parameters.getPageSize(), 3, 6, collection);
    };
    RemoteCollectionConfig config = RemoteCollectionConfig.builder().pageSize(2).build();
    RemoteCollection<Entity> collection = new DefaultRemoteCollection<>(endpoint, config);
    Iterator<Entity> iterator = collection.iterator();
    List<Entity> collected = new ArrayList<>();
    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        System.out.println("Received: " + entity.id);
        collected.add(entity);
        if (collected.size() > 10) {
            throw new Exception("Infinite loop.");
        }
    }
    Assert.assertEquals(6, collected.size());
    Assert.assertEquals(5, collected.get(5).id);
}
Also used : List(java.util.List) Iterator(java.util.Iterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Test(org.junit.Test) PageParameters(org.jboss.pnc.rest.api.parameters.PageParameters) Assert(org.junit.Assert) Function(java.util.function.Function) Page(org.jboss.pnc.dto.response.Page) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Page(org.jboss.pnc.dto.response.Page) PageParameters(org.jboss.pnc.rest.api.parameters.PageParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Test(org.junit.Test)

Example 2 with PageParameters

use of org.jboss.pnc.rest.api.parameters.PageParameters in project pnc by project-ncl.

the class DefaultRemoteCollection method loadNextPage.

private Page<T> loadNextPage(Function<PageParameters, Page<T>> endpoint, Page<T> currentPage) {
    int newPageIndex = currentPage.getPageIndex() + 1;
    logger.debug("Loading new page. Index {}", newPageIndex);
    PageParameters pageParametersNext = new PageParameters();
    pageParametersNext.setPageSize(currentPage.getPageSize());
    pageParametersNext.setPageIndex(newPageIndex);
    return endpoint.apply(pageParametersNext);
}
Also used : PageParameters(org.jboss.pnc.rest.api.parameters.PageParameters)

Aggregations

PageParameters (org.jboss.pnc.rest.api.parameters.PageParameters)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1 Page (org.jboss.pnc.dto.response.Page)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1