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(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%
addAttributeContext(Field, URI)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addAttributeDescriptor(Field, Descriptor)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createDescriptor(FieldSpecification, URI)
M: 0 C: 42
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
equals(Object)
M: 11 C: 28
72%
M: 6 C: 6
50%
M: 5 C: 2
29%
M: 5 C: 8
62%
M: 0 C: 1
100%
getAttributeDescriptor(FieldSpecification)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getAttributeDescriptors()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getContextsInternal(Set, Set)
M: 44 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getFieldDescriptor(Field)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
hashCode()
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.PropertiesSpecification;
21: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
22: import cz.cvut.kbss.jopa.utils.ErrorUtils;
23:
24: import java.lang.reflect.Field;
25: import java.net.URI;
26: import java.util.*;
27: import java.util.Map.Entry;
28:
29: /**
30: * Describes an entity.
31: * <p>
32: * Each attribute has a descriptor associated with it.
33: */
34: public class EntityDescriptor extends Descriptor {
35:
36: private final Map<Field, Descriptor> fieldDescriptors;
37:
38: public EntityDescriptor() {
39: this.fieldDescriptors = new HashMap<>();
40: }
41:
42: public EntityDescriptor(URI context) {
43: super(context);
44: this.fieldDescriptors = new HashMap<>();
45: }
46:
47: @Override
48: public void addAttributeDescriptor(Field attribute, Descriptor descriptor) {
49: Objects.requireNonNull(attribute, ErrorUtils.getNPXMessageSupplier("attribute"));
50: Objects.requireNonNull(descriptor, ErrorUtils.getNPXMessageSupplier("descriptor"));
51:
52: fieldDescriptors.put(attribute, descriptor);
53: }
54:
55: @Override
56: public void addAttributeContext(Field attribute, URI context) {
57: Objects.requireNonNull(attribute, ErrorUtils.getNPXMessageSupplier("attribute"));
58:
59: fieldDescriptors.put(attribute, new FieldDescriptor(context, attribute));
60: }
61:
62: @Override
63: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
64: Descriptor d = getFieldDescriptor(attribute.getJavaField());
65:• if (d == null) {
66: d = createDescriptor(attribute, context);
67: }
68: return d;
69: }
70:
71: @Override
72: public Collection<Descriptor> getAttributeDescriptors() {
73: return Collections.unmodifiableCollection(fieldDescriptors.values());
74: }
75:
76: private Descriptor getFieldDescriptor(Field field) {
77:• for (Entry<Field, Descriptor> e : fieldDescriptors.entrySet()) {
78:• if (e.getKey().equals(field)) {
79: return e.getValue();
80: }
81: }
82: return null;
83: }
84:
85: private static Descriptor createDescriptor(FieldSpecification<?, ?> att, URI context) {
86:• if ((att instanceof TypesSpecification) || (att instanceof PropertiesSpecification)) {
87: return new FieldDescriptor(context, att.getJavaField());
88: }
89: final Attribute<?, ?> attSpec = (Attribute<?, ?>) att;
90:• if (attSpec.getPersistentAttributeType() == PersistentAttributeType.OBJECT) {
91:• if (attSpec.isCollection()) {
92: return new ObjectPropertyCollectionDescriptor(context, att.getJavaField());
93: } else {
94: return new EntityDescriptor(context);
95: }
96: }
97: return new FieldDescriptor(context, att.getJavaField());
98: }
99:
100: @Override
101: protected Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited) {
102:• if (visited.contains(this)) {
103: return contexts;
104: }
105:• if (context == null) {
106: return null;
107: }
108: contexts.add(context);
109: visited.add(this);
110:• for (Descriptor fd : fieldDescriptors.values()) {
111: contexts = fd.getContextsInternal(contexts, visited);
112:• if (contexts == null) {
113: return null;
114: }
115: }
116: return contexts;
117: }
118:
119: @Override
120: public int hashCode() {
121: final int prime = 31;
122: int result = super.hashCode();
123:• result = prime * result + ((fieldDescriptors == null) ? 0 : fieldDescriptors.hashCode());
124: return result;
125: }
126:
127: @Override
128: public boolean equals(Object obj) {
129:• if (this == obj)
130: return true;
131:• if (!super.equals(obj))
132: return false;
133:• if (getClass() != obj.getClass())
134: return false;
135: EntityDescriptor other = (EntityDescriptor) obj;
136:• if (fieldDescriptors == null) {
137:• if (other.fieldDescriptors != null)
138: return false;
139:• } else if (!fieldDescriptors.equals(other.fieldDescriptors))
140: return false;
141: return true;
142: }
143:
144: }