Skip to content

Package: TypesFieldStrategy

TypesFieldStrategy

nameinstructionbranchcomplexitylinemethod
TypesFieldStrategy(EntityType, TypesSpecification, Descriptor, EntityMappingHelper)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addValueFromAxiom(Axiom)
M: 1 C: 15
94%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
buildAxiomValuesFromInstance(Object, AxiomValueGatherer)
M: 4 C: 70
95%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 0 C: 20
100%
M: 0 C: 1
100%
buildInstanceFieldValue(Object)
M: 5 C: 19
79%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 4
80%
M: 0 C: 1
100%
createAssertion()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
extractTypesToAdd(AxiomValueGatherer, Set, Set)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
extractTypesToRemove(AxiomValueGatherer, Set, Set)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$prepareTypes$2(Object)
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$typesDiff$0(Set, Object)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$typesDiff$1(Object)
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%
prepareTypes(Set)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
typesDiff(Set, Set)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 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;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22:
23: import java.net.URI;
24: import java.util.Collections;
25: import java.util.HashSet;
26: import java.util.Set;
27: import java.util.stream.Collectors;
28:
29: public class TypesFieldStrategy<X> extends FieldStrategy<TypesSpecification<? super X, ?>, X> {
30:
31: private final Set<String> values = new HashSet<>();
32:
33: public TypesFieldStrategy(EntityType<X> et, TypesSpecification<? super X, ?> att,
34: Descriptor descriptor, EntityMappingHelper mapper) {
35: super(et, att, descriptor, mapper);
36: }
37:
38: @Override
39: void addValueFromAxiom(Axiom<?> ax) {
40:• if (MappingUtils.isEntityClassAssertion(ax, et)) {
41: return;
42: }
43: final String typeAsString = ax.getValue().stringValue();
44: values.add(typeAsString);
45: }
46:
47: @Override
48: void buildInstanceFieldValue(Object instance) throws IllegalArgumentException,
49: IllegalAccessException {
50:• assert attribute.getJavaField().getType().isAssignableFrom(Set.class);
51:
52:• if (values.isEmpty()) {
53: return;
54: }
55: setValueOnInstance(instance, values);
56: }
57:
58: @Override
59: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
60: final Object val = extractFieldValueFromInstance(instance);
61: final X original = mapper.getOriginalInstance(instance);
62:• if (val == null) {
63:• if (original == null) {
64: return;
65: }
66: final Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original);
67:• if (origTypes == null || origTypes.isEmpty()) {
68: return;
69: }
70: valueBuilder.removeTypes(prepareTypes(origTypes), getAttributeContext());
71: } else {
72:• assert val instanceof Set; // This is verified when the metamodel is built
73: final Set<?> types = (Set<?>) val;
74:• if (original == null) {
75: valueBuilder.addTypes(prepareTypes(types), getAttributeContext());
76: } else {
77: Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original);
78:• if (origTypes == null) {
79: origTypes = Collections.emptySet();
80: }
81: extractTypesToAdd(valueBuilder, types, origTypes);
82: extractTypesToRemove(valueBuilder, types, origTypes);
83: }
84: }
85: }
86:
87: private void extractTypesToAdd(AxiomValueGatherer valueBuilder, Set<?> types, Set<?> origTypes) {
88: final Set<URI> toAdd = typesDiff(origTypes, types);
89: valueBuilder.addTypes(toAdd, getAttributeContext());
90: }
91:
92: private Set<URI> typesDiff(Set<?> base, Set<?> difference) {
93: final Set<URI> addedDiff = new HashSet<>(base.size());
94:• addedDiff.addAll(difference.stream().filter(t -> !base.contains(t)).map(t -> URI.create(t.toString()))
95: .collect(Collectors.toList()));
96: return addedDiff;
97: }
98:
99: private void extractTypesToRemove(AxiomValueGatherer valueBuilder, Set<?> types, Set<?> origTypes) {
100: final Set<URI> toRemove = typesDiff(types, origTypes);
101: valueBuilder.removeTypes(toRemove, getAttributeContext());
102: }
103:
104: private Set<URI> prepareTypes(Set<?> types) {
105: final Set<URI> toAdd = new HashSet<>(types.size());
106: toAdd.addAll(types.stream().map(t -> URI.create(t.toString())).collect(Collectors.toList()));
107: return toAdd;
108: }
109:
110: @Override
111: Assertion createAssertion() {
112: return Assertion.createClassAssertion(attribute.isInferred());
113: }
114: }