-
Notifications
You must be signed in to change notification settings - Fork 45
/
DiagramRenderingCache.java
121 lines (95 loc) · 4.14 KB
/
DiagramRenderingCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.diagrams.renderer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.eclipse.sirius.components.diagrams.elements.NodeElementProps;
import org.eclipse.sirius.components.representations.Element;
/**
* Cache used during the rendering of a diagram.
*
* @author sbegaudeau
*/
public class DiagramRenderingCache {
private final Map<UUID, List<Element>> nodeDescriptionIdToNodes = new LinkedHashMap<>();
private final Map<String, Element> nodeIdToNode = new LinkedHashMap<>();
private final Map<Element, Object> nodeToObject = new LinkedHashMap<>();
private final Map<Object, List<Element>> objectToNodes = new LinkedHashMap<>();
private final Map<Element, String> nodeToParentElementId = new LinkedHashMap<>();
private final Map<String, List<Element>> nodeIdToChildren = new LinkedHashMap<>();
public void put(UUID nodeDescriptionId, Element nodeElement) {
this.nodeDescriptionIdToNodes.computeIfAbsent(nodeDescriptionId, id -> new ArrayList<>()).add(nodeElement);
}
public void put(Object object, Element nodeElement) {
this.nodeToObject.put(nodeElement, object);
this.objectToNodes.computeIfAbsent(object, obj -> new ArrayList<>()).add(nodeElement);
}
public void put(Element node, String parentNodeId) {
if (node.getProps() instanceof NodeElementProps nodeElementProps) {
this.nodeIdToNode.put(nodeElementProps.getId(), node);
this.nodeToParentElementId.put(node, parentNodeId);
var children = this.nodeIdToChildren.getOrDefault(parentNodeId, new ArrayList<>());
children.add(node);
this.nodeIdToChildren.put(parentNodeId, children);
}
}
public Map<UUID, List<Element>> getNodeDescriptionIdToNodes() {
return this.nodeDescriptionIdToNodes;
}
public Map<Element, Object> getNodeToObject() {
return this.nodeToObject;
}
public Map<Object, List<Element>> getObjectToNodes() {
return this.objectToNodes;
}
public List<Element> getElementsRepresenting(Object semanticObject) {
return this.objectToNodes.getOrDefault(semanticObject, Collections.emptyList());
}
public Optional<Element> getParent(String nodeId) {
// @formatter:off
return Optional.ofNullable(this.nodeIdToNode.get(nodeId))
.map(this.nodeToParentElementId::get)
.map(this.nodeIdToNode::get);
// @formatter:on
}
public List<Element> getAncestors(String nodeId) {
List<Element> ancestors = new ArrayList<>();
var parent = this.getParent(nodeId).orElse(null);
while (parent != null) {
ancestors.add(parent);
if (parent.getProps() instanceof NodeElementProps nodeElementProps) {
parent = this.getParent(nodeElementProps.getId()).orElse(null);
}
}
return ancestors;
}
public List<Element> getChildren(String nodeId) {
return Optional.ofNullable(this.nodeIdToChildren.get(nodeId)).orElse(List.of());
}
public List<Element> getDescendants(String nodeId) {
List<Element> descendants = new ArrayList<>();
var children = this.getChildren(nodeId);
children.forEach(child -> {
descendants.add(child);
if (child.getProps() instanceof NodeElementProps nodeElementProps) {
descendants.addAll(this.getDescendants(nodeElementProps.getId()));
}
});
return descendants;
}
}