-
Notifications
You must be signed in to change notification settings - Fork 14
/
ParameterValue.java
119 lines (110 loc) · 4.08 KB
/
ParameterValue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.query.parameter;
import cz.cvut.kbss.jopa.utils.ErrorUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.Objects;
/**
* Query parameter value holder.
*
* @author kidney
*/
public abstract class ParameterValue {
/**
* Gets the value held by this wrapper.
*
* @return The parameter value
*/
public abstract Object getValue();
/**
* Gets this parameter value as a string which can be inserted directly into a query.
*
* @return Value as query string
*/
public abstract String getQueryString();
/**
* Returns a new variable parameter specification.
* <p>
* This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
* unbound parameter.
*
* @param name Parameter (variable) name
* @return Parameter value object
*/
public static ParameterValue createVariableValue(String name) {
return new NamedVariableParameterValue(name);
}
/**
* Returns a new variable parameter specification.
* <p>
* This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
* unbound parameter.
*
* @param position Parameter (variable) position
* @return Parameter value object
*/
public static ParameterValue createVariableValue(Integer position) {
return new PositionalVariableParameterValue(position);
}
/**
* Returns new String parameter value specification.
* <p>
* The language tag is optional.
*
* @param value The value
* @param language Language tag of the value, e.g. en, cz. Optional
* @return Parameter value object
*/
public static ParameterValue create(String value, String language) {
return new StringParameterValue(value, language);
}
/**
* Returns new parameter value specification.
*
* @param value The value
* @return Parameter value object
*/
public static ParameterValue create(Object value) {
Objects.requireNonNull(value, ErrorUtils.constructNPXMessage("value"));
if (value instanceof URI) {
return new UriParameterValue((URI) value);
} else if (value instanceof URL) {
try {
return new UriParameterValue(((URL) value).toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Unable to transform the specified URL to URI.", e);
}
} else if (value instanceof Boolean) {
return new BooleanParameterValue((Boolean) value);
} else if (value instanceof Short) {
return new ShortParameterValue((Short) value);
} else if (value instanceof Integer) {
return new IntegerParameterValue((Integer) value);
} else if (value instanceof Long) {
return new LongParameterValue((Long) value);
} else if (value instanceof Double) {
return new DoubleParameterValue((Double) value);
} else if (value instanceof Float) {
return new FloatParameterValue((Float) value);
} else if (value instanceof Date) {
return new DateParameterValue((Date) value);
} else {
return new StringParameterValue(value.toString());
}
}
}