1 package fr.jayasoft.ivy; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.LinkedHashMap ; 7 import java.util.LinkedList ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.Stack ; 11 12 import fr.jayasoft.ivy.circular.CircularDependencyException; 13 import fr.jayasoft.ivy.circular.CircularDependencyHelper; 14 import fr.jayasoft.ivy.util.Message; 15 import fr.jayasoft.ivy.version.VersionMatcher; 16 17 23 class ModuleDescriptorSorter { 24 public static List sortNodes(VersionMatcher matcher, Collection nodes) { 25 30 31 Map dependenciesMap = new LinkedHashMap (); 32 List nulls = new ArrayList (); 33 for (Iterator iter = nodes.iterator(); iter.hasNext();) { 34 IvyNode node = (IvyNode)iter.next(); 35 if (node.getDescriptor() == null) { 36 nulls.add(node); 37 } else { 38 List n = (List )dependenciesMap.get(node.getDescriptor()); 39 if (n == null) { 40 n = new ArrayList (); 41 dependenciesMap.put(node.getDescriptor(), n); 42 } 43 n.add(node); 44 } 45 } 46 List list = sortModuleDescriptors(matcher, dependenciesMap.keySet()); 47 List ret = new ArrayList ((int)(list.size()*1.3+nulls.size())); for (int i=0; i<list.size(); i++) { 49 ModuleDescriptor md = (ModuleDescriptor)list.get(i); 50 List n = (List )dependenciesMap.get(md); 51 ret.addAll(n); 52 } 53 ret.addAll(0, nulls); 54 return ret; 55 } 56 57 58 66 public static List sortModuleDescriptors(VersionMatcher matcher, Collection moduleDescriptors) throws CircularDependencyException { 67 return new ModuleDescriptorSorter(moduleDescriptors).sortModuleDescriptors(matcher); 68 } 69 70 71 private final Collection moduleDescriptors; 72 private final Iterator moduleDescriptorsIterator; 73 private final List sorted = new LinkedList (); 74 75 public ModuleDescriptorSorter(Collection moduleDescriptors) { 76 this.moduleDescriptors=moduleDescriptors; 77 moduleDescriptorsIterator = new LinkedList (moduleDescriptors).iterator(); 78 } 79 80 85 public List sortModuleDescriptors(VersionMatcher matcher) throws CircularDependencyException { 86 while (moduleDescriptorsIterator.hasNext()) { 87 sortModuleDescriptorsHelp(matcher, (ModuleDescriptor)moduleDescriptorsIterator.next(), new Stack ()); 88 } 89 return sorted; 90 } 91 92 100 private void sortModuleDescriptorsHelp(VersionMatcher matcher, ModuleDescriptor current, Stack callStack) throws CircularDependencyException { 101 if (sorted.contains(current)) { 103 return; 104 } 105 if (callStack.contains(current)) { 106 callStack.add(current); 107 Message.verbose("circular dependency ignored during sort: "+CircularDependencyHelper.formatMessage((ModuleDescriptor[]) callStack.toArray(new ModuleDescriptor[callStack.size()]))); 108 return; 109 } 110 DependencyDescriptor [] descriptors = current.getDependencies(); 111 ModuleDescriptor moduleDescriptorDependency = null; 112 for (int i = 0; descriptors!=null && i < descriptors.length; i++) { 113 moduleDescriptorDependency = getModuleDescriptorDependency(matcher, descriptors[i]); 114 115 if (moduleDescriptorDependency != null) { 116 callStack.push(current); 117 sortModuleDescriptorsHelp(matcher, moduleDescriptorDependency, callStack); 118 callStack.pop(); 119 } 120 } 121 sorted.add(current); 122 } 123 124 129 private ModuleDescriptor getModuleDescriptorDependency(VersionMatcher matcher, DependencyDescriptor descriptor) { 130 Iterator i = moduleDescriptors.iterator(); 131 ModuleDescriptor md = null; 132 while (i.hasNext()) { 133 md = (ModuleDescriptor) i.next(); 134 if (descriptor.getDependencyId().equals(md.getModuleRevisionId().getModuleId())) { 135 if (md.getResolvedModuleRevisionId().getRevision() == null) { 136 return md; 137 } else if (matcher.accept(descriptor.getDependencyRevisionId(), md)) { 138 return md; 139 } 140 } 141 } 142 return null; 143 } 144 } | Popular Tags |