Skip to content

Method: AbstractDescriptor(URI)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model.descriptors;
19:
20: import cz.cvut.kbss.jopa.exceptions.AmbiguousContextException;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22:
23: import java.net.URI;
24: import java.util.*;
25:
26: /**
27: * Defines base descriptor, which is used to specify context information for entities and their fields.
28: * <p>
29: * The descriptor hierarchy is a classical <b>Composite</b> pattern.
30: */
31: public abstract class AbstractDescriptor implements Descriptor {
32:
33: protected final Set<URI> contexts = new HashSet<>(4);
34:
35: protected final boolean assertionsInSubjectContext;
36:
37: private String language;
38: private boolean hasLanguage;
39:
40: private boolean includeInferred = true;
41:
42: protected AbstractDescriptor() {
43: this(true);
44: }
45:
46: protected AbstractDescriptor(boolean assertionsInSubjectContext) {
47: this.assertionsInSubjectContext = assertionsInSubjectContext;
48: }
49:
50: protected AbstractDescriptor(URI context) {
51: this(context, true);
52: }
53:
54: protected AbstractDescriptor(URI context, boolean assertionsInSubjectContext) {
55: if (context != null) {
56: contexts.add(context);
57: }
58: this.assertionsInSubjectContext = assertionsInSubjectContext;
59: }
60:
61: @Override
62: public Set<URI> getContexts() {
63: return Collections.unmodifiableSet(contexts);
64: }
65:
66: @Override
67: public Optional<URI> getSingleContext() {
68: return retrieveSingleContext(contexts);
69: }
70:
71: private static Optional<URI> retrieveSingleContext(Set<URI> col) {
72: if (col.size() > 1) {
73: throw new AmbiguousContextException("Expected at most one context, but got " + col);
74: }
75: return col.isEmpty() ? Optional.empty() : Optional.of(col.iterator().next());
76: }
77:
78: @Override
79: public Descriptor addContext(URI context) {
80: if (context == null) {
81: contexts.clear();
82: } else {
83: contexts.add(context);
84: }
85: return this;
86: }
87:
88: @Override
89: public Optional<URI> getSingleAttributeContext(FieldSpecification<?, ?> attribute) {
90: return retrieveSingleContext(getAttributeContexts(attribute));
91: }
92:
93: @Override
94: public String getLanguage() {
95: return language;
96: }
97:
98: @Override
99: public boolean hasLanguage() {
100: return hasLanguage;
101: }
102:
103: @Override
104: public Descriptor setLanguage(String languageTag) {
105: this.language = languageTag;
106: this.hasLanguage = true;
107: return this;
108: }
109:
110: @Override
111: public Descriptor anyLanguage() {
112: return setLanguage(null);
113: }
114:
115: @Override
116: public boolean areAssertionsInSubjectContext() {
117: return assertionsInSubjectContext;
118: }
119:
120: @Override
121: public boolean includeInferred() {
122: return includeInferred;
123: }
124:
125: @Override
126: public Descriptor disableInference() {
127: this.includeInferred = false;
128: return this;
129: }
130:
131: @Override
132: public Descriptor enableInference() {
133: this.includeInferred = true;
134: return this;
135: }
136:
137: protected void setIncludeInferred(boolean includeInferred) {
138: this.includeInferred = includeInferred;
139: }
140:
141: @Override
142: public boolean equals(Object o) {
143: if (this == o) {
144: return true;
145: }
146: if (!(o instanceof AbstractDescriptor)) {
147: return false;
148: }
149:
150: AbstractDescriptor that = (AbstractDescriptor) o;
151:
152: if (hasLanguage != that.hasLanguage) {
153: return false;
154: }
155: if (assertionsInSubjectContext != that.assertionsInSubjectContext) {
156: return false;
157: }
158: return Objects.equals(contexts, that.contexts) && Objects.equals(language, that.language);
159: }
160:
161: @Override
162: public int hashCode() {
163: int result = contexts.hashCode();
164: result = 31 * result + (language != null ? language.hashCode() : 0);
165: result = 31 * result + (hasLanguage ? 1 : 0);
166: result = 31 * result + (assertionsInSubjectContext ? 1 : 0);
167: return result;
168: }
169:
170: @Override
171: public String toString() {
172: return contexts.isEmpty() ? "default_context" : contexts.toString();
173: }
174: }