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