| 695 | } |
| 696 | |
| 697 | private MultiMap<E, E> generateNewMap( |
| 698 | E oldNode, |
| 699 | E newNode, |
| 700 | MultiMap<E, E> mm) { |
| 701 | // First, replace the key |
| 702 | Collection<E> targets = mm.get(oldNode); |
| 703 | if (targets != null) { |
| 704 | mm.removeKey(oldNode); |
| 705 | mm.put(newNode, targets); |
| 706 | } |
| 707 | |
| 708 | // We can't just do a remove and add in the map because of our |
| 709 | // guarantee of not changing orders. So we need to walk the lists and |
| 710 | // put the new node in the same slot as the old. |
| 711 | |
| 712 | // Walk all the other keys and replace any references to the oldNode |
| 713 | // in their targets. |
| 714 | MultiMap<E, E> newMap = new MultiMap<E, E>(mm.size()); |
| 715 | for (E key : mm.keySet()) { |
| 716 | Collection<E> c = mm.get(key); |
| 717 | ArrayList<E> al = new ArrayList<E>(c); |
| 718 | for (int i = 0; i < al.size(); i++) { |
| 719 | if (al.get(i) == oldNode) al.set(i, newNode); |
| 720 | } |
| 721 | newMap.put(key, al); |
| 722 | } |
| 723 | return newMap; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Remove a node in a way that connects the node's predecessor (if any) |