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

com.jnape.palatable.lambda.iterators.TakingIterator Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
package com.jnape.palatable.lambda.iterators;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class TakingIterator extends ImmutableIterator {

    private final int         n;
    private final Iterator iterator;
    private       int         currentIndex;

    public TakingIterator(int n, Iterator iterator) {
        this.n = n;
        this.iterator = iterator;
        currentIndex = 0;
    }

    @Override
    public boolean hasNext() {
        return currentIndex < n && iterator.hasNext();
    }

    @Override
    public A next() {
        if (currentIndex >= n)
            throw new NoSuchElementException();

        currentIndex++;
        return iterator.next();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy