Skip to content

Method: getFieldDescriptor(Field)

1: /**
2: * Copyright (C) 2022 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 AbstractDescriptor {
30:
31: private final Field field;
32:
33: public FieldDescriptor(FieldSpecification<?, ?> attribute) {
34: this.field = Objects.requireNonNull(attribute).getJavaField();
35: }
36:
37: public FieldDescriptor(URI context, FieldSpecification<?, ?> attribute) {
38: super(context);
39: this.field = Objects.requireNonNull(attribute).getJavaField();
40: }
41:
42: public FieldDescriptor(Set<URI> contexts, FieldSpecification<?, ?> attribute) {
43: this(attribute);
44: this.contexts.addAll(Objects.requireNonNull(contexts));
45: }
46:
47: @Override
48: public Collection<Descriptor> getAttributeDescriptors() {
49: return Collections.singleton(this);
50: }
51:
52: @Override
53: public Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute) {
54: Objects.requireNonNull(attribute);
55: return getFieldDescriptor(attribute.getJavaField());
56: }
57:
58: @Override
59: public Set<URI> getAttributeContexts(FieldSpecification<?, ?> attribute) {
60: Objects.requireNonNull(attribute);
61: return getFieldDescriptor(attribute.getJavaField()).getContexts();
62: }
63:
64: @Override
65: public FieldDescriptor addAttributeDescriptor(FieldSpecification<?, ?> attribute, Descriptor descriptor) {
66: // Do nothing
67: return this;
68: }
69:
70: @Override
71: public FieldDescriptor addAttributeContext(FieldSpecification<?, ?> attribute, URI context) {
72: // Do nothing
73: return this;
74: }
75:
76: /**
77: * Use {@link #setLanguage(String)} instead.
78: */
79: @Override
80: public FieldDescriptor setAttributeLanguage(FieldSpecification<?, ?> attribute, String languageTag) {
81: // Do nothing
82: return this;
83: }
84:
85: private AbstractDescriptor getFieldDescriptor(Field field) {
86:• if (this.field.equals(field)) {
87: return this;
88: }
89: throw new IllegalArgumentException("This field descriptor does not describe field " + field);
90: }
91:
92: Field getField() {
93: return field;
94: }
95:
96: @Override
97: public boolean overridesAssertionContext() {
98: return true;
99: }
100:
101: @Override
102: public int hashCode() {
103: final int prime = 31;
104: int result = super.hashCode();
105: result = prime * result + field.hashCode();
106: return result;
107: }
108:
109: @Override
110: public boolean equals(Object obj) {
111: if (this == obj)
112: return true;
113: if (!super.equals(obj))
114: return false;
115: if (getClass() != obj.getClass())
116: return false;
117: FieldDescriptor other = (FieldDescriptor) obj;
118: return field.equals(other.field);
119: }
120: }