Skip to content

Method: forEachRemaining(Consumer)

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