io.codemonastery.jdbj.ResultSetSpliterator Maven / Gradle / Ivy
Show all versions of jdbj Show documentation
package io.codemonastery.jdbj;
import io.codemonastery.jdbj.lambda.ResultMapper;
import javax.annotation.Nullable;
import java.sql.ResultSet;
import java.util.Spliterator;
import java.util.function.Consumer;
/**
* Wrapper around {@link ResultSet} and {@link ResultMapper} to create a {@link Spliterator} or {@link E}.
*
* Does not close the {@link ResultSet}.
* @param element type
* @see StreamQuery
*/
public class ResultSetSpliterator implements Spliterator {
private final SmartResultSet rs;
private final ResultMapper mapper;
public ResultSetSpliterator(ResultSet rs, ResultMapper mapper) {
this.rs = new SmartResultSet(rs);
this.mapper = mapper;
}
@Override
public boolean tryAdvance(Consumer super E> action) {
try {
final boolean advanced = rs.next();
if (advanced) {
action.accept(mapper.map(rs));
}
return advanced;
} catch (Exception e) {
throw new AdvanceFailedException(e);
}
}
@Override
public @Nullable Spliterator trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return Spliterator.IMMUTABLE;
}
}