Skip to content

Package: EntityResultMapper

EntityResultMapper

nameinstructionbranchcomplexitylinemethod
EntityResultMapper(EntityType)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addFieldMapper(FieldResultMapper)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getEntityType()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFieldMappers()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$map$0(ResultRow, Object, UnitOfWorkImpl, FieldResultMapper)
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%
map(ResultRow, UnitOfWorkImpl)
M: 6 C: 25
81%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%

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.mapper;
16:
17: import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.model.lifecycle.PostLoadInvoker;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
22: import cz.cvut.kbss.jopa.utils.ReflectionUtils;
23: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
24:
25: import java.util.ArrayList;
26: import java.util.Collections;
27: import java.util.List;
28:
29: /**
30: * Maps result of a SPARQL SELECT query to an entity instance.
31: */
32: class EntityResultMapper<T> implements SparqlResultMapper {
33:
34: private final EntityType<T> et;
35:
36: private final List<FieldResultMapper> fieldMappers = new ArrayList<>();
37:
38: EntityResultMapper(EntityType<T> et) {
39: this.et = et;
40: }
41:
42: void addFieldMapper(FieldResultMapper mapper) {
43: fieldMappers.add(mapper);
44: }
45:
46: List<FieldResultMapper> getFieldMappers() {
47: return Collections.unmodifiableList(fieldMappers);
48: }
49:
50: EntityType<T> getEntityType() {
51: return et;
52: }
53:
54: @Override
55: public T map(ResultRow resultRow, UnitOfWorkImpl uow) {
56: try {
57: final T instance = ReflectionUtils.instantiateUsingDefaultConstructor(et.getJavaType());
58: fieldMappers.forEach(m -> m.map(resultRow, instance, uow));
59: return (T) uow.registerExistingObject(instance, new EntityDescriptor(),
60: Collections.singletonList(new PostLoadInvoker(uow.getMetamodel())));
61: } catch (cz.cvut.kbss.jopa.exception.InstantiationException e) {
62: // This is not expected, since an entity class must have a public no-arg constructor
63: throw new SparqlResultMappingException(e);
64: }
65: }
66: }