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