All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.jdbc.impl.ResultSetSpliterator Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.jdbc.impl;

import org.sfm.jdbc.JdbcMapper;
import org.sfm.map.MappingContext;
import org.sfm.utils.ErrorHelper;
import org.sfm.utils.RowHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Spliterator;
import java.util.function.Consumer;

public class ResultSetSpliterator implements Spliterator {
    private final ResultSet resultSet;
    private final JdbcMapper mapper;
    private final MappingContext mappingContext;

    public ResultSetSpliterator(ResultSet resultSet, JdbcMapper mapper, MappingContext mappingContext) {
        this.resultSet = resultSet;
        this.mapper = mapper;
        this.mappingContext = mappingContext;
    }

    @Override
    public boolean tryAdvance(Consumer action) {
        try {
            if (resultSet.next()) {
                action.accept(mapper.map(resultSet, mappingContext));
                return true;
            }
        } catch (SQLException e) {
            ErrorHelper.rethrow(e);
        }
        return false;
    }

    @Override
    public void forEachRemaining(Consumer action) {
        try {
            mapper.forEach(resultSet, new RowHandler() {
                @Override
                public void handle(T t) throws Exception {
                    action.accept(t);
                }
            });
        } catch (SQLException e) {
            ErrorHelper.rethrow(e);
        }
    }

    @Override
    public Spliterator trySplit() {
        return null;
    }

    @Override
    public long estimateSize() {
        return Long.MAX_VALUE;
    }

    @Override
    public int characteristics() {
        return Spliterator.ORDERED | Spliterator.NONNULL;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy