Package: QueryFieldStrategy
QueryFieldStrategy
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
QueryFieldStrategy(EntityType, AbstractQueryAttribute) |
|
|
|
|
|
||||||||||||||||||||
canBeConverted(Object) |
|
|
|
|
|
||||||||||||||||||||
isValidRange(Object) |
|
|
|
|
|
||||||||||||||||||||
setValueOnInstance(Object, Object) |
|
|
|
|
|
||||||||||||||||||||
toAttributeValue(Object) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2023 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.oom.query;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.AbstractQueryAttribute;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.model.metamodel.SingularQueryAttribute;
20: import cz.cvut.kbss.jopa.model.query.TypedQuery;
21: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
22: import cz.cvut.kbss.jopa.oom.converter.DefaultConverterWrapper;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24:
25: /**
26: * @param <T> The query attribute type, e.g. {@link SingularQueryAttribute}
27: * @param <X> Entity class
28: */
29: public abstract class QueryFieldStrategy<T extends AbstractQueryAttribute<? super X, ?>, X> {
30:
31: final EntityType<X> et;
32: final T attribute;
33:
34: private final ConverterWrapper<Object, Object> converter;
35:
36: protected QueryFieldStrategy(EntityType<X> et, T attribute) {
37: this.et = et;
38: this.attribute = attribute;
39:• this.converter = attribute.getConverter() != null ? attribute.getConverter() : DefaultConverterWrapper.INSTANCE;
40: }
41:
42: /**
43: * Adds value from the specified typed query to this strategy.
44: * <p>
45: * The value(s) is/are then set on entity field using {@link #buildInstanceFieldValue(Object)}.
46: *
47: * @param typedQuery typed query to extract value from
48: */
49: public abstract void addValueFromTypedQuery(TypedQuery<?> typedQuery);
50:
51: /**
52: * Sets instance field from values gathered in this strategy.
53: *
54: * @param instance The instance to receive the field value
55: */
56: public abstract void buildInstanceFieldValue(Object instance);
57:
58: /**
59: * Sets the specified value on the specified instance, the field is taken from the attribute represented by this
60: * strategy.
61: * <p>
62: * Note that this method assumes the value and the field are of compatible types, no check is done here.
63: */
64: void setValueOnInstance(Object instance, Object value) {
65: EntityPropertiesUtils.setFieldValue(attribute.getJavaField(), instance, value);
66: }
67:
68: boolean isValidRange(Object value) {
69:• return attribute.getJavaType().isAssignableFrom(value.getClass()) || canBeConverted(value);
70: }
71:
72: boolean canBeConverted(Object value) {
73: return converter.supportsAxiomValueType(value.getClass());
74: }
75:
76: Object toAttributeValue(Object value) {
77: return converter.convertToAttribute(value);
78: }
79: }