Skip to content

Commit 2c10482

Browse files
committed
fix(model-sync-lib): fixed index issues
1 parent 689e048 commit 2c10482

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,16 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
131131
return toBeAdded.map { nodeToBeAdded ->
132132
val childrenInRole = node.allChildren.filter { it.roleInParent == nodeToBeAdded.role }
133133
val existingIds = childrenInRole.map { it.originalId() }
134-
val baseIndex = nodeToBeAdded.getIndexWithinRole(nodeData, childrenInRole.size)
134+
val baseIndex = nodeToBeAdded.getIndexWithinRole(nodeData)
135135
var offset = 0
136136
offset += childrenInRole.slice(0..minOf(baseIndex, childrenInRole.lastIndex)).count {
137137
!originalIdToSpec.containsKey(it.originalId()) // node will be deleted
138138
}
139139
offset -= specifiedChildren.filter { it.role == nodeToBeAdded.role }.slice(0 until baseIndex).count {
140140
!existingIds.contains(it.originalId()) // node will be moved here
141141
}
142-
node.addNewChildWithStats(nodeToBeAdded, baseIndex + offset)
142+
val index = (baseIndex + offset).coerceIn(0..childrenInRole.size)
143+
node.addNewChildWithStats(nodeToBeAdded, index)
143144
}
144145
}
145146

@@ -168,17 +169,16 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
168169
for (child in toBeSortedSpec) {
169170

170171
val childrenInRole = existingChildren.filter { it.roleInParent == child.role }
171-
val baseIndex = child.getIndexWithinRole(nodeData, childrenInRole.lastIndex)
172+
val baseIndex = child.getIndexWithinRole(nodeData).coerceAtMost(childrenInRole.lastIndex)
172173
var offset = 0
173174
offset += childrenInRole.slice(0..baseIndex).count {
174175
!originalIdToSpec.containsKey(it.originalId()) // node will be deleted
175176
}
176177
offset -= childrenInRole.slice(0..baseIndex).count {
177178
!existingIds.contains(it.originalId()) // node will be moved here
178179
}
179-
val index = if (childrenInRole.isEmpty()) 0 else baseIndex + offset
180-
val upperBound = if (existingChildren.isEmpty()) 0 else existingChildren.lastIndex
181-
targetIndices[child.originalId()] = minOf(index, upperBound)
180+
val index = (baseIndex + offset).coerceIn(0..childrenInRole.size)
181+
targetIndices[child.originalId()] = index
182182
}
183183

184184
existingChildren.forEach { child ->
@@ -219,11 +219,11 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
219219
toBeMovedHere.forEach {nodeToBeMoved ->
220220
val spec = originalIdToSpec[nodeToBeMoved.originalId()]!!
221221
val childrenInRole = existingChildren.filter { it.roleInParent == spec.role }
222-
val baseTargetIndex = spec.getIndexWithinRole(nodeData, childrenInRole.lastIndex)
223-
val offset = existingChildren.slice(0 until baseTargetIndex).count {
222+
val baseTargetIndex = spec.getIndexWithinRole(nodeData).coerceAtMost(childrenInRole.size)
223+
val offset = existingChildren.slice(0 until baseTargetIndex).count {
224224
!originalIdToSpec.containsKey(it.originalId()) // node will be deleted
225225
}
226-
val targetIndex = baseTargetIndex + offset
226+
val targetIndex = (baseTargetIndex + offset).coerceIn(0..childrenInRole.size)
227227

228228
node.moveChildWithStats(spec.role, targetIndex, nodeToBeMoved)
229229
}
@@ -243,9 +243,9 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
243243
}
244244
}
245245

246-
private fun NodeData.getIndexWithinRole(parent: NodeData, maxIndex: Int) : Int {
247-
return minOf(parent.children.filter { it.role == this.role }.indexOf(this), maxIndex)
248-
}
246+
private fun NodeData.getIndexWithinRole(parent: NodeData) : Int =
247+
parent.children.filter { it.role == this.role }.indexOf(this)
248+
249249

250250
private fun INode.addNewChildWithStats(spec: NodeData, index: Int) : INode {
251251
val concept = spec.concept?.let { s -> ConceptReference(s) }

0 commit comments

Comments
 (0)