@@ -131,15 +131,16 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
131
131
return toBeAdded.map { nodeToBeAdded ->
132
132
val childrenInRole = node.allChildren.filter { it.roleInParent == nodeToBeAdded.role }
133
133
val existingIds = childrenInRole.map { it.originalId() }
134
- val baseIndex = nodeToBeAdded.getIndexWithinRole(nodeData, childrenInRole.size )
134
+ val baseIndex = nodeToBeAdded.getIndexWithinRole(nodeData)
135
135
var offset = 0
136
136
offset + = childrenInRole.slice(0 .. minOf(baseIndex, childrenInRole.lastIndex)).count {
137
137
! originalIdToSpec.containsKey(it.originalId()) // node will be deleted
138
138
}
139
139
offset - = specifiedChildren.filter { it.role == nodeToBeAdded.role }.slice(0 until baseIndex).count {
140
140
! existingIds.contains(it.originalId()) // node will be moved here
141
141
}
142
- node.addNewChildWithStats(nodeToBeAdded, baseIndex + offset)
142
+ val index = (baseIndex + offset).coerceIn(0 .. childrenInRole.size)
143
+ node.addNewChildWithStats(nodeToBeAdded, index)
143
144
}
144
145
}
145
146
@@ -168,17 +169,16 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
168
169
for (child in toBeSortedSpec) {
169
170
170
171
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)
172
173
var offset = 0
173
174
offset + = childrenInRole.slice(0 .. baseIndex).count {
174
175
! originalIdToSpec.containsKey(it.originalId()) // node will be deleted
175
176
}
176
177
offset - = childrenInRole.slice(0 .. baseIndex).count {
177
178
! existingIds.contains(it.originalId()) // node will be moved here
178
179
}
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
182
182
}
183
183
184
184
existingChildren.forEach { child ->
@@ -219,11 +219,11 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
219
219
toBeMovedHere.forEach {nodeToBeMoved ->
220
220
val spec = originalIdToSpec[nodeToBeMoved.originalId()]!!
221
221
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 {
224
224
! originalIdToSpec.containsKey(it.originalId()) // node will be deleted
225
225
}
226
- val targetIndex = baseTargetIndex + offset
226
+ val targetIndex = ( baseTargetIndex + offset).coerceIn( 0 .. childrenInRole.size)
227
227
228
228
node.moveChildWithStats(spec.role, targetIndex, nodeToBeMoved)
229
229
}
@@ -243,9 +243,9 @@ class ModelImporter(private val root: INode, val stats: ImportStats? = null) {
243
243
}
244
244
}
245
245
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
+
249
249
250
250
private fun INode.addNewChildWithStats (spec : NodeData , index : Int ) : INode {
251
251
val concept = spec.concept?.let { s -> ConceptReference (s) }
0 commit comments