Skip to content

Package: EntityDescriptor

EntityDescriptor

nameinstructionbranchcomplexitylinemethod
EntityDescriptor()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntityDescriptor(Set)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntityDescriptor(URI)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntityDescriptor(URI, boolean)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
EntityDescriptor(boolean)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addAttributeContext(FieldSpecification, URI)
M: 0 C: 30
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addAttributeDescriptor(FieldSpecification, Descriptor)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createDescriptor(FieldSpecification, Set)
M: 0 C: 39
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
equals(Object)
M: 14 C: 70
83%
M: 5 C: 17
77%
M: 3 C: 9
75%
M: 2 C: 17
89%
M: 0 C: 1
100%
getAttributeContexts(FieldSpecification)
M: 0 C: 19
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getAttributeDescriptor(FieldSpecification)
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getAttributeDescriptors()
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%
hashCode()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$hashCode$1(Map.Entry)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$setLanguage$0(String, Descriptor)
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%
setAttributeLanguage(FieldSpecification, String)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setLanguage(String)
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%

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.model.descriptors;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
18: import cz.cvut.kbss.jopa.model.metamodel.Attribute.PersistentAttributeType;
19: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
20: import cz.cvut.kbss.jopa.model.metamodel.QueryAttribute;
21: import cz.cvut.kbss.jopa.utils.ErrorUtils;
22:
23: import java.lang.reflect.Field;
24: import java.net.URI;
25: import java.util.*;
26: import java.util.Map.Entry;
27:
28: /**
29: * Describes an entity.
30: * <p>
31: * Each attribute has a descriptor associated with it.
32: */
33: public class EntityDescriptor extends AbstractDescriptor {
34:
35: private final Map<Field, Descriptor> fieldDescriptors;
36:
37: public EntityDescriptor() {
38: this.fieldDescriptors = new HashMap<>();
39: }
40:
41: /**
42: * Allows to configure where object property assertions should be stored
43: *
44: * @param assertionsInSubjectContext Whether object property assertions are stored in the subject's. Defaults to
45: * {@code true}. If {@code false}, object property assertions are stored in the
46: * object's context
47: * @see #areAssertionsInSubjectContext()
48: */
49: public EntityDescriptor(boolean assertionsInSubjectContext) {
50: super(assertionsInSubjectContext);
51: this.fieldDescriptors = new HashMap<>();
52: }
53:
54: public EntityDescriptor(URI context) {
55: super(context);
56: this.fieldDescriptors = new HashMap<>();
57: }
58:
59: public EntityDescriptor(Set<URI> contexts) {
60: this();
61: this.contexts.addAll(Objects.requireNonNull(contexts));
62: }
63:
64: public EntityDescriptor(URI context, boolean assertionsInSubjectContext) {
65: super(context, assertionsInSubjectContext);
66: this.fieldDescriptors = new HashMap<>();
67: }
68:
69: @Override
70: public EntityDescriptor addAttributeDescriptor(FieldSpecification<?, ?> attribute, Descriptor descriptor) {
71: Objects.requireNonNull(attribute, ErrorUtils.getNPXMessageSupplier("attribute"));
72: Objects.requireNonNull(descriptor, ErrorUtils.getNPXMessageSupplier("descriptor"));
73:
74: fieldDescriptors.put(attribute.getJavaField(), descriptor);
75: return this;
76: }
77:
78: @Override
79: public EntityDescriptor addAttributeContext(FieldSpecification<?, ?> attribute, URI context) {
80: Objects.requireNonNull(attribute, ErrorUtils.getNPXMessageSupplier("attribute"));
81:
82:• fieldDescriptors.putIfAbsent(attribute.getJavaField(),
83: createDescriptor(attribute, context != null ? Collections.singleton(context) : Collections.emptySet()));
84: fieldDescriptors.get(attribute.getJavaField()).addContext(context);
85: return this;
86: }
87:
88: @Override
89: public EntityDescriptor setLanguage(String languageTag) {
90: super.setLanguage(languageTag);
91: fieldDescriptors.values().forEach(d -> d.setLanguage(languageTag));
92: return this;
93: }
94:
95: @Override
96: public EntityDescriptor setAttributeLanguage(FieldSpecification<?, ?> attribute, String languageTag) {
97: Objects.requireNonNull(attribute);
98:
99: fieldDescriptors.putIfAbsent(attribute.getJavaField(), createDescriptor(attribute, getContexts()));
100: fieldDescriptors.get(attribute.getJavaField()).setLanguage(languageTag);
101: return this;
102: }
103:
104: @Override
105: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
106: Objects.requireNonNull(attribute);
107: Descriptor d = fieldDescriptors.get(attribute.getJavaField());
108:• if (d == null) {
109: d = createDescriptor(attribute, getContexts());
110:• if (hasLanguage()) {
111: d.setLanguage(getLanguage());
112: }
113: }
114: return d;
115: }
116:
117: @Override
118: public Set<URI> getAttributeContexts(FieldSpecification<?, ?> attribute) {
119: Objects.requireNonNull(attribute);
120: final Descriptor attDescriptor = getAttributeDescriptor(attribute);
121:• return attDescriptor.overridesAssertionContext() || !assertionsInSubjectContext ?
122: attDescriptor.getContexts() : getContexts();
123: }
124:
125: @Override
126: public Collection<Descriptor> getAttributeDescriptors() {
127: return Collections.unmodifiableCollection(fieldDescriptors.values());
128: }
129:
130: private static AbstractDescriptor createDescriptor(FieldSpecification<?, ?> att, Set<URI> contexts) {
131:• if (att instanceof Attribute) {
132: final Attribute<?, ?> attSpec = (Attribute<?, ?>) att;
133:• if (attSpec.getPersistentAttributeType() == PersistentAttributeType.OBJECT) {
134:• if (attSpec.isCollection()) {
135: return new ObjectPropertyCollectionDescriptor(contexts, att);
136: } else {
137: return new EntityDescriptor(contexts);
138: }
139: }
140:• } else if (att instanceof QueryAttribute) {
141: return new EntityDescriptor(contexts);
142: }
143: return new FieldDescriptor(contexts, att);
144: }
145:
146: @Override
147: public boolean equals(Object o) {
148:• if (this == o) {
149: return true;
150: }
151:• if (!(o instanceof EntityDescriptor)) {
152: return false;
153: }
154:• if (!super.equals(o)) {
155: return false;
156: }
157:
158: EntityDescriptor that = (EntityDescriptor) o;
159:
160:• if (fieldDescriptors.size() != that.fieldDescriptors.size()) {
161: return false;
162: }
163:• for (Entry<Field, Descriptor> e : fieldDescriptors.entrySet()) {
164:• if (e.getValue() == null) {
165:• if (that.fieldDescriptors.containsKey(e.getKey()) && that.fieldDescriptors.get(e.getKey()) != null) {
166: return false;
167: }
168: } else {
169:• if (e.getValue() == this && that.fieldDescriptors.get(e.getKey()) == that) {
170: continue;
171: }
172:• if (!e.getValue().equals(that.fieldDescriptors.get(e.getKey()))) {
173: return false;
174: }
175: }
176: }
177: return true;
178: }
179:
180: @Override
181: public int hashCode() {
182: int result = super.hashCode();
183: result = 31 * result + fieldDescriptors.entrySet().stream()
184: .map(e -> e.getKey().hashCode() ^
185:• (e.getValue() == this ? 0 :
186: e.getValue().hashCode())).reduce(0, Integer::sum);
187: return result;
188: }
189: }