Skip to content

Method: getShort(String)

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.jena.query;
19:
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
22:
23: import java.lang.reflect.Constructor;
24: import java.lang.reflect.InvocationTargetException;
25: import java.util.Objects;
26:
27: public class AskResultSet extends AbstractResultSet {
28:
29: private final boolean result;
30:
31: public AskResultSet(boolean result) {
32: this.result = result;
33: }
34:
35: @Override
36: public int findColumn(String columnLabel) {
37: return 0;
38: }
39:
40: @Override
41: public int getColumnCount() {
42: return 1;
43: }
44:
45: @Override
46: public boolean isBound(int variableIndex) {
47: // ASK query variables are always bound
48: return true;
49: }
50:
51: @Override
52: public boolean isBound(String variableName) {
53: // ASK query variables are always bound
54: return true;
55: }
56:
57: // We discard column index and column name, because in a boolean result, there is no such concept. Therefore,
58: // the result is returned for any column index and column name.
59:
60: @Override
61: public boolean getBoolean(int columnIndex) {
62: ensureState();
63: return result;
64: }
65:
66: @Override
67: public boolean getBoolean(String columnLabel) {
68: ensureState();
69: return result;
70: }
71:
72: @Override
73: public byte getByte(int columnIndex) {
74: ensureState();
75: throw unsupported("byte");
76: }
77:
78: private static UnsupportedOperationException unsupported(String type) {
79: return new UnsupportedOperationException("ASK query results cannot return " + type + " values.");
80: }
81:
82: @Override
83: public byte getByte(String columnLabel) {
84: ensureState();
85: throw unsupported("byte");
86: }
87:
88: @Override
89: public double getDouble(int columnIndex) {
90: ensureState();
91: throw unsupported("double");
92: }
93:
94: @Override
95: public double getDouble(String columnLabel) {
96: ensureState();
97: throw unsupported("double");
98: }
99:
100: @Override
101: public float getFloat(int columnIndex) {
102: ensureState();
103: throw unsupported("float");
104: }
105:
106: @Override
107: public float getFloat(String columnLabel) {
108: ensureState();
109: throw unsupported("float");
110: }
111:
112: @Override
113: public int getInt(int columnIndex) {
114: ensureState();
115: throw unsupported("int");
116: }
117:
118: @Override
119: public int getInt(String columnLabel) {
120: ensureState();
121: throw unsupported("int");
122: }
123:
124: @Override
125: public long getLong(int columnIndex) {
126: ensureState();
127: throw unsupported("long");
128: }
129:
130: @Override
131: public long getLong(String columnLabel) {
132: ensureState();
133: throw unsupported("long");
134: }
135:
136: @Override
137: public Object getObject(int columnIndex) {
138: ensureState();
139: return result;
140: }
141:
142: @Override
143: public Object getObject(String columnLabel) {
144: ensureState();
145: return result;
146: }
147:
148: @Override
149: public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
150: return toObject(cls);
151: }
152:
153: private <T> T toObject(Class<T> cls) throws JenaDriverException {
154: ensureState();
155: Objects.requireNonNull(cls);
156: if (cls.isAssignableFrom(Boolean.class)) {
157: return cls.cast(result);
158: }
159: if (cls.isAssignableFrom(String.class)) {
160: return cls.cast(Boolean.toString(result));
161: }
162: return buildUsingConstructor(cls);
163: }
164:
165: private <T> T buildUsingConstructor(Class<T> cls) throws JenaDriverException {
166: try {
167: for (Constructor<?> c : cls.getDeclaredConstructors()) {
168: if (c.getParameterCount() != 1) {
169: continue;
170: }
171: final Class<?> paramType = c.getParameterTypes()[0];
172: if (paramType.isAssignableFrom(Boolean.class) || paramType.isAssignableFrom(boolean.class)) {
173: return cls.cast(c.newInstance(result));
174: } else if (paramType.isAssignableFrom(String.class)) {
175: return cls.cast(c.newInstance(Boolean.toString(result)));
176: }
177: }
178: } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
179: throw new JenaDriverException("Unable to instantiate class " + cls + " with value " + result, e);
180: }
181: throw new JenaDriverException("No suitable constructor for value " + result + " found in type " + cls);
182: }
183:
184: @Override
185: public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
186: return toObject(cls);
187: }
188:
189: @Override
190: public short getShort(int columnIndex) {
191: ensureState();
192: throw unsupported("short");
193: }
194:
195: @Override
196: public short getShort(String columnLabel) {
197: ensureState();
198: throw unsupported("short");
199: }
200:
201: @Override
202: public String getString(int columnIndex) {
203: ensureState();
204: return Boolean.toString(result);
205: }
206:
207: @Override
208: public String getString(String columnLabel) {
209: ensureState();
210: return Boolean.toString(result);
211: }
212:
213: @Override
214: public boolean hasNext() {
215: ensureOpen();
216: return getRowIndex() == -1;
217: }
218: }