PagingIterator.java 1.07 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.cq.projects.impl.util;

import java.util.Iterator;

public class PagingIterator<E>
implements Iterator<E> {
    private Iterator<E> it;
    private Integer offset;
    private Integer limit;
    private int limitCounter = 0;

    public PagingIterator(Iterator<E> it, Integer offset, Integer limit) {
        this.it = it;
        this.offset = offset;
        this.limit = limit;
        this.doOffset();
    }

    private void doOffset() {
        if (this.offset == null) {
            return;
        }
        for (int i = 0; i < this.offset && this.it.hasNext(); ++i) {
            this.it.next();
        }
    }

    @Override
    public boolean hasNext() {
        if (this.limit == null || this.limit < 0) {
            return this.it.hasNext();
        }
        return this.limitCounter < this.limit && this.it.hasNext();
    }

    @Override
    public E next() {
        E next = this.it.next();
        ++this.limitCounter;
        return next;
    }

    @Override
    public void remove() {
        this.it.remove();
    }
}