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

ru.vyarus.yaml.updater.parse.comments.util.CountingIterator Maven / Gradle / Ivy

The newest version!
package ru.vyarus.yaml.updater.parse.comments.util;

import java.util.Iterator;

/**
 * Iterator, remembering processed items count.
 *
 * @param  value type
 * @author Vyacheslav Rusakov
 * @since 23.04.2021
 */
public class CountingIterator implements Iterator {

    private final Iterator it;
    private int pos;

    public CountingIterator(final Iterator it) {
        this.it = it;
    }

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

    @Override
    public T next() {
        final T res = it.next();
        // increment after to not count invalid attempts at the end
        pos++;
        return res;
    }

    /**
     * @return current item position, starting from 0
     */
    public int getPosition() {
        return pos;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy