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%
equals(Object)
M: 16 C: 21
57%
M: 7 C: 5
42%
M: 6 C: 1
14%
M: 6 C: 7
54%
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%
hashCode()
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
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: * <p>
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: * <p>
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: * @author ledvima1
32: */
33: public abstract class Descriptor {
34:
35: protected final URI context;
36:
37: protected Descriptor() {
38: this(null);
39: }
40:
41: protected Descriptor(URI context) {
42: this.context = context;
43: }
44:
45: /**
46: * Gets context for this descriptor.
47: * <p>
48: * Note that the context URI may be {@code null}, meaning that the default context is referenced
49: *
50: * @return Context URI
51: */
52: public URI getContext() {
53: return context;
54: }
55:
56: /**
57: * Gets attribute descriptors specified in this descriptor.
58: *
59: * @return Unmodifiable view of attribute descriptors
60: */
61: public abstract Collection<Descriptor> getAttributeDescriptors();
62:
63: /**
64: * Gets descriptor for the specified attribute.
65: *
66: * @param attribute Entity attribute, as specified by the application metamodel
67: * @return Descriptor
68: * @throws IllegalArgumentException If the descriptor is not available
69: */
70: public abstract Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute);
71:
72: /**
73: * Adds descriptor for the specified attribute.
74: *
75: * @param attribute The attribute to set descriptor for
76: * @param descriptor The descriptor to use
77: */
78: public abstract void addAttributeDescriptor(Field attribute, Descriptor descriptor);
79:
80: /**
81: * Adds repository context for the specified attribute.
82: * <p>
83: * This in effect means creating a descriptor for the specified field with the specified context.
84: *
85: * @param attribute The attribute to set context for
86: * @param context The context to set
87: * @see #addAttributeDescriptor(Field, Descriptor)
88: */
89: public abstract void addAttributeContext(Field attribute, URI context);
90:
91: /**
92: * Gets all contexts present in this descriptor.
93: * <p>
94: * If any of the descriptors specifies the default context, an empty set is returned.
95: * <p>
96: * In case of entity descriptor this means recursively asking all of its attributes for their context.
97: *
98: * @return Set of context URIs or an empty set, if the default one should be used
99: */
100: public Set<URI> getAllContexts() {
101: Set<URI> contexts = new HashSet<>();
102: contexts = getContextsInternal(contexts, new HashSet<>());
103:• return contexts != null ? contexts : Collections.<URI>emptySet();
104: }
105:
106: /**
107: * Gets the contexts, discarding any already visited descriptors.
108: *
109: * @param contexts Contexts collected so far
110: * @param visited Visited descriptors
111: * @return Already visited contexts + those found in this descriptor
112: */
113: protected abstract Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited);
114:
115: @Override
116: public int hashCode() {
117: final int prime = 31;
118: int result = 1;
119:• result = prime * result + ((context == null) ? 0 : context.hashCode());
120: return result;
121: }
122:
123: @Override
124: public boolean equals(Object obj) {
125:• if (this == obj)
126: return true;
127:• if (obj == null)
128: return false;
129:• if (getClass() != obj.getClass())
130: return false;
131: Descriptor other = (Descriptor) obj;
132:• if (context == null) {
133:• if (other.context != null)
134: return false;
135:• } else if (!context.equals(other.context))
136: return false;
137: return true;
138: }
139:
140: @Override
141: public String toString() {
142:• return context != null ? context.toString() : "default_context";
143: }
144: }