Skip to content

Package: QueryFieldStrategy

QueryFieldStrategy

nameinstructionbranchcomplexitylinemethod
QueryFieldStrategy(EntityType, AbstractQueryAttribute)
M: 3 C: 15
83%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
canBeConverted(Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isValidRange(Object)
M: 0 C: 15
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
setValueOnInstance(Object, Object)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
toAttributeValue(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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