com.jnape.palatable.lambda.iterators.TakingIterator Maven / Gradle / Ivy
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