net.sf.gluebooster.java.booster.basic.container.ContainerBoostUtilsBasic Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.container;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.sf.gluebooster.java.booster.essentials.meta.BoostedComparator;
import net.sf.gluebooster.java.booster.essentials.utils.ContainerBoostUtils;
/**
* Additional container boost utils.
*
* @author cbauer
*
*/
public class ContainerBoostUtilsBasic extends ContainerBoostUtils {
/**
* Sorts a list using a comparator.
*
* @param list
* the list to be modified
* @param comparator
* there may be only a partial ordering
*/
public static void sortPartialOrderedList(List list, Comparator comparator) {
try {
Collections.sort(list, comparator);
} catch (Exception orderingProblemException) {
if (comparator instanceof BoostedComparator) {
ArrayList result = new ArrayList(list.size());
ArrayList changingList = new ArrayList(list);
// try to get a partial ordering.
while (!changingList.isEmpty()) {
Element minimum = getMinimum(changingList, comparator);
result.add(minimum);
changingList.remove(minimum);
}
// check the ordering (easy check)
for (int i = result.size() - 1; i > 0; i--) {
Element element1 = result.get(i - 1);
Element element2 = result.get(i);
Integer comparison = ComparatorBoostUtils.compare(element1, element2, comparator);
if (comparison != null && comparison.intValue() > 0) {
ComparatorBoostUtils.compare(element1, element2, comparator);
throw new IllegalStateException("could not order list because " + element1 + " should not be greater than " + element2);
}
}
list.clear();
list.addAll(result);
} else {
throw orderingProblemException;
}
}
}
}