Java - Creating a Paginated List
spring has the concept of paging and they do something like this:

public Page findAll(Pageable pageable);


where the Page interface has the following methods (trimmed):

getTotalElements()
getTotalPages()
isFirst()
isLast()
nextPageable()
previousPageable()


it also extends the iterable interface so you can loop like this:

Library library = new Library();
Pageable pageRequest = ...
Page&lt;T&gt; page;
do {
  page = library.getData(pageRequest);
  for (T t: page) {
    log.debug("T is {}", t);
  }
  pageRequest = page.nextPageable();
} while( !page.isLast() );


the most notable difference is that the page being requested is not stored as a state in the client library. by storing the state you make it harder to use the same instance from multiple places in the code.

see http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/paging-chapter.html