Skip to content

Method: getBoolean(int)

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