Skip to contentMethod: forEachRemaining(Consumer)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.iteration;
16:
17: import cz.cvut.kbss.ontodriver.ResultSet;
18: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverRuntimeException;
20:
21: import java.util.Objects;
22: import java.util.Spliterator;
23: import java.util.Spliterators;
24: import java.util.function.Consumer;
25:
26: /**
27: * {@link Spliterator} implementation for a {@link ResultSet}.
28: * <p>
29: * Note that the methods wrap {@link OntoDriverException}s possibly thrown by the underlying result set in a {@link OntoDriverRuntimeException}
30: * in order to support the {@link Spliterator} API.
31: */
32: public class ResultSetSpliterator extends Spliterators.AbstractSpliterator<ResultRow> {
33:
34: private final ResultSet resultSet;
35:
36: /**
37: * Creates a spliterator reporting unknown estimate size and the following characteristics:
38: * <ul>
39: * <li>{@link Spliterator#IMMUTABLE}</li>
40: * <li>{@link Spliterator#ORDERED}</li>
41: * <li>{@link Spliterator#NONNULL}</li>
42: * </ul>
43: *
44: * @param resultSet {@code ResultSet} on which the iteration will be performed
45: */
46: public ResultSetSpliterator(ResultSet resultSet) {
47: super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.NONNULL);
48: this.resultSet = Objects.requireNonNull(resultSet);
49: }
50:
51: @Override
52: public boolean tryAdvance(Consumer<? super ResultRow> action) {
53: Objects.requireNonNull(action);
54: try {
55: if (resultSet.hasNext()) {
56: resultSet.next();
57: action.accept(new DelegatingResultRow(resultSet));
58: return true;
59: } else {
60: return false;
61: }
62: } catch (OntoDriverException e) {
63: throw new OntoDriverRuntimeException(e);
64: }
65: }
66:
67: @Override
68: public void forEachRemaining(Consumer<? super ResultRow> action) {
69: Objects.requireNonNull(action);
70: try {
71: // Row is just a view on the result set which does the actual iteration
72: final DelegatingResultRow row = new DelegatingResultRow(resultSet);
73:• while (resultSet.hasNext()) {
74: resultSet.next();
75: action.accept(row);
76: }
77: } catch (OntoDriverException e) {
78: throw new OntoDriverRuntimeException(e);
79: }
80: }
81: }