Skip to content

Package: Descriptor

Descriptor

nameinstructionbranchcomplexitylinemethod
Descriptor()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
Descriptor(URI)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
anyLanguage()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
equals(Object)
M: 5 C: 46
90%
M: 4 C: 12
75%
M: 4 C: 5
56%
M: 0 C: 6
100%
M: 0 C: 1
100%
getAllContexts()
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getContext()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLanguage()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasLanguage()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode()
M: 0 C: 35
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setLanguage(String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
toString()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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.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.HashSet;
24: import java.util.Set;
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 Descriptor {
32:
33: protected final URI context;
34:
35: private String language;
36: private boolean hasLanguage;
37:
38: protected Descriptor() {
39: this(null);
40: }
41:
42: protected Descriptor(URI context) {
43: this.context = context;
44: }
45:
46: /**
47: * Gets context for this descriptor.
48: * <p>
49: * Note that the context URI may be {@code null}, meaning that the default context is referenced
50: *
51: * @return Context URI
52: */
53: public URI getContext() {
54: return context;
55: }
56:
57: /**
58: * Gets the language set for this descriptor.
59: *
60: * @return Language tag (e.g. en, cs), can be {@code null}, meaning any language is supported or the language tag
61: * has not been set (see {@link #hasLanguage()})
62: */
63: public String getLanguage() {
64: return language;
65: }
66:
67: /**
68: * Gets information about whether language tag has been set on this descriptor.
69: * <p>
70: * The language tag can be explicitly set to {@code null}, meaning any language is supported. This can be used
71: * to override PU-level language setting.
72: *
73: * @return {@code true} if a language tag has been set on this descriptor, {@code false} otherwise
74: */
75: public boolean hasLanguage() {
76: return hasLanguage;
77: }
78:
79: /**
80: * Sets language tag of this descriptor.
81: * <p>
82: * Applies to any possible sub-descriptors as well.
83: *
84: * @param languageTag The language tag to use, possibly {@code null}, meaning no language preference should be used
85: * @see #anyLanguage()
86: */
87: public void setLanguage(String languageTag) {
88: this.language = languageTag;
89: this.hasLanguage = true;
90: }
91:
92: /**
93: * Configures this descriptor to support any language tag (including no language tags).
94: * <p>
95: * This is useful for overriding previously set language tag expectations (either on PU level or parent descriptor
96: * level).
97: * <p>
98: * This does the same as calling {@link #setLanguage(String)} with {@code null} argument, but is more explicit.
99: */
100: public void anyLanguage() {
101: setLanguage(null);
102: }
103:
104: /**
105: * Gets attribute descriptors specified in this descriptor.
106: *
107: * @return Unmodifiable view of attribute descriptors
108: */
109: public abstract Collection<Descriptor> getAttributeDescriptors();
110:
111: /**
112: * Gets descriptor for the specified attribute.
113: *
114: * @param attribute Entity attribute, as specified by the application metamodel
115: * @return Descriptor
116: * @throws IllegalArgumentException If the descriptor is not available
117: */
118: public abstract Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute);
119:
120: /**
121: * Adds descriptor for the specified attribute.
122: *
123: * @param attribute The attribute to set descriptor for
124: * @param descriptor The descriptor to use
125: */
126: public abstract void addAttributeDescriptor(Field attribute, Descriptor descriptor);
127:
128: /**
129: * Adds repository context for the specified attribute.
130: * <p>
131: * This in effect means creating a descriptor for the specified field with the specified context.
132: *
133: * @param attribute The attribute to set context for
134: * @param context The context to set
135: * @see #addAttributeDescriptor(Field, Descriptor)
136: */
137: public abstract void addAttributeContext(Field attribute, URI context);
138:
139: /**
140: * Sets language to be used when working (retrieving, persisting) with values of the specified attribute.
141: * <p>
142: * Note that setting language in this manner will not have any effect on descriptors of the
143: * specified attribute previously retrieved from this descriptor.
144: *
145: * @param attribute The attribute concerned
146: * @param languageTag Language tag to use, possibly {@code null}
147: */
148: public abstract void setAttributeLanguage(Field attribute, String languageTag);
149:
150: /**
151: * Gets all contexts present in this descriptor.
152: * <p>
153: * If any of the descriptors specifies the default context, an empty set is returned.
154: * <p>
155: * In case of entity descriptor this means recursively asking all of its attributes for their context.
156: *
157: * @return Set of context URIs or an empty set, if the default one should be used
158: */
159: public Set<URI> getAllContexts() {
160: Set<URI> contexts = new HashSet<>();
161: contexts = getContextsInternal(contexts, new HashSet<>());
162:• return contexts != null ? contexts : Collections.emptySet();
163: }
164:
165: /**
166: * Gets the contexts, discarding any already visited descriptors.
167: *
168: * @param contexts Contexts collected so far
169: * @param visited Visited descriptors
170: * @return Already visited contexts + those found in this descriptor
171: */
172: protected abstract Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited);
173:
174: @Override
175: public boolean equals(Object o) {
176:• if (this == o) return true;
177:• if (!(o instanceof Descriptor)) return false;
178:
179: Descriptor that = (Descriptor) o;
180:
181:• if (hasLanguage != that.hasLanguage) return false;
182:• if (context != null ? !context.equals(that.context) : that.context != null) return false;
183:• return language != null ? language.equals(that.language) : that.language == null;
184: }
185:
186: @Override
187: public int hashCode() {
188:• int result = context != null ? context.hashCode() : 0;
189:• result = 31 * result + (language != null ? language.hashCode() : 0);
190:• result = 31 * result + (hasLanguage ? 1 : 0);
191: return result;
192: }
193:
194: @Override
195: public String toString() {
196:• return context != null ? context.toString() : "default_context";
197: }
198: }