Skip to contentMethod: registerObserver(Observer)
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.rdf4j.query;
16:
17: import cz.cvut.kbss.ontodriver.ResultSet;
18: import cz.cvut.kbss.ontodriver.Statement;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20:
21: import java.util.NoSuchElementException;
22: import java.util.Observer;
23:
24: abstract class AbstractResultSet implements ResultSet {
25:
26: private final Statement statement;
27:
28: private int index;
29: private boolean open;
30:
31: AbstractResultSet(Statement statement) {
32: assert statement != null;
33:
34: this.statement = statement;
35: this.index = -1;
36: this.open = true;
37: }
38:
39: @Override
40: public boolean isOpen() {
41: return open;
42: }
43:
44: @Override
45: public int getRowIndex() {
46: ensureOpen();
47: return index;
48: }
49:
50: @Override
51: public Statement getStatement() {
52: ensureOpen();
53: return statement;
54: }
55:
56: @Override
57: public boolean isFirst() {
58: ensureOpen();
59: return index == 0;
60: }
61:
62: void ensureOpen() {
63: if (!open) {
64: throw new IllegalStateException("The result set is closed!");
65: }
66: }
67:
68: @Override
69: public void close() throws OntoDriverException {
70: this.open = false;
71: }
72:
73: @Override
74: public void first() {
75: throw new UnsupportedOperationException(
76: "Returning to the first row is not supported by this result set.");
77: }
78:
79: @Override
80: public void previous() {
81: throw new UnsupportedOperationException("Going back is not supported by this result set.");
82: }
83:
84: @Override
85: public void registerObserver(Observer observer) {
86: throw new UnsupportedOperationException("Not supported by the current version.");
87: }
88:
89: @Override
90: public void relative(int rows) throws OntoDriverException {
91: setRowIndex(index + rows);
92: }
93:
94: @Override
95: public void last() throws OntoDriverException {
96: ensureOpen();
97: while (hasNext()) {
98: next();
99: }
100: }
101:
102: @Override
103: public void next() throws OntoDriverException {
104: ensureOpen();
105: if (!hasNext()) {
106: throw new NoSuchElementException();
107: }
108: index++;
109: }
110:
111: @Override
112: public void setRowIndex(int rowIndex) throws OntoDriverException {
113: ensureOpen();
114: if (rowIndex < index) {
115: throw new UnsupportedOperationException(
116: "Going back in this result set is not supported.");
117: }
118: if (rowIndex == index) {
119: return;
120: }
121: while (index <= rowIndex) {
122: next();
123: }
124: }
125: }