public static Collection<SortUnit> consolidateSortUnits(final Collection<SortUnit> in) {
Map<MetaClass, SortUnit> sortUnitMap = new HashMap<MetaClass, SortUnit>(in.size() * 2);
List<SortUnit> consolidatedList = new ArrayList<SortUnit>(in.size());
SortUnit masterSU;
for (SortUnit su : in) {
masterSU = sortUnitMap.get(su.getType());
if (masterSU == su) continue;
if (masterSU != null) {
for (Object item : su.getItems()) {
masterSU.addItem(item);
}
}
else {
sortUnitMap.put(su.getType(), su);
}
}
// second pass to re-reference dependencies
for (SortUnit su : in) {
Set<SortUnit> newDependencySet = new HashSet<SortUnit>();
for (SortUnit dep : su.getDependencies()) {
masterSU = sortUnitMap.get(dep.getType());
if (masterSU == null) {
masterSU = dep;
}
newDependencySet.add(masterSU);
}
consolidatedList.add(new SortUnit(su.getType(), su.getItems(), newDependencySet, su.isHard()));
}
return consolidatedList;
}