Skip to content

Package: InstanceContext

InstanceContext

nameinstructionbranchcomplexitylinemethod
InstanceContext(Object, Map)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addItem(Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
close()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getFieldForProperty(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInstance()
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%
getInstanceType()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getItemType()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasPropertiesField()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isBlankNodeIdentifier(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isPropertyMapped(String)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setFieldValue(Field, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setIdentifierValue(Object)
M: 4 C: 31
89%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 7
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2017 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.jsonld.deserialization;
16:
17: import cz.cvut.kbss.jsonld.JsonLd;
18: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
19:
20: import java.lang.reflect.Field;
21: import java.util.Map;
22:
23: abstract class InstanceContext<T> {
24:
25: /**
26: * Blank node id start, as per <a href="https://www.w3.org/TR/turtle/#BNodes">https://www.w3.org/TR/turtle/#BNodes</a>
27: */
28: private static final String BLANK_NODE_ID_START = "_:";
29:
30: T instance;
31:
32: final Map<String, Object> knownInstances;
33:
34: InstanceContext(T instance, Map<String, Object> knownInstances) {
35: this.instance = instance;
36: this.knownInstances = knownInstances;
37: }
38:
39: T getInstance() {
40: return instance;
41: }
42:
43: @SuppressWarnings("unchecked")
44: Class<T> getInstanceType() {
45: return (Class<T>) instance.getClass();
46: }
47:
48: /**
49: * Sets identifier of the instance specified by this context.
50: *
51: * @param value Identifier value
52: */
53: void setIdentifierValue(Object value) {
54: final Field idField = getFieldForProperty(JsonLd.ID);
55:• assert idField != null;
56:• if (isBlankNodeIdentifier(value.toString()) && !idField.getType().equals(String.class)) {
57: return;
58: }
59: setFieldValue(idField, value);
60: knownInstances.put(value.toString(), instance);
61: }
62:
63: private boolean isBlankNodeIdentifier(String identifier) {
64: return identifier.startsWith(BLANK_NODE_ID_START);
65: }
66:
67: // These methods are intended for overriding, because the behaviour is supported only by some context implementations
68:
69: /**
70: * Gets a Java field mapped by the specified property.
71: * <p>
72: * This applies to singular object contexts only.
73: *
74: * @param property Property IRI
75: * @return Field mapped by the specified property. Can be {@code null}
76: */
77: Field getFieldForProperty(String property) {
78: throw new UnsupportedOperationException("Not supported by this type of instance context.");
79: }
80:
81: /**
82: * Sets value of the specified field on the instance represented by this context
83: *
84: * @param field The field to set
85: * @param value The value to set
86: */
87: void setFieldValue(Field field, Object value) {
88: throw new UnsupportedOperationException("Not supported by this type of instance context.");
89: }
90:
91: /**
92: * Adds item to the collection represented by this context.
93: *
94: * @param item Item to add
95: */
96: void addItem(Object item) {
97: throw new UnsupportedOperationException("Not supported by this type of instance context.");
98: }
99:
100: /**
101: * Gets type of the element type of a collection represented by this context.
102: *
103: * @return Collection element type
104: */
105: Class<?> getItemType() {
106: throw new UnsupportedOperationException("Not supported by this type of instance context.");
107: }
108:
109: /**
110: * Whether the specified property is mapped by a field in this context.
111: * <p>
112: * Note that if the context represents an instance with a {@link cz.cvut.kbss.jopa.model.annotations.Properties}
113: * field, every property is considered mapped
114: *
115: * @param property The property to check
116: * @return {@code true} if a field mapping the property exists in this context, {@code false} otherwise
117: */
118: boolean isPropertyMapped(String property) {
119: // Default behaviour
120: return false;
121: }
122:
123: /**
124: * Checks whether the class represented by this context contains a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
125: *
126: * @return Whether the represented class has properties field
127: */
128: boolean hasPropertiesField() {
129: return BeanAnnotationProcessor.hasPropertiesField(getInstanceType());
130: }
131:
132: /**
133: * Called when this context is being closed
134: */
135: void close() {
136: // Do nothing by default
137: }
138: }