org.sfm.utils.ForEachIteratorIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.utils;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ForEachIteratorIterator implements Iterator {
private final ForEachIterator iterator;
private boolean isFetched;
private boolean hasValue;
private T currentValue;
public ForEachIteratorIterator(ForEachIterator iterator) {
this.iterator = iterator;
}
@Override
public boolean hasNext() {
fetch();
return hasValue;
}
private void fetch() {
if (!isFetched) {
try {
doFetch();
} catch (Exception e) {
ErrorHelper.rethrow(e);
}
}
}
private void doFetch() throws Exception {
if (!iterator.next(new RowHandler() {
@Override
public void handle(T t) throws Exception {
currentValue = t;
hasValue = true;
}
})) {
currentValue = null;
hasValue = false;
}
isFetched = true;
}
@Override
public T next() {
fetch();
if (hasValue) {
T v = currentValue;
currentValue = null;
isFetched = false;
return v;
} else {
throw new NoSuchElementException("No more rows");
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}