Source:
<code>public void setSourcesList(List<BranchSource> sources) throws IOException { if (this.sources.isEmpty() || sources.isEmpty()) { this.sources.replaceBy(sources); return; } Set<String> oldIds = sourceIds(this.sources); Set<String> newIds = sourceIds(sources); if (oldIds.containsAll(newIds) || newIds.containsAll(oldIds)) { this.sources.replaceBy(sources); return; } Map<String,String> changedIds = new HashMap<>(); Set<String> additions = new HashSet<>(newIds); additions.removeAll(oldIds); Set<String> removals = new HashSet<>(oldIds); removals.removeAll(newIds); sources = new ArrayList<>(sources); for (ListIterator<BranchSource> i = sources.listIterator(); i.hasNext(); ) { BranchSource addition = i.next(); if (!additions.contains(addition.getSource().getId())) { continue; } for (BranchSource removal: this.sources) { if (!removals.contains(removal.getSource().getId())) { continue; } if (!equalButForId(removal.getSource(), addition.getSource())) { continue; } changedIds.put(removal.getSource().getId(), addition.getSource().getId()); removals.remove(removal.getSource().getId()); <START> break; <END> } } this.sources.replaceBy(sources); BranchProjectFactory<P,R> factory = getProjectFactory(); for (P item: getItems()) { if (!factory.isProject(item)) { continue; } Branch oldBranch = factory.getBranch(item); if (changedIds.containsKey(oldBranch.getSourceId())) { Branch newBranch = new Branch(changedIds.get(oldBranch.getSourceId()), oldBranch.getHead(), oldBranch.getScm(), oldBranch.getProperties()); newBranch.setActions(oldBranch.getActions()); factory.setBranch(item, newBranch); } } }</code><technical_language>this one, interested in removal matching</technical_language>

Target:
public void setSourcesList(List<BranchSource> sources) throws IOException { if (this.sources.isEmpty() || sources.isEmpty()) { this.sources.replaceBy(sources); return; } Set<String> oldIds = sourceIds(this.sources); Set<String> newIds = sourceIds(sources); if (oldIds.containsAll(newIds) || newIds.containsAll(oldIds)) { this.sources.replaceBy(sources); return; } Map<String,String> changedIds = new HashMap<>(); Set<String> additions = new HashSet<>(newIds); additions.removeAll(oldIds); Set<String> removals = new HashSet<>(oldIds); removals.removeAll(newIds); for (BranchSource addition : sources) { String additionId = addition.getSource().getId(); if (!additions.contains(additionId)) { continue; } for (BranchSource removal : this.sources) { String removalId = removal.getSource().getId(); if (!removals.contains(removalId)) { continue; } if (!equalButForId(removal.getSource(), addition.getSource())) { continue; } changedIds.put(removalId, additionId); removals.remove(removalId); additions.remove(additionId); break; } } this.sources.replaceBy(sources); BranchProjectFactory<P,R> factory = getProjectFactory(); for (P item: getItems()) { if (!factory.isProject(item)) { continue; } Branch oldBranch = factory.getBranch(item); if (changedIds.containsKey(oldBranch.getSourceId())) { Branch newBranch = new Branch(changedIds.get(oldBranch.getSourceId()), oldBranch.getHead(), oldBranch.getScm(), oldBranch.getProperties()); newBranch.setActions(oldBranch.getActions()); factory.setBranch(item, newBranch); } } }