Package: QueryResultSpliterator
QueryResultSpliterator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
QueryResultSpliterator(Spliterator, Function, Procedure) |
|
|
|
|
|
||||||||||||||||||||
forEachRemaining(Consumer) |
|
|
|
|
|
||||||||||||||||||||
lambda$forEachRemaining$1(Consumer, ResultRow) |
|
|
|
|
|
||||||||||||||||||||
lambda$tryAdvance$0(Consumer, ResultRow) |
|
|
|
|
|
||||||||||||||||||||
mapAndApply(ResultRow, Consumer) |
|
|
|
|
|
||||||||||||||||||||
tryAdvance(Consumer) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2020 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.jopa.model;
16:
17: import cz.cvut.kbss.jopa.utils.Procedure;
18: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
19:
20: import java.util.Optional;
21: import java.util.Spliterator;
22: import java.util.Spliterators;
23: import java.util.function.Consumer;
24: import java.util.function.Function;
25:
26: /**
27: * Spliterator for processing {@link cz.cvut.kbss.ontodriver.ResultSet} from {@link cz.cvut.kbss.jopa.model.query.Query} or
28: * {@link cz.cvut.kbss.jopa.model.query.TypedQuery} stream support.
29: * <p>
30: * The main responsibilities of this spliterator are extracting result rows using the specified mapper,
31: * passing the extraction result to the specified consumer and invoking the {@code onClose} handler once the iteration is finished.
32: * This handler releases the underlying statement and result set.
33: *
34: * @param <X> The type of the extracted item
35: */
36: class QueryResultSpliterator<X> extends Spliterators.AbstractSpliterator<X> {
37:
38: private final Spliterator<ResultRow> resultSetSpliterator;
39: private final Function<ResultRow, Optional<X>> mapper;
40: private final Procedure onClose;
41:
42: QueryResultSpliterator(Spliterator<ResultRow> resultSetSpliterator, Function<ResultRow, Optional<X>> mapper,
43: Procedure onClose) {
44: super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.NONNULL);
45: this.resultSetSpliterator = resultSetSpliterator;
46: this.mapper = mapper;
47: this.onClose = onClose;
48: }
49:
50: private void mapAndApply(ResultRow row, Consumer<? super X> action) {
51: mapper.apply(row).ifPresent(action);
52: }
53:
54: @Override
55: public boolean tryAdvance(Consumer<? super X> action) {
56: try {
57: final boolean result = resultSetSpliterator.tryAdvance(row -> mapAndApply(row, action));
58:• if (!result) {
59: onClose.execute();
60: }
61: return result;
62: } catch (RuntimeException e) {
63: onClose.execute();
64: throw e;
65: }
66: }
67:
68: @Override
69: public void forEachRemaining(Consumer<? super X> action) {
70: try {
71: resultSetSpliterator.forEachRemaining(row -> mapAndApply(row, action));
72: } finally {
73: onClose.execute();
74: }
75: }
76: }