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: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
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%
getIdentifier()
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%
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%
resolveAssignableValue(Class, Object)
M: 0 C: 36
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
setFieldValue(Field, Object)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setIdentifierValue(String)
M: 0 C: 40
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
supports(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%

Coverage

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.jsonld.deserialization;
16:
17: import cz.cvut.kbss.jsonld.JsonLd;
18: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
19: import cz.cvut.kbss.jsonld.deserialization.util.DataTypeTransformer;
20: import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
21:
22: import java.lang.reflect.Field;
23: import java.util.Map;
24:
25: abstract class InstanceContext<T> {
26:
27: /**
28: * Blank node id start, as per <a href="https://www.w3.org/TR/turtle/#BNodes">https://www.w3.org/TR/turtle/#BNodes</a>
29: */
30: private static final String BLANK_NODE_ID_START = "_:";
31:
32: T instance;
33:
34: private String identifier;
35:
36: final Map<String, Object> knownInstances;
37:
38: InstanceContext(T instance, Map<String, Object> knownInstances) {
39: this.instance = instance;
40: this.knownInstances = knownInstances;
41: }
42:
43: T getInstance() {
44: return instance;
45: }
46:
47: @SuppressWarnings("unchecked")
48: Class<T> getInstanceType() {
49: return (Class<T>) instance.getClass();
50: }
51:
52: /**
53: * Sets identifier of the instance specified by this context.
54: *
55: * @param value Identifier value
56: */
57: void setIdentifierValue(String value) {
58: final Field idField = getFieldForProperty(JsonLd.ID);
59: final boolean blankNode = isBlankNodeIdentifier(value);
60:• if (idField == null) {
61:• if (blankNode) {
62: return;
63: } else {
64: throw UnknownPropertyException.create(JsonLd.ID, getInstanceType());
65: }
66: }
67:• if (blankNode && !idField.getType().equals(String.class)) {
68: return;
69: }
70: setFieldValue(idField, value);
71: this.identifier = value;
72: knownInstances.put(value, instance);
73: }
74:
75: private static boolean isBlankNodeIdentifier(String identifier) {
76: return identifier.startsWith(BLANK_NODE_ID_START);
77: }
78:
79: /**
80: * Gets the instance identifier.
81: * <p>
82: * Note that the identifier may not be available.
83: *
84: * @return Identifier value, or {@code null} if it is not available (it has not been set or is not applicable in
85: * this context)
86: * @see #setIdentifierValue(String)
87: */
88: String getIdentifier() {
89: return identifier;
90: }
91:
92: // These methods are intended for overriding, because the behaviour is supported only by some context implementations
93:
94: /**
95: * Gets a Java field mapped by the specified property.
96: * <p>
97: * This applies to singular object contexts only.
98: *
99: * @param property Property IRI
100: * @return Field mapped by the specified property. Can be {@code null}
101: */
102: Field getFieldForProperty(String property) {
103: throw new UnsupportedOperationException("Not supported by this type of instance context.");
104: }
105:
106: /**
107: * Sets value of the specified field on the instance represented by this context
108: *
109: * @param field The field to set
110: * @param value The value to set
111: */
112: void setFieldValue(Field field, Object value) {
113: // Do nothing
114: }
115:
116: /**
117: * Adds item to the collection represented by this context.
118: *
119: * @param item Item to add
120: */
121: void addItem(Object item) {
122: // Do nothing
123: }
124:
125: /**
126: * Gets type of the element of a collection represented by this context.
127: *
128: * @return Collection element type
129: */
130: Class<?> getItemType() {
131: throw new UnsupportedOperationException("Not supported by this type of instance context.");
132: }
133:
134: Object resolveAssignableValue(Class<?> targetType, Object value) {
135:• if (value != null && !targetType.isAssignableFrom(value.getClass())) {
136:• if (knownInstances.containsKey(value.toString())) {
137: final Object known = knownInstances.get(value.toString());
138:• if (!targetType.isAssignableFrom(known.getClass())) {
139: return DataTypeTransformer.transformValue(value, targetType);
140: } else {
141: return known;
142: }
143: } else {
144: return DataTypeTransformer.transformValue(value, targetType);
145: }
146: }
147: return value;
148: }
149:
150: /**
151: * Whether the specified property is mapped by a field in this context.
152: * <p>
153: * Note that if the context represents an instance with a {@link cz.cvut.kbss.jopa.model.annotations.Properties}
154: * field, every property is considered mapped
155: *
156: * @param property The property to check
157: * @return {@code true} if a field mapping the property exists in this context, {@code false} otherwise
158: */
159: boolean isPropertyMapped(String property) {
160: // Default behavior
161: return false;
162: }
163:
164: /**
165: * Checks whether the specified property is supported by this context.
166: * <p>
167: * A property is supported by a context for deserialization if it is mapped and the mapped field does not have
168: * read-only access.
169: *
170: * @param property The property to check
171: * @return Whether property is supported by this context
172: * @see #isPropertyMapped(String)
173: * @see cz.cvut.kbss.jsonld.annotation.JsonLdProperty.Access#READ_ONLY
174: */
175: boolean supports(String property) {
176: // Default behavior
177: return false;
178: }
179:
180: /**
181: * Checks whether the class represented by this context contains a {@link cz.cvut.kbss.jopa.model.annotations.Properties}
182: * field.
183: *
184: * @return Whether the represented class has properties field
185: */
186: boolean hasPropertiesField() {
187: return BeanAnnotationProcessor.hasPropertiesField(getInstanceType());
188: }
189:
190: /**
191: * Called when this context is being closed
192: */
193: void close() {
194: // Do nothing by default
195: }
196: }