Skip to content

Method: isTraversable(Object)

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.serialization.traversal;
16:
17: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
18: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
19:
20: import java.util.Collection;
21: import java.util.Map;
22: import java.util.Objects;
23:
24: /**
25: * Serializes a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
26: * <p>
27: * Note that at the moment, when the map also contains a property which is already mapped by another field, a conflict in
28: * the resulting JSON-LD will arise.
29: */
30: class PropertiesTraverser {
31:
32: private final ObjectGraphTraverser parent;
33:
34: PropertiesTraverser(ObjectGraphTraverser parent) {
35: this.parent = parent;
36: }
37:
38: public void traverseProperties(SerializationContext<Map<?, ?>> ctx) {
39: for (Map.Entry<?, ?> e : ctx.getValue().entrySet()) {
40: final String property = e.getKey().toString();
41: if (e.getValue() == null) {
42: continue;
43: }
44: if (e.getValue() instanceof Collection) {
45: final Collection<?> propertyValues = (Collection<?>) e.getValue();
46: serializePropertyValues(property, propertyValues);
47: } else {
48: visitSingleValue(property, e.getValue());
49: }
50: }
51: }
52:
53: private void visitSingleValue(String property, Object value) {
54: assert value != null;
55: if (isTraversable(value)) {
56: parent.traverseSingular(new SerializationContext<>(property, value));
57: } else {
58: parent.visitAttribute(new SerializationContext<>(property, value));
59: }
60: }
61:
62: private static boolean isTraversable(Object value) {
63: final Class<?> cls = value.getClass();
64:• return (BeanClassProcessor.isIdentifierType(value.getClass()) && !String.class.equals(cls)) ||
65:• BeanAnnotationProcessor.isOwlClassEntity(value.getClass()) ||
66:• BeanAnnotationProcessor.hasTypesField(cls);
67: }
68:
69: private void serializePropertyValues(String property, Collection<?> values) {
70: if (values.isEmpty()) {
71: return;
72: }
73: if (values.size() == 1) {
74: final Object val = values.iterator().next();
75: if (val != null) {
76: visitSingleValue(property, val);
77: }
78: } else {
79: final SerializationContext<Collection<?>> colContext = new SerializationContext<>(property, values);
80: parent.openCollection(colContext);
81: values.stream().filter(Objects::nonNull).forEach(v -> visitSingleValue(null, v));
82: parent.closeCollection(colContext);
83: }
84: }
85: }