insertNodes.addLast(unsortedNode);
// on update, mark the updated node as unsorted and save it so it can be moved
} else if(changeType == ListEvent.UPDATE) {
IndexedTreeNode<IndexedTreeNode> unsortedNode = unsorted.getNode(unsortedIndex);
IndexedTreeNode sortedNode = unsortedNode.getValue();
sortedNode.setSorted(false);
updateNodes.add(sortedNode);
// on delete, delete the index and sorted node
} else if(changeType == ListEvent.DELETE) {
IndexedTreeNode<IndexedTreeNode> unsortedNode = unsorted.getNode(unsortedIndex);
unsortedNode.removeFromTree(unsorted);
int deleteSortedIndex = deleteByUnsortedNode(unsortedNode);
updates.addDelete(deleteSortedIndex);
}
}
// fire update events
for(Iterator<IndexedTreeNode<IndexedTreeNode>> i = updateNodes.iterator(); i.hasNext(); ) {
IndexedTreeNode<IndexedTreeNode> sortedNode = i.next();
int originalIndex = sortedNode.getIndex();
// the element is still in sorted order, forward the update event
if(isNodeInSortedOrder(sortedNode)) {
sortedNode.setSorted(true);
updates.addUpdate(originalIndex);
// sort order is not enforced so we lose perfect sorting order
// but we don't need to move elements around
} else if(mode == SortedListWithIndexedTree.AVOID_MOVING_ELEMENTS) {
updates.addUpdate(originalIndex);
// sort order is enforced so move the element to its new location
} else {
sortedNode.removeFromTree(sorted);
updates.addDelete(originalIndex);
int insertedIndex = insertByUnsortedNode(sortedNode.getValue());
updates.addInsert(insertedIndex);
}
}
// fire insert events
while(!insertNodes.isEmpty()) {
IndexedTreeNode insertNode = insertNodes.removeFirst();
int insertedIndex = insertByUnsortedNode(insertNode);
updates.addInsert(insertedIndex);
}
// commit the changes and notify listeners