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