com.jnape.palatable.lambda.iterators.TakingIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda Show documentation
Show all versions of lambda Show documentation
Functional patterns for Java
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