Skip to contentMethod: create(Object)
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.parameter;
16:
17: import cz.cvut.kbss.jopa.utils.ErrorUtils;
18:
19: import java.net.URI;
20: import java.net.URISyntaxException;
21: import java.net.URL;
22: import java.util.Date;
23: import java.util.Objects;
24:
25: /**
26: * Query parameter value holder.
27: *
28: * @author kidney
29: */
30: public abstract class ParameterValue {
31:
32: /**
33: * Gets the value held by this wrapper.
34: *
35: * @return The parameter value
36: */
37: public abstract Object getValue();
38:
39: /**
40: * Gets this parameter value as a string which can be inserted directly into a query.
41: *
42: * @return Value as query string
43: */
44: public abstract String getQueryString();
45:
46: /**
47: * Returns a new variable parameter specification.
48: * <p>
49: * This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
50: * unbound parameter.
51: *
52: * @param name Parameter (variable) name
53: * @return Parameter value object
54: */
55: public static ParameterValue createVariableValue(String name) {
56: return new NamedVariableParameterValue(name);
57: }
58:
59: /**
60: * Returns a new variable parameter specification.
61: * <p>
62: * This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
63: * unbound parameter.
64: *
65: * @param position Parameter (variable) position
66: * @return Parameter value object
67: */
68: public static ParameterValue createVariableValue(Integer position) {
69: return new PositionalVariableParameterValue(position);
70: }
71:
72: /**
73: * Returns new String parameter value specification.
74: * <p>
75: * The language tag is optional.
76: *
77: * @param value The value
78: * @param language Language tag of the value, e.g. en, cz. Optional
79: * @return Parameter value object
80: */
81: public static ParameterValue create(String value, String language) {
82: return new StringParameterValue(value, language);
83: }
84:
85: /**
86: * Returns new parameter value specification.
87: *
88: * @param value The value
89: * @return Parameter value object
90: */
91: public static ParameterValue create(Object value) {
92: Objects.requireNonNull(value, ErrorUtils.constructNPXMessage("value"));
93:• if (value instanceof URI) {
94: return new UriParameterValue((URI) value);
95:• } else if (value instanceof URL) {
96: try {
97: return new UriParameterValue(((URL) value).toURI());
98: } catch (URISyntaxException e) {
99: throw new IllegalArgumentException("Unable to transform the specified URL to URI.", e);
100: }
101:• } else if (value instanceof Boolean) {
102: return new BooleanParameterValue((Boolean) value);
103:• } else if (value instanceof Short) {
104: return new ShortParameterValue((Short) value);
105:• } else if (value instanceof Integer) {
106: return new IntegerParameterValue((Integer) value);
107:• } else if (value instanceof Long) {
108: return new LongParameterValue((Long) value);
109:• } else if (value instanceof Double) {
110: return new DoubleParameterValue((Double) value);
111:• } else if (value instanceof Float) {
112: return new FloatParameterValue((Float) value);
113:• } else if (value instanceof Date) {
114: return new DateParameterValue((Date) value);
115: } else {
116: return new StringParameterValue(value.toString());
117: }
118: }
119: }