Skip to content

Method: getParameter(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.jopa.query.sparql;
16:
17: import cz.cvut.kbss.jopa.model.query.Parameter;
18: import cz.cvut.kbss.jopa.query.QueryHolder;
19: import cz.cvut.kbss.jopa.query.QueryParameter;
20:
21: import java.util.*;
22:
23: public class SparqlQueryHolder implements QueryHolder {
24:
25: // Original query string
26: private final String query;
27:
28: private final Map<Parameter<?>, QueryParameter<?>> parameterSet;
29: private final Map<Object, QueryParameter<?>> identifiersToParameters;
30: // These parameters are in order matching the query parts and can appear multiple times in the list
31: private final List<QueryParameter<?>> parameters;
32: private final List<String> queryParts;
33:
34: public SparqlQueryHolder(String query, List<String> parts, List<QueryParameter<?>> parameters) {
35: this.query = query;
36: this.parameters = parameters;
37: this.queryParts = parts;
38: this.parameterSet = new HashMap<>();
39: parameters.forEach(p -> parameterSet.put(p, p));
40: this.identifiersToParameters = new HashMap<>(parameterSet.size());
41: parameterSet.values().forEach(p -> identifiersToParameters.put(p.getIdentifier(), p));
42: }
43:
44: @Override
45: public String getQuery() {
46: return query;
47: }
48:
49: @Override
50: public Set<Parameter<?>> getParameters() {
51: return Collections.unmodifiableSet(parameterSet.keySet());
52: }
53:
54: @Override
55: public QueryParameter<?> getParameter(String name) {
56: if (!identifiersToParameters.containsKey(name)) {
57: throw unknownParameter(name);
58: }
59: return identifiersToParameters.get(name);
60: }
61:
62: private IllegalArgumentException unknownParameter(Object p) {
63: return new IllegalArgumentException("Parameter '" + p + "' does not exist in this query.");
64: }
65:
66: @Override
67: public Parameter<?> getParameter(int position) {
68:• if (!identifiersToParameters.containsKey(position)) {
69: throw unknownParameter(position);
70: }
71: return identifiersToParameters.get(position);
72: }
73:
74: @Override
75: public Object getParameterValue(Parameter<?> parameter) {
76: assert getInternalParameter(parameter).getValue() != null;
77: return getInternalParameter(parameter).getValue().getValue();
78: }
79:
80: private QueryParameter<?> getInternalParameter(Parameter<?> p) {
81: Objects.requireNonNull(p);
82: if (!parameterSet.containsKey(p)) {
83: throw unknownParameter(p);
84: }
85: return parameterSet.get(p);
86: }
87:
88: @Override
89: public <T> void setParameter(Parameter<T> parameter, Object value) {
90: Objects.requireNonNull(value);
91: getInternalParameter(parameter).setValue(value);
92: }
93:
94: @Override
95: public <T> void setParameter(Parameter<T> parameter, String value, String language) {
96: Objects.requireNonNull(value);
97: getInternalParameter(parameter).setValue(value, language);
98: }
99:
100: @Override
101: public void clearParameter(Parameter<?> parameter) {
102: getInternalParameter(parameter).resetValue();
103: }
104:
105: @Override
106: public void clearParameters() {
107: parameterSet.values().forEach(QueryParameter::resetValue);
108: }
109:
110: @Override
111: public String assembleQuery() {
112: final StringBuilder sb = new StringBuilder();
113: for (int i = 0; i < parameters.size(); i++) {
114: sb.append(queryParts.get(i));
115: final String paramValue = parameters.get(i).getValue().getQueryString();
116: sb.append(paramValue);
117: }
118: if (queryParts.size() > parameters.size()) {
119: sb.append(queryParts.get(parameters.size()));
120: }
121: return sb.toString();
122: }
123:
124: @Override
125: public String toString() {
126: return query;
127: }
128: }