Skip to contentMethod: first()
1: /**
2: * Copyright (C) 2016 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.sesame.query;
16:
17: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
18: import cz.cvut.kbss.ontodriver.ResultSet;
19: import cz.cvut.kbss.ontodriver.Statement;
20:
21: import java.util.NoSuchElementException;
22: import java.util.Observer;
23:
24: public abstract class AbstractResultSet implements ResultSet {
25:
26: private final Statement statement;
27:
28: 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() throws OntoDriverException {
46: ensureOpen();
47: return index;
48: }
49:
50: @Override
51: public Statement getStatement() throws OntoDriverException {
52: ensureOpen();
53: return statement;
54: }
55:
56: @Override
57: public boolean isFirst() throws OntoDriverException {
58: ensureOpen();
59: return index == 0;
60: }
61:
62: protected 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() throws OntoDriverException {
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() throws OntoDriverException {
81: throw new UnsupportedOperationException("Going back is not supported by this result set.");
82: }
83:
84: @Override
85: public void registerObserver(Observer observer) throws OntoDriverException {
86: // TODO Auto-generated method stub
87:
88: }
89:
90: @Override
91: public void relative(int rows) throws OntoDriverException {
92: setRowIndex(index + rows);
93: }
94:
95: @Override
96: public void last() throws OntoDriverException {
97: ensureOpen();
98: while (hasNext()) {
99: next();
100: }
101: }
102:
103: @Override
104: public void next() throws OntoDriverException {
105: ensureOpen();
106: if (!hasNext()) {
107: throw new NoSuchElementException();
108: }
109: index++;
110: }
111:
112: @Override
113: public void setRowIndex(int rowIndex) throws OntoDriverException {
114: ensureOpen();
115: if (rowIndex < index) {
116: throw new UnsupportedOperationException(
117: "Going back in this result set is not supported.");
118: }
119: if (rowIndex == index) {
120: return;
121: }
122: while (index <= rowIndex) {
123: next();
124: }
125: }
126: }