List<Resource> iterable = new ArrayList<Resource>(list);
Iterator<Resource> itor = iterable.iterator();
while (itor.hasNext())
{
Resource r = itor.next();
FragmentDescriptor f = _metaData.getFragment(r);
if (f == null)
{
//no fragment for this resource so cannot have any ordering directives
continue;
}
//Handle any explicit <before> relationships for the fragment we're considering
List<String> befores = f.getBefores();
if (befores != null && !befores.isEmpty())
{
for (String b: befores)
{
//Fragment we're considering must be before b
//Check that we are already before it, if not, move us so that we are.
//If the name does not exist in our list, then get it out of the no-other list
if (!isBefore(list, f.getName(), b))
{
//b is not already before name, move it so that it is
int idx1 = getIndexOf(list, f.getName());
int idx2 = getIndexOf(list, b);
//if b is not in the same list
if (idx2 < 0)
{
changes = true;
// must be in the noOthers list or it would have been an error
Resource bResource = _metaData.getJarForFragment(b);
if (bResource != null)
{
//If its in the no-others list, insert into this list so that we are before it
if (_noOthers.remove(bResource))
{
insert(list, idx1+1, b);
}
}
}
else
{
//b is in the same list but b is before name, so swap it around
list.remove(idx1);
insert(list, idx2, f.getName());
changes = true;
}
}
}
}
//Handle any explicit <after> relationships
List<String> afters = f.getAfters();
if (afters != null && !afters.isEmpty())
{
for (String a: afters)
{
//Check that fragment we're considering is after a, moving it if possible if its not
if (!isAfter(list, f.getName(), a))
{
//name is not after a, move it
int idx1 = getIndexOf(list, f.getName());
int idx2 = getIndexOf(list, a);
//if a is not in the same list as name
if (idx2 < 0)
{
changes = true;
//take it out of the noOthers list and put it in the right place in this list
Resource aResource = _metaData.getJarForFragment(a);
if (aResource != null)
{
if (_noOthers.remove(aResource))
{
insert(list,idx1, aResource);