All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.braintreegateway.PaginatedCollection Maven / Gradle / Ivy

package com.braintreegateway;

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

/**
 * A collection used to page through results.
 *
 * @param 
 *            type of object being paged, e.g. {@link MerchantAccount}
 */
public class PaginatedCollection implements Iterable {

    private class PagedIterator implements Iterator {
        private PaginatedCollection paginatedCollection;
        private int pageSize;
        private int currentPage;
        private int index;
        private int totalSize;
        private List items;

        public PagedIterator(PaginatedCollection paginatedCollection) {
            this.paginatedCollection = paginatedCollection;
            this.pageSize = 0;
            this.currentPage = 0;
            this.index = 0;
            this.totalSize = 0;
            this.items = new ArrayList();
        }

        public boolean hasNext() {
            if (currentPage == 0 || this.index % this.pageSize == 0 && this.index < this.totalSize) {
                this.currentPage++;
                PaginatedResult results = paginatedCollection.pager.getPage(this.currentPage);
                this.totalSize = results.getTotalItems();
                this.items = results.getCurrentPage();
                this.pageSize = results.getPageSize();
            }

            return this.index < this.totalSize;
        }

        public E next() {
            E item = items.get(index % this.pageSize);
            index++;
            return item;
        }

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

    private SimplePager pager;
    private int pageSize;

    public PaginatedCollection(SimplePager pager) {
        this.pager = pager;
    }

    public Iterator iterator() {
        return new PagedIterator(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy