Skip to content

Package: SparqlQueryHolder

SparqlQueryHolder

nameinstructionbranchcomplexitylinemethod
SparqlQueryHolder(String, List, List)
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
assembleQuery()
M: 0 C: 53
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
clearParameter(Parameter)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
clearParameters()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getInternalParameter(Parameter)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getParameter(String)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getParameter(int)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getParameterValue(Parameter)
M: 4 C: 12
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getParameters()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getQuery()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$new$0(QueryParameter)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$new$1(QueryParameter)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setParameter(Parameter, Object)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setParameter(Parameter, String, String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
unknownParameter(Object)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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: }