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 cdd38a5ca3a1..a2b9b67933aa 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 @@ -67,35 +67,38 @@ public void loadAll() { } /** - * The implementation of {@link Iterator} for PagedList. + * The implementation of {@link ListIterator} for PagedList. */ - private class Itr implements Iterator { - /** The iterator for the actual list of items. */ - private Iterator itemsItr; + private class ListItr implements ListIterator { + /** The list iterator for the actual list of items. */ + private ListIterator itemsListItr; /** - * Creates an instance of the iterator. + * Creates an instance of the ListIterator. + * + * @param index the position in the list to start. */ - public Itr() { - itemsItr = items.iterator(); + public ListItr(int index) { + itemsListItr = items.listIterator(index); } @Override public boolean hasNext() { - return itemsItr.hasNext() || nextPageLink != null; + return itemsListItr.hasNext() || nextPageLink != null; } @Override public E next() { - if (!itemsItr.hasNext()) { + if (!itemsListItr.hasNext()) { if (nextPageLink == null) { throw new NoSuchElementException(); } else { try { Page nextPage = loadPage(nextPageLink); nextPageLink = nextPage.getNextPageLink(); + int size = items.size(); addAll(nextPage.getItems()); - itemsItr = items.iterator(); + itemsListItr = items.listIterator(size); } catch (CloudException e) { throw new WebServiceException(e.toString(), e); } catch (IOException e) { @@ -103,29 +106,12 @@ public E next() { } } } - return itemsItr.next(); + return itemsListItr.next(); } @Override public void remove() { - itemsItr.remove(); - } - } - - /** - * The implementation of {@link ListIterator} for PagedList. - */ - private class ListItr extends Itr implements ListIterator { - /** The list iterator for the actual list of items. */ - private ListIterator itemsListItr; - - /** - * Creates an instance of the ListIterator. - * - * @param index the position in the list to start. - */ - public ListItr(int index) { - itemsListItr = items.listIterator(index); + itemsListItr.remove(); } @Override @@ -178,7 +164,7 @@ public boolean contains(Object o) { @Override public Iterator iterator() { - return new Itr(); + return new ListItr(0); } @Override 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 new file mode 100644 index 000000000000..6ddc0c61ea4a --- /dev/null +++ b/azure-client-runtime/src/test/java/com/microsoft/azure/PagedListTests.java @@ -0,0 +1,91 @@ +/** + * + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * + */ + +package com.microsoft.azure; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class PagedListTests { + private PagedList list; + + @Before + public void setupList() { + list = new PagedList(new TestPage(0, 20)) { + @Override + public Page loadPage(String nextPageLink) throws CloudException, IOException { + int pageNum = Integer.parseInt(nextPageLink); + return new TestPage(pageNum, 20); + } + }; + } + + @Test + public void sizeTest() { + Assert.assertEquals(20, list.size()); + } + + @Test + public void getTest() { + Assert.assertEquals(15, (int) list.get(15)); + } + + @Test + public void iterateTest() { + int j = 0; + for (int i : list) { + Assert.assertEquals(i, j++); + } + } + + @Test + public void removeTest() { + Integer i = list.get(10); + list.remove(10); + Assert.assertEquals(19, list.size()); + Assert.assertEquals(19, (int) list.get(18)); + } + + @Test + public void addTest() { + Integer i = list.get(10); + list.add(100); + Assert.assertEquals(21, list.size()); + Assert.assertEquals(100, (int) list.get(11)); + Assert.assertEquals(19, (int) list.get(20)); + } + + public static class TestPage implements Page { + private int page; + private int max; + + public TestPage(int page, int max) { + this.page = page; + this.max = max; + } + + @Override + public String getNextPageLink() { + if (page + 1 == max) { + return null; + } + return Integer.toString(page + 1); + } + + @Override + public List getItems() { + List items = new ArrayList<>(); + items.add(page); + return items; + } + } +}