Skip to content

Method: toQueryValues(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.jopa.query.parameter;
19:
20: import cz.cvut.kbss.jopa.query.sparql.SparqlConstants;
21:
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: /**
26: * Query parameter value holder.
27: */
28: public interface ParameterValue {
29:
30: /**
31: * Gets the value held by this wrapper.
32: *
33: * @return The parameter value
34: */
35: Object getValue();
36:
37: /**
38: * Gets this parameter value as a string which can be inserted directly into a query.
39: *
40: * @return Value as query string
41: */
42: String getQueryString();
43:
44: /**
45: * Builds a list of the specified size containing the value(s) represented by this parameter value.
46: * <p>
47: * If this instance does not contain enough values to fill in the list of the specified size, its remainder is
48: * filled with {@link cz.cvut.kbss.jopa.query.sparql.SparqlConstants#UNDEF}s.
49: * <p>
50: * The resulting list will be used to build a SPARQL {@literal VALUES} table.
51: *
52: * @param size Requested size of value list
53: * @return List of values
54: */
55: default List<String> toQueryValues(int size) {
56:• assert size > 0;
57:
58:• if (size == 1) {
59: return List.of(getQueryString());
60: }
61: final List<String> result = new ArrayList<>(size);
62: result.add(getQueryString());
63:• for (int i = 1; i < size; i++) {
64: result.add(SparqlConstants.UNDEF);
65: }
66: return result;
67: }
68:
69: /**
70: * Whether this parameter value is set or it represents just the parameter identification.
71: *
72: * @return {@code true} if this instance represents an explicit parameter value
73: */
74: default boolean isSet() {
75: return true;
76: }
77:
78: /**
79: * Returns the number of values held by this instance.
80: * <p>
81: * Will return number different from 1 only for collection value parameters.
82: *
83: * @return Number of values represented by this instance
84: */
85: default int valueCount() {return 1;}
86: }