if (ref == null) {
continue;
}
NodeKey childKey = ref.getKey();
// Are nodes inserted before this node?
Insertions insertions = insertionsByBeforeKey.remove(childKey);
if (insertions != null) {
for (ChildReference inserted : insertions.inserted()) {
newChildren.add(inserted);
}
}
if (removals.remove(childKey)) {
// The node is removed ...
} else {
// The node remains ...
Name newName = newNames.get(childKey);
if (newName != null) {
// But has been renamed ...
ref = ref.with(newName, 1);
}
newChildren.add(ref);
}
}
}
if (!insertionsByBeforeKey.isEmpty()) {
// there are transient insertions (due to reordering of transient nodes) that have to be inserted in a correct order
// if any reorderings involved existing children, they would have already been removed by the previous block
// note that these insertions have to be added as child because *they do not appear* in the appended list
LinkedList<ChildReference> toBeInsertedInOrder = new LinkedList<ChildReference>();
for (Insertions insertion : insertionsByBeforeKey.values()) {
// process the remaining insertions-before, which indicate transient & reordered children (reordering removes
// children
// from the appended list
for (ChildReference activeReference : insertion.inserted()) {
if (toBeInsertedInOrder.contains(activeReference)) {
// the current reference is already in the list
continue;
}
Insertions insertionsBeforeActive = insertionsByBeforeKey.get(activeReference.getKey());
if (insertionsBeforeActive == null) {
toBeInsertedInOrder.addFirst(activeReference);
continue;
}
for (ChildReference referenceBeforeActive : insertionsBeforeActive.inserted()) {
if (!toBeInsertedInOrder.contains(referenceBeforeActive)) {
toBeInsertedInOrder.add(referenceBeforeActive);
}
}
toBeInsertedInOrder.add(activeReference);