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

pl.chilldev.commons.collections.pageable.PageableIterator Maven / Gradle / Ivy

The newest version!
/**
 * This file is part of the ChillDev-Commons.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2015 © by Rafał Wrzeszcz - Wrzasq.pl.
 */

package pl.chilldev.commons.collections.pageable;

import java.util.Iterator;
import java.util.function.Function;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

/**
 * Collection class representing paged resource.
 *
 * @param  Collection element type.
 */
public class PageableIterator implements Iterator
{
    /**
     * Current result page.
     */
    private Slice page;

    /**
     * Current page data.
     */
    private Iterator data;

    /**
     * Data source for paged results.
     */
    private Function> source;

    /**
     * Initializes iterator with given starting point.
     *
     * @param page Initial page.
     * @param source Paged results data source.
     */
    public PageableIterator(Slice page, Function> source)
    {
        this.source = source;

        this.handlePage(page);
    }

    /**
     * Sets current page data.
     *
     * @param page Current page.
     */
    private void handlePage(Slice page)
    {
        this.page = page;
        this.data = page.getContent().iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasNext()
    {
        return this.data.hasNext() || this.page.hasNext();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Type next()
    {
        // if there is no more data in current page move to next one
        if (!this.data.hasNext()) {
            this.handlePage(this.source.apply(this.page.nextPageable()));
        }

        return this.data.next();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy