Skip to contentMethod: getInt(int)
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.ontodriver.owlapi.query;
16:
17: import cz.cvut.kbss.ontodriver.Statement;
18: import cz.cvut.kbss.owl2query.model.QueryResult;
19: import org.semanticweb.owlapi.model.OWLObject;
20:
21: class AskResultSet extends AbstractResultSet {
22:
23: private final boolean result;
24: private boolean nextCalled = false;
25:
26: public AskResultSet(QueryResult<OWLObject> queryResult, Statement statement) {
27: super(statement);
28: this.result = !queryResult.isEmpty();
29: }
30:
31: @Override
32: public int findColumn(String columnLabel) {
33: return 0;
34: }
35:
36: @Override
37: public int getColumnCount() {
38: return 1;
39: }
40:
41: @Override
42: public boolean isBound(int variableIndex) {
43: ensureState();
44: // We do not care about index/name in ASK result set
45: return true;
46: }
47:
48: @Override
49: public boolean isBound(String variableName) {
50: ensureState();
51: // We do not care about index/name in ASK result set
52: return true;
53: }
54:
55: @Override
56: public void first() {
57: // Do nothing
58: }
59:
60: @Override
61: public boolean getBoolean(int columnIndex) {
62: ensureState();
63: return result;
64: }
65:
66: private void ensureState() {
67: ensureOpen();
68: if (!nextCalled) {
69: throw new IllegalStateException(
70: "Next has to be called before attempting to get any value from the result set.");
71: }
72: }
73:
74: @Override
75: public boolean getBoolean(String columnLabel) {
76: ensureState();
77: return result;
78: }
79:
80: @Override
81: public byte getByte(int columnIndex) {
82: ensureState();
83: throw unsupported("byte");
84: }
85:
86: private static UnsupportedOperationException unsupported(String type) {
87: return new UnsupportedOperationException("ASK query results cannot return " + type + "values.");
88: }
89:
90: @Override
91: public byte getByte(String columnLabel) {
92: ensureState();
93: throw unsupported("byte");
94: }
95:
96: @Override
97: public double getDouble(int columnIndex) {
98: ensureState();
99: throw unsupported("double");
100: }
101:
102: @Override
103: public double getDouble(String columnLabel) {
104: ensureState();
105: throw unsupported("double");
106: }
107:
108: @Override
109: public float getFloat(int columnIndex) {
110: ensureState();
111: throw unsupported("float");
112: }
113:
114: @Override
115: public float getFloat(String columnLabel) {
116: ensureState();
117: throw unsupported("float");
118: }
119:
120: @Override
121: public int getInt(int columnIndex) {
122: ensureState();
123: throw unsupported("int");
124: }
125:
126: @Override
127: public int getInt(String columnLabel) {
128: ensureState();
129: throw unsupported("int");
130: }
131:
132: @Override
133: public long getLong(int columnIndex) {
134: ensureState();
135: throw unsupported("long");
136: }
137:
138: @Override
139: public long getLong(String columnLabel) {
140: ensureState();
141: throw unsupported("long");
142: }
143:
144: @Override
145: public Object getObject(int columnIndex) {
146: ensureState();
147: return result;
148: }
149:
150: @Override
151: public Object getObject(String columnLabel) {
152: ensureState();
153: return result;
154: }
155:
156: @Override
157: public <T> T getObject(int columnIndex, Class<T> cls) {
158: ensureState();
159: if (cls.isAssignableFrom(Boolean.class)) {
160: return cls.cast(result);
161: }
162: if (cls.isAssignableFrom(String.class)) {
163: return cls.cast(Boolean.toString(result));
164: }
165: throw unsupported(cls.getSimpleName());
166: }
167:
168: @Override
169: public <T> T getObject(String columnLabel, Class<T> cls) {
170: return getObject(0, cls);
171: }
172:
173: @Override
174: public int getRowIndex() {
175: return 0;
176: }
177:
178: @Override
179: public short getShort(int columnIndex) {
180: ensureState();
181: throw unsupported("short");
182: }
183:
184: @Override
185: public short getShort(String columnLabel) {
186: ensureState();
187: throw unsupported("short");
188: }
189:
190: @Override
191: public String getString(int columnIndex) {
192: ensureState();
193: return Boolean.toString(result);
194: }
195:
196: @Override
197: public String getString(String columnLabel) {
198: ensureState();
199: return Boolean.toString(result);
200: }
201:
202: @Override
203: public boolean isFirst() {
204: return true;
205: }
206:
207: @Override
208: public boolean hasNext() {
209: return !nextCalled;
210: }
211:
212: @Override
213: public void last() {
214: // do nothing
215: }
216:
217: @Override
218: public void next() {
219: if (!hasNext()) {
220: throw new IllegalStateException("No more elements in this result set.");
221: }
222: this.nextCalled = true;
223: }
224:
225: @Override
226: public void previous() {
227: // Do nothing
228: }
229:
230:
231: @Override
232: public void relative(int rows) {
233: throw new UnsupportedOperationException("Row-based navigation not supported by ASK result set.");
234: }
235:
236: @Override
237: public void setRowIndex(int rowIndex) {
238: throw new UnsupportedOperationException("Row-based navigation not supported by ASK result set.");
239: }
240: }