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

cz.cvut.kbss.ontodriver.iteration.ResultSetSpliterator Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
/**
 * Copyright (C) 2019 Czech Technical University in Prague
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details. You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 */
package cz.cvut.kbss.ontodriver.iteration;

import cz.cvut.kbss.ontodriver.ResultSet;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.exception.OntoDriverRuntimeException;

import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;

/**
 * {@link Spliterator} implementation for a {@link ResultSet}.
 * 

* Note that the methods wrap {@link OntoDriverException}s possibly thrown by the underlying result set in a {@link OntoDriverRuntimeException} * in order to support the {@link Spliterator} API. */ public class ResultSetSpliterator extends Spliterators.AbstractSpliterator { private final ResultSet resultSet; /** * Creates a spliterator reporting unknown estimate size and the following characteristics: *

    *
  • {@link Spliterator#IMMUTABLE}
  • *
  • {@link Spliterator#ORDERED}
  • *
  • {@link Spliterator#NONNULL}
  • *
* * @param resultSet {@code ResultSet} on which the iteration will be performed */ public ResultSetSpliterator(ResultSet resultSet) { super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.NONNULL); this.resultSet = Objects.requireNonNull(resultSet); } @Override public boolean tryAdvance(Consumer action) { Objects.requireNonNull(action); try { if (resultSet.hasNext()) { resultSet.next(); action.accept(new DelegatingResultRow(resultSet)); return true; } else { return false; } } catch (OntoDriverException e) { throw new OntoDriverRuntimeException(e); } } @Override public void forEachRemaining(Consumer action) { Objects.requireNonNull(action); try { // Row is just a view on the result set which does the actual iteration final DelegatingResultRow row = new DelegatingResultRow(resultSet); while (resultSet.hasNext()) { resultSet.next(); action.accept(row); } } catch (OntoDriverException e) { throw new OntoDriverRuntimeException(e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy