From 689ff03d80f8bbe03bb27e85baf2dabf18ac4695 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Fri, 18 Mar 2016 17:35:46 -0700 Subject: [PATCH] Add more laziness to PagedList --- .../java/com/microsoft/azure/PagedList.java | 96 ++++++++++++------- .../com/microsoft/azure/PagedListTests.java | 38 +++++++- 2 files changed, 96 insertions(+), 38 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java b/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java index a2b9b67933aa2..5f1676ff2b68c 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/PagedList.java @@ -47,22 +47,31 @@ public PagedList(Page page) { * @throws CloudException thrown if an error is raised from Azure. * @throws IOException thrown if there's any failure in deserialization. */ - public abstract Page loadPage(String nextPageLink) throws CloudException, IOException; + public abstract Page nextPage(String nextPageLink) throws CloudException, IOException; + + public boolean hasNextPage() { + return this.nextPageLink != null; + } + + public void loadNextPage() { + try { + Page 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 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(); } } @@ -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 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(); @@ -158,8 +159,7 @@ public boolean isEmpty() { @Override public boolean contains(Object o) { - loadAll(); - return items.contains(o); + return indexOf(o) >= 0; } @Override @@ -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 @@ -221,16 +226,8 @@ public void clear() { @Override public E get(int index) { - while (index >= items.size() && nextPageLink != null) { - try { - Page 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); } @@ -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); } @@ -267,11 +281,19 @@ public ListIterator listIterator() { @Override public ListIterator listIterator(int index) { + while (index >= items.size() && hasNextPage()) { + loadNextPage(); + } return new ListItr(index); } @Override public List subList(int fromIndex, int toIndex) { + while ((fromIndex >= items.size() + || toIndex >= items.size()) + && hasNextPage()) { + loadNextPage(); + } return items.subList(fromIndex, toIndex); } } diff --git a/azure-client-runtime/src/test/java/com/microsoft/azure/PagedListTests.java b/azure-client-runtime/src/test/java/com/microsoft/azure/PagedListTests.java index 6ddc0c61ea4af..b82c8d0628787 100644 --- a/azure-client-runtime/src/test/java/com/microsoft/azure/PagedListTests.java +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/PagedListTests.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class PagedListTests { @@ -22,7 +23,7 @@ public class PagedListTests { public void setupList() { list = new PagedList(new TestPage(0, 20)) { @Override - public Page loadPage(String nextPageLink) throws CloudException, IOException { + public Page nextPage(String nextPageLink) throws CloudException, IOException { int pageNum = Integer.parseInt(nextPageLink); return new TestPage(pageNum, 20); } @@ -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 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 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 { private int page; private int max;