Skip to contentMethod: getPosition()
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;
19:
20: import cz.cvut.kbss.jopa.model.query.Parameter;
21: import cz.cvut.kbss.jopa.query.parameter.ParameterValue;
22: import cz.cvut.kbss.jopa.query.parameter.ParameterValueFactory;
23:
24: import java.util.Objects;
25:
26: public class QueryParameter<T> implements Parameter<T> {
27:
28: private final String name;
29: private final Integer position;
30: private boolean projected;
31:
32: private final ParameterValueFactory valueFactory;
33:
34: private ParameterValue value;
35:
36: public QueryParameter(String name, ParameterValueFactory valueFactory) {
37: this.name = name;
38: this.valueFactory = valueFactory;
39: this.position = null;
40: resetValue();
41: }
42:
43: public QueryParameter(Integer position, ParameterValueFactory valueFactory) {
44: this.position = position;
45: this.valueFactory = valueFactory;
46: this.name = null;
47: resetValue();
48: }
49:
50: @Override
51: public String getName() {
52: return name;
53: }
54:
55: @Override
56: public Integer getPosition() {
57: return position;
58: }
59:
60: public Object getIdentifier() {
61: return name != null ? name : position;
62: }
63:
64: public String getIdentifierAsQueryString() {
65: return createVariableValue().getQueryString();
66: }
67:
68: public ParameterValue getValue() {
69: return value;
70: }
71:
72: public void setProjected(boolean projected) {
73: this.projected = projected;
74: }
75:
76: public boolean isProjected() {
77: return projected;
78: }
79:
80: public void setValue(Object value) {
81: assert value != null;
82: this.value = valueFactory.create(value);
83: }
84:
85: public void setValue(String value, String language) {
86: assert value != null;
87: this.value = valueFactory.create(value, language);
88: }
89:
90: public void setUntypedValue(Object value) {
91: assert value != null;
92: this.value = valueFactory.createUntyped(value);
93: }
94:
95: public void resetValue() {
96: this.value = createVariableValue();
97: }
98:
99: private ParameterValue createVariableValue() {
100: return name != null ? valueFactory.createVariableValue(name) : valueFactory.createVariableValue(position);
101: }
102:
103: @Override
104: public Class<T> getParameterType() {
105: throw new IllegalStateException("Parameter types are not supported by the current implementation.");
106: }
107:
108: @Override
109: public String toString() {
110: return getIdentifierAsQueryString() + (value.isSet() ? " = " + value : "");
111: }
112:
113: @Override
114: public boolean equals(Object o) {
115: if (this == o) {
116: return true;
117: }
118: if (o == null || !Parameter.class.isAssignableFrom(o.getClass())) {
119: return false;
120: }
121:
122: Parameter<?> that = (Parameter<?>) o;
123: return Objects.equals(name, that.getName()) && Objects.equals(position, that.getPosition());
124:
125: }
126:
127: @Override
128: public int hashCode() {
129: return Objects.hash(getName(), getPosition());
130: }
131: }