Skip to content

Method: toString()

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.exceptions.AmbiguousContextException;
18: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
19:
20: import java.net.URI;
21: import java.util.*;
22:
23: /**
24: * Defines base descriptor, which is used to specify context information for entities and their fields.
25: * <p>
26: * The descriptor hierarchy is a classical <b>Composite</b> pattern.
27: */
28: public abstract class AbstractDescriptor implements Descriptor {
29:
30: protected final Set<URI> contexts = new HashSet<>(4);
31:
32: protected final boolean assertionsInSubjectContext;
33:
34: private String language;
35: private boolean hasLanguage;
36:
37: protected AbstractDescriptor() {
38: this(true);
39: }
40:
41: protected AbstractDescriptor(boolean assertionsInSubjectContext) {
42: this.assertionsInSubjectContext = assertionsInSubjectContext;
43: }
44:
45: protected AbstractDescriptor(URI context) {
46: this(context, true);
47: }
48:
49: protected AbstractDescriptor(URI context, boolean assertionsInSubjectContext) {
50: if (context != null) {
51: contexts.add(context);
52: }
53: this.assertionsInSubjectContext = assertionsInSubjectContext;
54: }
55:
56: @Override
57: public Set<URI> getContexts() {
58: return Collections.unmodifiableSet(contexts);
59: }
60:
61: @Override
62: public Optional<URI> getSingleContext() {
63: return retrieveSingleContext(contexts);
64: }
65:
66: private static Optional<URI> retrieveSingleContext(Set<URI> col) {
67: if (col.size() > 1) {
68: throw new AmbiguousContextException("Expected at most one context, but got " + col);
69: }
70: return col.isEmpty() ? Optional.empty() : Optional.of(col.iterator().next());
71: }
72:
73: @Override
74: public Descriptor addContext(URI context) {
75: if (context == null) {
76: contexts.clear();
77: } else {
78: contexts.add(context);
79: }
80: return this;
81: }
82:
83: @Override
84: public Optional<URI> getSingleAttributeContext(FieldSpecification<?, ?> attribute) {
85: return retrieveSingleContext(getAttributeContexts(attribute));
86: }
87:
88: @Override
89: public String getLanguage() {
90: return language;
91: }
92:
93: @Override
94: public boolean hasLanguage() {
95: return hasLanguage;
96: }
97:
98: @Override
99: public Descriptor setLanguage(String languageTag) {
100: this.language = languageTag;
101: this.hasLanguage = true;
102: return this;
103: }
104:
105: @Override
106: public Descriptor anyLanguage() {
107: return setLanguage(null);
108: }
109:
110: @Override
111: public boolean areAssertionsInSubjectContext() {
112: return assertionsInSubjectContext;
113: }
114:
115: @Override
116: public boolean equals(Object o) {
117: if (this == o) {
118: return true;
119: }
120: if (!(o instanceof AbstractDescriptor)) {
121: return false;
122: }
123:
124: AbstractDescriptor that = (AbstractDescriptor) o;
125:
126: if (hasLanguage != that.hasLanguage) {
127: return false;
128: }
129: if (assertionsInSubjectContext != that.assertionsInSubjectContext) {
130: return false;
131: }
132: return Objects.equals(contexts, that.contexts) && Objects.equals(language, that.language);
133: }
134:
135: @Override
136: public int hashCode() {
137: int result = contexts.hashCode();
138: result = 31 * result + (language != null ? language.hashCode() : 0);
139: result = 31 * result + (hasLanguage ? 1 : 0);
140: result = 31 * result + (assertionsInSubjectContext ? 1 : 0);
141: return result;
142: }
143:
144: @Override
145: public String toString() {
146:• return contexts.isEmpty() ? "default_context" : contexts.toString();
147: }
148: }