public static void sort(List modules) {
final HashMap moduleMap = new HashMap();
final ArrayList errorModules = new ArrayList();
final ArrayList weightModules = new ArrayList();
for (int i = 0; i < modules.size(); i++)
{
final PackageState state = (PackageState) modules.get(i);
if (state.getState() == PackageState.STATE_ERROR)
{
errorModules.add (state);
}
else
{
final SortModule mod = new SortModule(state);
weightModules.add (mod);
moduleMap.put(state.getModule().getModuleClass(), mod);
}
}
final SortModule[] weigths = (SortModule[])
weightModules.toArray(new SortModule[weightModules.size()]);
for (int i = 0; i < weigths.length; i++)
{
final SortModule sortMod = weigths[i];
sortMod.setDependSubsystems
(collectSubsystemModules(sortMod.getState().getModule(),
moduleMap));
}
// repeat the computation until all modules have a matching
// position. This is not the best algorithm, but it works
// and is relativly simple. It will need some optimizations
// in the future, but as this is only executed once, we don't
// have to care much about it.
boolean doneWork = true;
while (doneWork)
{
doneWork = false;
for (int i = 0; i < weigths.length; i++)
{
final SortModule mod = weigths[i];
final int position = searchModulePosition(mod, moduleMap);
if (position != mod.getPosition())
{
mod.setPosition(position);
doneWork = true;
}
}
}
Arrays.sort(weigths);
modules.clear();
for (int i = 0; i < weigths.length; i++)
{
modules.add (weigths[i].getState());
}
for (int i = 0; i < errorModules.size(); i++)
{
modules.add (errorModules.get(i));
}
}
Sorts the given list of package states.
ToDo: The algorithm used is simple and not optimized,
we could write something better in a near future. |