Skip to content

Commit

Permalink
Allow empty pages in paged list
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Sep 13, 2016
1 parent 7b325e2 commit bc575ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.util.ListIterator;
import java.util.NoSuchElementException;

import javax.xml.bind.DataBindingException;

/**
* Defines a list response from a paging operation. The pages are
* lazy initialized when an instance of this class is iterated.
Expand All @@ -28,10 +26,10 @@
public abstract class PagedList<E> implements List<E> {
/** The actual items in the list. */
private List<E> items;
/** Stores the link to get the next page of items. */
private String nextPageLink;
/** Stores the latest page fetched. */
private Page<E> currentPage;
/** Cached page right after the current one. */
private Page<E> cachedPage;

/**
* Creates an instance of Pagedlist.
Expand All @@ -48,11 +46,26 @@ public PagedList() {
public PagedList(Page<E> page) {
this();
List<E> retrievedItems = page.getItems();
if (retrievedItems != null && retrievedItems.size() != 0) {
if (retrievedItems != null) {
items.addAll(retrievedItems);
}
nextPageLink = page.getNextPageLink();
currentPage = page;
cachePage(page.getNextPageLink());
}

private void cachePage(String nextPageLink) {
try {
while (nextPageLink != null) {
cachedPage = nextPage(nextPageLink);
nextPageLink = cachedPage.getNextPageLink();
if (hasNextPage()) {
// a legit, non-empty page has been fetched, otherwise keep fetching
break;
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

/**
Expand All @@ -71,22 +84,18 @@ public PagedList(Page<E> page) {
* @return true if there are more pages to load. False otherwise.
*/
public boolean hasNextPage() {
return this.nextPageLink != null;
return this.cachedPage != null && this.cachedPage.getItems() != null && !this.cachedPage.getItems().isEmpty();
}

/**
* Loads a page from next page link.
* The exceptions are wrapped into Java Runtime exceptions.
*/
public void loadNextPage() {
try {
Page<E> nextPage = nextPage(this.nextPageLink);
this.nextPageLink = nextPage.getNextPageLink();
this.items.addAll(nextPage.getItems());
this.currentPage = nextPage;
} catch (IOException e) {
throw new DataBindingException(e.getMessage(), e);
}
this.currentPage = cachedPage;
cachedPage = null;
this.items.addAll(currentPage.getItems());
cachePage(currentPage.getNextPageLink());
}

/**
Expand All @@ -107,15 +116,6 @@ public Page<E> currentPage() {
return currentPage;
}

/**
* Gets the next page's link.
*
* @return the next page link.
*/
public String nextPageLink() {
return nextPageLink;
}

/**
* The implementation of {@link ListIterator} for PagedList.
*/
Expand All @@ -141,17 +141,14 @@ public boolean hasNext() {
public E next() {
if (!itemsListItr.hasNext()) {
if (!hasNextPage()) {
throw new NoSuchElementException();
throw new NoSuchElementException();
} else {
int size = items.size();
loadNextPage();
itemsListItr = items.listIterator(size);
}
}
if (itemsListItr.hasNext()) {
return itemsListItr.next();
}
return null;
return itemsListItr.next();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public class PagedListTests {

@Before
public void setupList() {
list = new PagedList<Integer>(new TestPage(0, 20)) {
list = new PagedList<Integer>(new TestPage(0, 21)) {
@Override
public Page<Integer> nextPage(String nextPageLink) throws CloudException, IOException {
int pageNum = Integer.parseInt(nextPageLink);
return new TestPage(pageNum, 20);
return new TestPage(pageNum, 21);
}
};
}
Expand Down Expand Up @@ -119,9 +119,13 @@ public String getNextPageLink() {

@Override
public List<Integer> getItems() {
List<Integer> items = new ArrayList<>();
items.add(page);
return items;
if (page + 1 != max) {
List<Integer> items = new ArrayList<>();
items.add(page);
return items;
} else {
return new ArrayList<>();
}
}
}
}

0 comments on commit bc575ae

Please sign in to comment.