org.aksw.commons.collections.IteratorUtils Maven / Gradle / Ivy
package org.aksw.commons.collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
/**
* Created by Claus Stadler
* Date: Nov 1, 2010
* Time: 10:22:25 PM
*/
public class IteratorUtils {
public static Iterable makeIterable(Iterator iterator) {
return new IteratorIterable(iterator);
}
public static T expectOneItem(Iterator iterator) {
T result = null;
if (!iterator.hasNext()) {
throw new IllegalArgumentException("Exactly one item expected in stream; got none");
}
result = iterator.next();
if (iterator.hasNext()) {
throw new IllegalArgumentException("Exactly one item expected in stream; got multiple");
}
return result;
}
public static T expectZeroOrOneItems(Iterator iterator) {
T result;
if (!iterator.hasNext()) {
result = null;
} else {
result = expectOneItem(iterator);
}
return result;
}
/** Similar to guava's Iterators.limit but with long argument*/
public static Iterator limit(Iterator iterator, long limitSize) {
Objects.requireNonNull(iterator);
return new Iterator() {
protected long count;
@Override
public boolean hasNext() {
return count < limitSize && iterator.hasNext();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
count++;
return iterator.next();
}
@Override
public void remove() {
iterator.remove();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy