Skip to content

Method: ParameterValueFactory(MetamodelProvider)

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