Skip to content

Commit 00d8536

Browse files
committed
refactor(model-sync-lib): turned index maps into fields
1 parent fa79e93 commit 00d8536

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

model-sync-lib/src/main/kotlin/org/modelix/model/sync/ModelImporter.kt

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import java.io.File
77

88
class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
99

10+
private val originalIdToExisting: MutableMap<String, INode> = mutableMapOf()
11+
private val originalIdToSpec: MutableMap<String, NodeData> = mutableMapOf()
12+
private val originalIdToRef: MutableMap<String, INodeReference> = mutableMapOf()
13+
1014
fun import(jsonFile: File) {
1115
require(jsonFile.exists())
1216
require(jsonFile.extension == "json")
@@ -17,54 +21,49 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
1721

1822
fun import(data: ModelData) {
1923
stats?.reset()
24+
originalIdToExisting.clear()
25+
originalIdToSpec.clear()
26+
originalIdToRef.clear()
2027

2128
syncProperties(root, data.root) // root original id is required for following operations
2229
val allExistingNodes = root.getDescendants(true).toList()
23-
val originalIdToSpec: MutableMap<String, NodeData> = buildSpecIndex(data.root)
30+
buildSpecIndex(data.root)
2431

25-
syncAllProperties(allExistingNodes, originalIdToSpec)
26-
val originalIdToExisting = buildExistingIndex(allExistingNodes)
32+
syncAllProperties(allExistingNodes)
33+
buildExistingIndex(allExistingNodes)
2734

28-
sortAllExistingChildren(allExistingNodes, originalIdToExisting, originalIdToSpec)
29-
val addedNodes = addAllMissingChildren(root, originalIdToExisting, originalIdToSpec)
30-
syncAllProperties(addedNodes, originalIdToSpec)
35+
sortAllExistingChildren(allExistingNodes)
36+
val addedNodes = addAllMissingChildren(root)
37+
syncAllProperties(addedNodes)
3138

3239
val allNodes = allExistingNodes + addedNodes
3340

34-
handleAllMovesAcrossParents(allNodes, originalIdToExisting, originalIdToSpec)
41+
handleAllMovesAcrossParents(allNodes)
3542

36-
val originalIdToRef: MutableMap<String, INodeReference> = buildRefIndex(allNodes)
37-
syncAllReferences(allNodes, originalIdToSpec, originalIdToRef)
43+
buildRefIndex(allNodes)
44+
syncAllReferences(allNodes)
3845

39-
deleteAllExtraChildren(root, originalIdToSpec)
46+
deleteAllExtraChildren(root)
4047
}
4148

42-
private fun buildExistingIndex(allNodes: List<INode>): MutableMap<String, INode> {
43-
val originalIdToExisting: MutableMap<String, INode> = mutableMapOf()
49+
private fun buildExistingIndex(allNodes: List<INode>) {
4450
allNodes.forEach {node ->
4551
node.originalId()?.let { originalIdToExisting[it] = node }
4652
}
47-
return originalIdToExisting
4853
}
4954

50-
private fun buildRefIndex(allNodes: List<INode>): MutableMap<String, INodeReference> {
51-
val originalIdToRef: MutableMap<String, INodeReference> = mutableMapOf()
55+
private fun buildRefIndex(allNodes: List<INode>) {
5256
allNodes.forEach {node ->
5357
node.originalId()?.let { originalIdToRef[it] = node.reference }
5458
}
55-
return originalIdToRef
5659
}
5760

58-
private fun buildSpecIndex(
59-
nodeData: NodeData,
60-
originalIdToSpec: MutableMap<String, NodeData> = mutableMapOf()
61-
): MutableMap<String, NodeData> {
61+
private fun buildSpecIndex(nodeData: NodeData) {
6262
nodeData.originalId()?.let { originalIdToSpec[it] = nodeData }
63-
nodeData.children.forEach { buildSpecIndex(it, originalIdToSpec) }
64-
return originalIdToSpec
63+
nodeData.children.forEach { buildSpecIndex(it) }
6564
}
6665

67-
private fun syncAllProperties(allNodes: List<INode>, originalIdToSpec: MutableMap<String, NodeData>) {
66+
private fun syncAllProperties(allNodes: List<INode>) {
6867
allNodes.forEach {node ->
6968
originalIdToSpec[node.originalId()]?.let { spec -> syncProperties(node, spec) }
7069
}
@@ -87,7 +86,7 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
8786
toBeRemoved.forEach { node.setPropertyValueWithStats(it, null) }
8887
}
8988

90-
private fun syncAllReferences(allNodes: List<INode>, originalIdToSpec: MutableMap<String, NodeData>, originalIdToRef: Map<String, INodeReference>) {
89+
private fun syncAllReferences(allNodes: List<INode>) {
9190
allNodes.forEach {node ->
9291
originalIdToSpec[node.originalId()]?.let { spec -> syncReferences(node, spec, originalIdToRef) }
9392
}
@@ -103,29 +102,20 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
103102
toBeRemoved.forEach { node.setReferenceTargetWithStats(it, null) }
104103
}
105104

106-
private fun addAllMissingChildren(
107-
node: INode,
108-
originalIdToExisting: MutableMap<String, INode>,
109-
originalIdToSpec: MutableMap<String, NodeData>
110-
): MutableList<INode> {
105+
private fun addAllMissingChildren(node: INode): MutableList<INode> {
111106
val addedNodes = mutableListOf<INode>()
112107
originalIdToSpec[node.originalId()]?.let {
113108
addedNodes.addAll(
114-
addMissingChildren(node, it, originalIdToExisting, originalIdToSpec)
109+
addMissingChildren(node, it)
115110
)
116111
}
117112
node.allChildren.forEach {
118-
addedNodes.addAll(addAllMissingChildren(it, originalIdToExisting, originalIdToSpec))
113+
addedNodes.addAll(addAllMissingChildren(it))
119114
}
120115
return addedNodes
121116
}
122117

123-
private fun addMissingChildren(
124-
node: INode,
125-
nodeData: NodeData,
126-
originalIdToExisting: MutableMap<String, INode>,
127-
originalIdToSpec: MutableMap<String, NodeData>
128-
): List<INode> {
118+
private fun addMissingChildren(node: INode, nodeData: NodeData): List<INode> {
129119
val specifiedChildren = nodeData.children.toList()
130120
val toBeAdded = specifiedChildren.filter { !originalIdToExisting.contains(it.originalId()) }
131121

@@ -146,21 +136,14 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
146136
}
147137

148138
private fun sortAllExistingChildren(
149-
allNodes: List<INode>,
150-
originalIdToExisting: MutableMap<String, INode>,
151-
originalIdToSpec: MutableMap<String, NodeData>
139+
allNodes: Iterable<INode>
152140
) {
153141
allNodes.forEach { node ->
154-
originalIdToSpec[node.originalId()]?.let { sortExistingChildren(node, it, originalIdToExisting, originalIdToSpec) }
142+
originalIdToSpec[node.originalId()]?.let { sortExistingChildren(node, it) }
155143
}
156144
}
157145

158-
private fun sortExistingChildren(
159-
node: INode,
160-
nodeData: NodeData,
161-
originalIdToExisting: MutableMap<String, INode>,
162-
originalIdToSpec: MutableMap<String, NodeData>
163-
) {
146+
private fun sortExistingChildren(node: INode, nodeData: NodeData) {
164147
val existingChildren = node.allChildren.toList()
165148
val existingIds = existingChildren.map { it.originalId() }
166149
val specifiedChildren = nodeData.children
@@ -194,24 +177,16 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
194177
}
195178
}
196179

197-
private fun handleAllMovesAcrossParents(
198-
allNodes: List<INode>,
199-
originalIdToExisting: MutableMap<String, INode>,
200-
originalIdToSpec: MutableMap<String, NodeData>
201-
) {
202-
val moves = collectMovesAcrossParents(allNodes, originalIdToExisting, originalIdToSpec)
180+
private fun handleAllMovesAcrossParents(allNodes: List<INode>) {
181+
val moves = collectMovesAcrossParents(allNodes)
203182
while (moves.isNotEmpty()) {
204183
val nextMove = moves.first { !it.nodeToBeMoved.getDescendants(false).contains(it.targetParent) }
205-
performMoveAcrossParents(nextMove.targetParent, nextMove.nodeToBeMoved, originalIdToSpec)
184+
performMoveAcrossParents(nextMove.targetParent, nextMove.nodeToBeMoved)
206185
moves.remove(nextMove)
207186
}
208187
}
209188

210-
private fun collectMovesAcrossParents(
211-
allNodes: List<INode>,
212-
originalIdToExisting: MutableMap<String, INode>,
213-
originalIdToSpec: MutableMap<String, NodeData>
214-
): MutableList<MoveAcrossParents> {
189+
private fun collectMovesAcrossParents(allNodes: List<INode>): MutableList<MoveAcrossParents> {
215190
val movesAcrossParents = mutableListOf<MoveAcrossParents>()
216191
allNodes.forEach {node ->
217192
val nodeData = originalIdToSpec[node.originalId()] ?: return@forEach
@@ -231,11 +206,7 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
231206

232207
private data class MoveAcrossParents(val targetParent: INode, val nodeToBeMoved: INode)
233208

234-
private fun performMoveAcrossParents(
235-
node: INode,
236-
toBeMovedHere: INode,
237-
originalIdToSpec: MutableMap<String, NodeData>
238-
) {
209+
private fun performMoveAcrossParents(node: INode, toBeMovedHere: INode) {
239210
val nodeData = originalIdToSpec[node.originalId()] ?: return
240211
val existingChildren = node.allChildren.toList()
241212
val spec = originalIdToSpec[toBeMovedHere.originalId()]!!
@@ -250,7 +221,7 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
250221

251222
}
252223

253-
private fun deleteAllExtraChildren(root: INode, originalIdToSpec: MutableMap<String, NodeData>) {
224+
private fun deleteAllExtraChildren(root: INode) {
254225
val toBeRemoved = mutableListOf<INode>()
255226
root.allChildren.forEach {
256227
if (!originalIdToSpec.containsKey(it.originalId())) {
@@ -261,7 +232,7 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
261232
it.parent?.removeChildWithStats(it)
262233
}
263234
root.allChildren.forEach {
264-
deleteAllExtraChildren(it, originalIdToSpec)
235+
deleteAllExtraChildren(it)
265236
}
266237
}
267238

0 commit comments

Comments
 (0)