Skip to content

Method: getAttributeDescriptors()

1: /**
2: * Copyright (C) 2020 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.FieldSpecification;
18:
19: import java.lang.reflect.Field;
20: import java.net.URI;
21: import java.util.Collection;
22: import java.util.Collections;
23: import java.util.Objects;
24: import java.util.Set;
25:
26: /**
27: * Describes a singular data property or a plural data, object or annotation property field.
28: */
29: public class FieldDescriptor extends Descriptor {
30:
31: private final Field field;
32:
33: public FieldDescriptor(Field attribute) {
34: this.field = Objects.requireNonNull(attribute);
35: }
36:
37: public FieldDescriptor(URI context, Field attribute) {
38: super(context);
39: this.field = Objects.requireNonNull(attribute);
40: }
41:
42: @Override
43: public Collection<Descriptor> getAttributeDescriptors() {
44: return Collections.singleton(this);
45: }
46:
47: @Override
48: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
49: Objects.requireNonNull(attribute);
50: return getFieldDescriptor(attribute.getJavaField());
51: }
52:
53: @Override
54: public URI getAttributeContext(FieldSpecification<?, ?> attribute) {
55: Objects.requireNonNull(attribute);
56: return getFieldDescriptor(attribute.getJavaField()).getContext();
57: }
58:
59: @Override
60: public FieldDescriptor addAttributeDescriptor(Field attribute, Descriptor descriptor) {
61: // Do nothing
62: return this;
63: }
64:
65: @Override
66: public FieldDescriptor addAttributeContext(Field attribute, URI context) {
67: // Do nothing
68: return this;
69: }
70:
71: /**
72: * Use {@link #setLanguage(String)} instead.
73: */
74: @Override
75: public FieldDescriptor setAttributeLanguage(Field attribute, String languageTag) {
76: // Do nothing
77: return this;
78: }
79:
80: private Descriptor getFieldDescriptor(Field field) {
81: if (this.field.equals(field)) {
82: return this;
83: }
84: throw new IllegalArgumentException("This field descriptor does not describe field " + field);
85: }
86:
87: @Override
88: protected Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited) {
89: if (context == null) {
90: return null;
91: }
92: contexts.add(context);
93: visited.add(this);
94: return contexts;
95: }
96:
97: Field getField() {
98: return field;
99: }
100:
101: @Override
102: protected boolean overridesAssertionsInSubjectContext() {
103: return true;
104: }
105:
106: @Override
107: public int hashCode() {
108: final int prime = 31;
109: int result = super.hashCode();
110: result = prime * result + field.hashCode();
111: return result;
112: }
113:
114: @Override
115: public boolean equals(Object obj) {
116: if (this == obj)
117: return true;
118: if (!super.equals(obj))
119: return false;
120: if (getClass() != obj.getClass())
121: return false;
122: FieldDescriptor other = (FieldDescriptor) obj;
123: return field.equals(other.field);
124: }
125: }