Skip to contentMethod: getAttributeDescriptors()
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.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: * @author ledvima1
35: */
36: public class EntityDescriptor extends Descriptor {
37:
38: private final Map<Field, Descriptor> fieldDescriptors;
39:
40: public EntityDescriptor() {
41: this.fieldDescriptors = new HashMap<>();
42: }
43:
44: public EntityDescriptor(URI context) {
45: super(context);
46: this.fieldDescriptors = new HashMap<>();
47: }
48:
49: @Override
50: public void addAttributeDescriptor(Field attribute, Descriptor descriptor) {
51: Objects.requireNonNull(attribute, ErrorUtils.constructNPXMessage("attribute"));
52: Objects.requireNonNull(descriptor, ErrorUtils.constructNPXMessage("descriptor"));
53:
54: fieldDescriptors.put(attribute, descriptor);
55: }
56:
57: @Override
58: public void addAttributeContext(Field attribute, URI context) {
59: Objects.requireNonNull(attribute, ErrorUtils.constructNPXMessage("attribute"));
60:
61: fieldDescriptors.put(attribute, new FieldDescriptor(context, attribute));
62: }
63:
64: @Override
65: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
66: Descriptor d = getFieldDescriptor(attribute.getJavaField());
67: if (d == null) {
68: d = createDescriptor(attribute, context);
69: }
70: return d;
71: }
72:
73: @Override
74: public Collection<Descriptor> getAttributeDescriptors() {
75: return Collections.unmodifiableCollection(fieldDescriptors.values());
76: }
77:
78: private Descriptor getFieldDescriptor(Field field) {
79: for (Entry<Field, Descriptor> e : fieldDescriptors.entrySet()) {
80: if (e.getKey().equals(field)) {
81: return e.getValue();
82: }
83: }
84: return null;
85: }
86:
87: private static Descriptor createDescriptor(FieldSpecification<?, ?> att, URI context) {
88: if ((att instanceof TypesSpecification) || (att instanceof PropertiesSpecification)) {
89: return new FieldDescriptor(context, att.getJavaField());
90: }
91: final Attribute<?, ?> attSpec = (Attribute<?, ?>) att;
92: if (attSpec.getPersistentAttributeType() == PersistentAttributeType.OBJECT) {
93: if (attSpec.isCollection()) {
94: return new ObjectPropertyCollectionDescriptor(context, att.getJavaField());
95: } else {
96: return new EntityDescriptor(context);
97: }
98: }
99: return new FieldDescriptor(context, att.getJavaField());
100: }
101:
102: @Override
103: protected Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited) {
104: if (visited.contains(this)) {
105: return contexts;
106: }
107: if (context == null) {
108: return null;
109: }
110: contexts.add(context);
111: visited.add(this);
112: for (Descriptor fd : fieldDescriptors.values()) {
113: contexts = fd.getContextsInternal(contexts, visited);
114: if (contexts == null) {
115: return null;
116: }
117: }
118: return contexts;
119: }
120:
121: @Override
122: public int hashCode() {
123: final int prime = 31;
124: int result = super.hashCode();
125: result = prime * result + ((fieldDescriptors == null) ? 0 : fieldDescriptors.hashCode());
126: return result;
127: }
128:
129: @Override
130: public boolean equals(Object obj) {
131: if (this == obj)
132: return true;
133: if (!super.equals(obj))
134: return false;
135: if (getClass() != obj.getClass())
136: return false;
137: EntityDescriptor other = (EntityDescriptor) obj;
138: if (fieldDescriptors == null) {
139: if (other.fieldDescriptors != null)
140: return false;
141: } else if (!fieldDescriptors.equals(other.fieldDescriptors))
142: return false;
143: return true;
144: }
145:
146: }