Skip to content

Commit

Permalink
Add more laziness to PagedList
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Mar 19, 2016
1 parent f4307d2 commit 689ff03
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,31 @@ public PagedList(Page<E> page) {
* @throws CloudException thrown if an error is raised from Azure.
* @throws IOException thrown if there's any failure in deserialization.
*/
public abstract Page<E> loadPage(String nextPageLink) throws CloudException, IOException;
public abstract Page<E> nextPage(String nextPageLink) throws CloudException, IOException;

public boolean hasNextPage() {
return this.nextPageLink != null;
}

public void loadNextPage() {
try {
Page<E> nextPage = nextPage(this.nextPageLink);
this.nextPageLink = nextPage.getNextPageLink();
this.items.addAll(nextPage.getItems());
} catch (CloudException e) {
throw new WebServiceException(e.toString(), e);
} catch (IOException e) {
throw new DataBindingException(e.getMessage(), e);
}

}

/**
* Keep loading the next page from the next page link until all items are loaded.
*/
public void loadAll() {
while (nextPageLink != null) {
try {
Page<E> nextPage = loadPage(nextPageLink);
nextPageLink = nextPage.getNextPageLink();
addAll(nextPage.getItems());
} catch (CloudException e) {
throw new WebServiceException(e.toString(), e);
} catch (IOException e) {
throw new DataBindingException(e.getMessage(), e);
}
while (hasNextPage()) {
loadNextPage();
}
}

Expand Down Expand Up @@ -90,20 +99,12 @@ public boolean hasNext() {
@Override
public E next() {
if (!itemsListItr.hasNext()) {
if (nextPageLink == null) {
if (!hasNextPage()) {
throw new NoSuchElementException();
} else {
try {
Page<E> nextPage = loadPage(nextPageLink);
nextPageLink = nextPage.getNextPageLink();
int size = items.size();
addAll(nextPage.getItems());
itemsListItr = items.listIterator(size);
} catch (CloudException e) {
throw new WebServiceException(e.toString(), e);
} catch (IOException e) {
throw new DataBindingException(e.getMessage(), e);
}
int size = items.size();
loadNextPage();
itemsListItr = items.listIterator(size);
}
}
return itemsListItr.next();
Expand Down Expand Up @@ -158,8 +159,7 @@ public boolean isEmpty() {

@Override
public boolean contains(Object o) {
loadAll();
return items.contains(o);
return indexOf(o) >= 0;
}

@Override
Expand Down Expand Up @@ -191,7 +191,12 @@ public boolean remove(Object o) {

@Override
public boolean containsAll(Collection<?> c) {
return items.containsAll(c);
for (Object e : c) {
if (!contains(e)) {
return false;
}
}
return true;
}

@Override
Expand Down Expand Up @@ -221,16 +226,8 @@ public void clear() {

@Override
public E get(int index) {
while (index >= items.size() && nextPageLink != null) {
try {
Page<E> nextPage = loadPage(nextPageLink);
nextPageLink = nextPage.getNextPageLink();
addAll(nextPage.getItems());
} catch (CloudException e) {
throw new WebServiceException(e.toString(), e);
} catch (IOException e) {
throw new DataBindingException(e.getMessage(), e);
}
while (index >= items.size() && hasNextPage()) {
loadNextPage();
}
return items.get(index);
}
Expand All @@ -252,11 +249,28 @@ public E remove(int index) {

@Override
public int indexOf(Object o) {
return items.indexOf(o);
int index = 0;
if (o == null) {
for (E item : this) {
if (item == null) {
return index;
}
++index;
}
} else {
for (E item : this) {
if (item == o) {
return index;
}
++index;
}
}
return -1;
}

@Override
public int lastIndexOf(Object o) {
loadAll();
return items.lastIndexOf(o);
}

Expand All @@ -267,11 +281,19 @@ public ListIterator<E> listIterator() {

@Override
public ListIterator<E> listIterator(int index) {
while (index >= items.size() && hasNextPage()) {
loadNextPage();
}
return new ListItr(index);
}

@Override
public List<E> subList(int fromIndex, int toIndex) {
while ((fromIndex >= items.size()
|| toIndex >= items.size())
&& hasNextPage()) {
loadNextPage();
}
return items.subList(fromIndex, toIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PagedListTests {
Expand All @@ -22,7 +23,7 @@ public class PagedListTests {
public void setupList() {
list = new PagedList<Integer>(new TestPage(0, 20)) {
@Override
public Page<Integer> loadPage(String nextPageLink) throws CloudException, IOException {
public Page<Integer> nextPage(String nextPageLink) throws CloudException, IOException {
int pageNum = Integer.parseInt(nextPageLink);
return new TestPage(pageNum, 20);
}
Expand Down Expand Up @@ -64,6 +65,41 @@ public void addTest() {
Assert.assertEquals(19, (int) list.get(20));
}

@Test
public void containsTest() {
Assert.assertTrue(list.contains(0));
Assert.assertTrue(list.contains(3));
Assert.assertTrue(list.contains(19));
Assert.assertFalse(list.contains(20));
}

@Test
public void containsAllTest() {
List<Integer> subList = new ArrayList<>();
subList.addAll(Arrays.asList(0, 3, 19));
Assert.assertTrue(list.containsAll(subList));
subList.add(20);
Assert.assertFalse(list.containsAll(subList));
}

@Test
public void subListTest() {
List<Integer> subList = list.subList(5, 15);
Assert.assertEquals(10, subList.size());
Assert.assertTrue(list.containsAll(subList));
Assert.assertEquals(7, (int) subList.get(2));
}

@Test
public void testIndexOf() {
Assert.assertEquals(15, list.indexOf(15));
}

@Test
public void testLastIndexOf() {
Assert.assertEquals(15, list.lastIndexOf(15));
}

public static class TestPage implements Page<Integer> {
private int page;
private int max;
Expand Down

0 comments on commit 689ff03

Please sign in to comment.