Skip to content

Method: getShort(int)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.query;
19:
20: import cz.cvut.kbss.ontodriver.Statement;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
23:
24: public class AskResultSet extends AbstractResultSet {
25:
26: private final boolean result;
27:
28: public AskResultSet(boolean result, Statement statement) {
29: super(statement);
30: this.result = result;
31: }
32:
33: @Override
34: public int findColumn(String columnLabel) {
35: return 0;
36: }
37:
38: @Override
39: public int getColumnCount() {
40: return 1;
41: }
42:
43: @Override
44: public boolean isBound(int variableIndex) {
45: // Return always true, as we do not care about index/name for ASK results
46: return true;
47: }
48:
49: @Override
50: public boolean isBound(String variableName) {
51: // Return always true, as we do not care about index/name for ASK results
52: return true;
53: }
54:
55: // We discard column index and column name, because in a boolean result, there is no such concept. Therefore,
56: // the result is returned for any column index and column name.
57:
58: @Override
59: public boolean getBoolean(int columnIndex) {
60: ensureState();
61: return result;
62: }
63:
64: private void ensureState() {
65: ensureOpen();
66: if (!isFirst()) {
67: throw new IllegalStateException("Must call next before getting the first value.");
68: }
69: }
70:
71: @Override
72: public boolean getBoolean(String columnLabel) {
73: ensureState();
74: return result;
75: }
76:
77: @Override
78: public byte getByte(int columnIndex) {
79: ensureState();
80: throw unsupported("byte");
81: }
82:
83: private static UnsupportedOperationException unsupported(String type) {
84: return new UnsupportedOperationException("ASK query results cannot return " + type + "values.");
85: }
86:
87: @Override
88: public byte getByte(String columnLabel) {
89: ensureState();
90: throw unsupported("byte");
91: }
92:
93: @Override
94: public double getDouble(int columnIndex) {
95: ensureState();
96: throw unsupported("double");
97: }
98:
99: @Override
100: public double getDouble(String columnLabel) {
101: ensureState();
102: throw unsupported("double");
103: }
104:
105: @Override
106: public float getFloat(int columnIndex) {
107: ensureState();
108: throw unsupported("float");
109: }
110:
111: @Override
112: public float getFloat(String columnLabel) {
113: ensureState();
114: throw unsupported("float");
115: }
116:
117: @Override
118: public int getInt(int columnIndex) {
119: ensureState();
120: throw unsupported("int");
121: }
122:
123: @Override
124: public int getInt(String columnLabel) {
125: ensureState();
126: throw unsupported("int");
127: }
128:
129: @Override
130: public long getLong(int columnIndex) {
131: ensureState();
132: throw unsupported("long");
133: }
134:
135: @Override
136: public long getLong(String columnLabel) {
137: ensureState();
138: throw unsupported("long");
139: }
140:
141: @Override
142: public Object getObject(int columnIndex) {
143: ensureState();
144: return result;
145: }
146:
147: @Override
148: public Object getObject(String columnLabel) {
149: ensureState();
150: return result;
151: }
152:
153: @Override
154: public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
155: ensureState();
156: return toType(cls);
157: }
158:
159: @Override
160: public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
161: ensureState();
162: return toType(cls);
163: }
164:
165: private <T> T toType(Class<T> type) throws Rdf4jDriverException {
166: if (type.isAssignableFrom(Boolean.class)) {
167: return type.cast(result);
168: }
169: if (type.isAssignableFrom(String.class)) {
170: return type.cast(getString(0));
171: }
172: throw new Rdf4jDriverException("Unable to return boolean result as type " + type);
173: }
174:
175: @Override
176: public short getShort(int columnIndex) {
177: ensureState();
178: throw unsupported("short");
179: }
180:
181: @Override
182: public short getShort(String columnLabel) {
183: ensureState();
184: throw unsupported("short");
185: }
186:
187: @Override
188: public String getString(int columnIndex) {
189: ensureState();
190: return Boolean.toString(result);
191: }
192:
193: @Override
194: public String getString(String columnLabel) {
195: ensureState();
196: return Boolean.toString(result);
197: }
198:
199: @Override
200: public boolean hasNext() throws OntoDriverException {
201: ensureOpen();
202: return !isFirst();
203: }
204: }