Package: ParameterValueFactory
ParameterValueFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ParameterValueFactory(MetamodelProvider) |
|
|
|
|
|
||||||||||||||||||||
create(Object) |
|
|
|
|
|
||||||||||||||||||||
create(String, String) |
|
|
|
|
|
||||||||||||||||||||
createUntyped(Object) |
|
|
|
|
|
||||||||||||||||||||
createVariableValue(Integer) |
|
|
|
|
|
||||||||||||||||||||
createVariableValue(String) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 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.sessions.MetamodelProvider;
18:
19: import java.net.URI;
20: import java.net.URISyntaxException;
21: import java.net.URL;
22: import java.time.temporal.TemporalAccessor;
23: import java.time.temporal.TemporalAmount;
24: import java.util.Collection;
25: import java.util.Date;
26: import java.util.Objects;
27: import java.util.stream.Collectors;
28:
29: public class ParameterValueFactory {
30:
31: private final MetamodelProvider metamodelProvider;
32:
33: public ParameterValueFactory(MetamodelProvider metamodelProvider) {
34: this.metamodelProvider = metamodelProvider;
35: }
36:
37: /**
38: * Returns a new variable parameter specification.
39: * <p>
40: * This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
41: * unbound parameter.
42: *
43: * @param name Parameter (variable) name
44: * @return Parameter value object
45: */
46: public ParameterValue createVariableValue(String name) {
47: return new NamedVariableParameterValue(name);
48: }
49:
50: /**
51: * Returns a new variable parameter specification.
52: * <p>
53: * This is the default implementation, if a parameter is not set, a variable is used in the query to represent an
54: * unbound parameter.
55: *
56: * @param position Parameter (variable) position
57: * @return Parameter value object
58: */
59: public ParameterValue createVariableValue(Integer position) {
60: return new PositionalVariableParameterValue(position);
61: }
62:
63: /**
64: * Returns new String parameter value specification.
65: * <p>
66: * The language tag is optional.
67: *
68: * @param value The value
69: * @param language Language tag of the value, e.g. en, cz. Optional
70: * @return Parameter value object
71: */
72: public ParameterValue create(String value, String language) {
73: return new StringParameterValue(value, language);
74: }
75:
76: /**
77: * Returns new parameter value specification.
78: *
79: * @param value The value
80: * @return Parameter value object
81: */
82: public ParameterValue create(Object value) {
83: Objects.requireNonNull(value);
84:• if (value instanceof URI) {
85: return new UriParameterValue((URI) value);
86:• } else if (value instanceof URL) {
87: try {
88: return new UriParameterValue(((URL) value).toURI());
89: } catch (URISyntaxException e) {
90: throw new IllegalArgumentException("Unable to transform the specified URL to URI.", e);
91: }
92:• } else if (value instanceof Boolean) {
93: return new BooleanParameterValue((Boolean) value);
94:• } else if (value instanceof Short) {
95: return new ShortParameterValue((Short) value);
96:• } else if (value instanceof Integer) {
97: return new IntegerParameterValue((Integer) value);
98:• } else if (value instanceof Long) {
99: return new LongParameterValue((Long) value);
100:• } else if (value instanceof Double) {
101: return new DoubleParameterValue((Double) value);
102:• } else if (value instanceof Float) {
103: return new FloatParameterValue((Float) value);
104:• } else if (value instanceof TemporalAccessor) {
105: return new TemporalParameterValue((TemporalAccessor) value);
106:• } else if (value instanceof Date) {
107: return new TemporalParameterValue(((Date) value).toInstant());
108:• } else if (value instanceof TemporalAmount) {
109: return new DurationParameterValue((TemporalAmount) value);
110:• } else if (metamodelProvider.isEntityType(value.getClass())) {
111: return new EntityParameterValue(value, metamodelProvider);
112:• } else if (value instanceof Collection) {
113: return new CollectionParameterValue(
114: ((Collection<?>) value).stream().map(this::create).collect(Collectors.toList()));
115: } else {
116: return new StringParameterValue(value.toString());
117: }
118: }
119:
120: /**
121: * Returns new untyped parameter value specification.
122: *
123: * @param value The value
124: * @return Parameter value object
125: */
126: public ParameterValue createUntyped(Object value) {
127: return new UntypedParameterValue(Objects.requireNonNull(value));
128: }
129: }