Skip to content

Method: resolveReferences(String, Object)

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization.reference;
19:
20: import cz.cvut.kbss.jsonld.exception.UnresolvedReferenceException;
21:
22: import java.lang.reflect.Field;
23: import java.util.*;
24:
25: /**
26: * Registry of pending references.
27: */
28: public class PendingReferenceRegistry {
29:
30: private final Map<String, Set<PendingReference>> pendingReferences = new HashMap<>();
31:
32: /**
33: * Registers a pending reference with the specified identifier.
34: *
35: * @param identifier Reference identifier
36: * @param targetObject Object which contains the referring attribute
37: * @param targetField Field representing the target attribute
38: */
39: public void addPendingReference(String identifier, Object targetObject, Field targetField) {
40: assert identifier != null;
41: assert targetObject != null;
42: assert targetField != null;
43: addReference(identifier, new SingularPendingReference(targetObject, targetField));
44: }
45:
46: private void addReference(String identifier, PendingReference reference) {
47: final Set<PendingReference> refs = pendingReferences.computeIfAbsent(identifier, (id) -> new LinkedHashSet<>());
48: refs.add(reference);
49: }
50:
51: /**
52: * Registers a pending reference with the specified identifier.
53: *
54: * @param identifier Reference identifier
55: * @param targetObject Collection referencing the object
56: */
57: public void addPendingReference(String identifier, Collection targetObject) {
58: assert identifier != null;
59: assert targetObject != null;
60: addReference(identifier, new CollectionPendingReference(targetObject));
61: }
62:
63: /**
64: * Resolves the pending references by replacing them with the specified full object.
65: * <p>
66: * This method goes through the pending references and sets the specified {@code referencedObject} on the
67: * corresponding target objects.
68: *
69: * @param identifier Identifier of the referenced object
70: * @param referencedObject The referenced object
71: * @throws cz.cvut.kbss.jsonld.exception.TargetTypeException If the {@code referencedObject} cannot be assigned to a
72: * target field due to type mismatch
73: */
74: public void resolveReferences(String identifier, Object referencedObject) {
75:• assert identifier != null;
76:• assert referencedObject != null;
77: final Set<PendingReference> refs = pendingReferences.remove(identifier);
78:• if (refs != null) {
79: refs.forEach(pr -> pr.apply(referencedObject));
80: }
81: }
82:
83: /**
84: * Checks whether any pending unresolved references are left.
85: *
86: * @throws UnresolvedReferenceException Thrown when pending references exist
87: */
88: public void verifyNoUnresolvedReferencesExist() {
89: if (!pendingReferences.isEmpty()) {
90: throw new UnresolvedReferenceException(
91: "There are unresolved references to objects " + pendingReferences.keySet());
92: }
93: }
94:
95: Map<String, Set<PendingReference>> getPendingReferences() {
96: return Collections.unmodifiableMap(pendingReferences);
97: }
98: }