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%
resolveAssignableValue(Class, Object)
M: 0 C: 38
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
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%
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) 2017 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.deserialization;
14:
15: import cz.cvut.kbss.jsonld.JsonLd;
16: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
17: import cz.cvut.kbss.jsonld.deserialization.util.DataTypeTransformer;
18:
19: import java.lang.reflect.Field;
20: import java.util.Map;
21: import java.util.Optional;
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: Optional<Object> resolveAssignableValue(Class<?> targetType, Object value) {
110:• if (!targetType.isAssignableFrom(value.getClass())) {
111:• if (knownInstances.containsKey(value.toString())) {
112: final Object known = knownInstances.get(value.toString());
113:• if (!targetType.isAssignableFrom(known.getClass())) {
114: return Optional.ofNullable(DataTypeTransformer.transformValue(value, targetType));
115: } else {
116: return Optional.of(known);
117: }
118: } else {
119: return Optional.ofNullable(DataTypeTransformer.transformValue(value, targetType));
120: }
121: }
122: return Optional.of(value);
123: }
124:
125: /**
126: * Whether the specified property is mapped by a field in this context.
127: * <p>
128: * Note that if the context represents an instance with a {@link cz.cvut.kbss.jopa.model.annotations.Properties}
129: * field, every property is considered mapped
130: *
131: * @param property The property to check
132: * @return {@code true} if a field mapping the property exists in this context, {@code false} otherwise
133: */
134: boolean isPropertyMapped(String property) {
135: // Default behavior
136: return false;
137: }
138:
139: /**
140: * Checks whether the specified property is supported by this context.
141: * <p>
142: * A property is supported by a context for deserialization if it is mapped and the mapped field does not have
143: * read-only access.
144: *
145: * @param property The property to check
146: * @return Whether property is supported by this context
147: * @see #isPropertyMapped(String)
148: * @see cz.cvut.kbss.jsonld.annotation.JsonLdProperty.Access#READ_ONLY
149: */
150: boolean supports(String property) {
151: // Default behavior
152: return false;
153: }
154:
155: /**
156: * Checks whether the class represented by this context contains a {@link cz.cvut.kbss.jopa.model.annotations.Properties}
157: * field.
158: *
159: * @return Whether the represented class has properties field
160: */
161: boolean hasPropertiesField() {
162: return BeanAnnotationProcessor.hasPropertiesField(getInstanceType());
163: }
164:
165: /**
166: * Called when this context is being closed
167: */
168: void close() {
169: // Do nothing by default
170: }
171: }