package org.sagebionetworks.bridge.services.backfill;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.account.AccountCriteria;
import com.stormpath.sdk.account.AccountList;
import com.stormpath.sdk.application.Application;
import com.stormpath.sdk.impl.account.DefaultAccountCriteria;

class StormpathAccountIterator implements Iterator<List<Account>> {

    private static final int PAGE_SIZE = 50;
    private final Application app;
    private int offset;
    private boolean hasNext;

    StormpathAccountIterator(Application app) {
        this.app = app;
        offset = 0;
        hasNext = true;
    }

    @Override
    public boolean hasNext() {
        return hasNext;
    }

    @Override
    public List<Account> next() {
        AccountCriteria criteria = new DefaultAccountCriteria();
        criteria.offsetBy(offset);
        criteria.limitTo(PAGE_SIZE);
        AccountList list = app.getAccounts(criteria);
        Iterator<Account> iterator = list.iterator();
        List<Account> accountList = new ArrayList<Account>();
        while (iterator.hasNext()) {
            accountList.add(iterator.next());
        }
        hasNext = accountList.size() == PAGE_SIZE;
        offset = offset + PAGE_SIZE;
        return accountList;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

@@ -69,67 +69,16 @@
 til.
-ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List
+Iterator
 ;%0A%0Ai
@@ -327,16 +327,77 @@
 teria;%0A%0A
+/**%0A * Iterates through Stormpath accounts page by page.%0A */%0A
 class St
@@ -423,19 +423,20 @@
 tor 
-implement
+extend
 s 
+Page
 Iter
@@ -440,29 +440,24 @@
 terator%3C
-List%3C
 Account%3E
 %3E %7B%0A%0A   
@@ -448,17 +448,16 @@
 Account%3E
-%3E
  %7B%0A%0A    
@@ -535,61 +535,8 @@
 app;
-%0A    private int offset;%0A    private boolean hasNext;
 %0A%0A  
@@ -613,95 +613,41 @@
     
-    offset = 0;%0A        hasNext = true;%0A    %7D%0A%0A    @Override%0A    public boolean hasNext
+%7D%0A%0A    @Override%0A    int pageSize
 () %7B
@@ -662,23 +662,25 @@
  return 
-hasNext
+PAGE_SIZE
 ;%0A    %7D%0A
@@ -702,19 +702,16 @@
     
-public List
+Iterator
 %3CAcc
@@ -720,16 +720,20 @@
 nt%3E next
+Page
 () %7B%0A   
@@ -820,22 +820,27 @@
 ffsetBy(
-offset
+pageStart()
 );%0A     
@@ -859,25 +859,26 @@
 limitTo(
-PAGE_SIZE
+pageSize()
 );%0A     
@@ -938,420 +938,28 @@
     
-Iterator%3CAccount%3E iterator = list.iterator();%0A        List%3CAccount%3E accountList = new ArrayList%3CAccount%3E();%0A        while (iterator.hasNext()) %7B%0A            accountList.add(iterator.next());%0A        %7D%0A        hasNext = accountList.size() == PAGE_SIZE;%0A        offset = offset + PAGE_SIZE;%0A        return accountList;%0A    %7D%0A%0A    @Override%0A    public void remove() %7B%0A        throw new UnsupportedOperationException
+return list.iterator
 ();%0A

Test the paginated Stormpath account iterator