/** * Given a list of ModuleDependencies (of the same groupId and artifactId), * picks the {@link ModuleDependency} that satisfies the constraint and has the highest version. * * @param candidates * List that represents specific (non-range) versions. * @return The highest satisfying ModuleDependency or null if none can be found. */ public ModuleDependency findHighestFrom(Collection<ModuleDependency> candidates) { //Create a sorted map of the ModuleDependnecies sorted on version (descending order). SortedMap<ArtifactVersion, ModuleDependency> sorted = new TreeMap<ArtifactVersion, ModuleDependency>(new ReverseComparator()); for (ModuleDependency candidate : candidates) { sorted.put(candidate.parseVersion(), candidate); } //Now find the highest version that satisfies this dependency. for (ModuleDependency e : sorted.values()) { if (contains(e)) return e; } // non found return null; }
/** * Checks whether this ModuleDependency is satisfied by the dependency of the given ModuleDependency. * If the version string is a defined version, then it does a comparison. If the version string * is a version range if parses this and caters for this. * * @param other The dependency to check for. * @return true if contained false otherwise. */ public boolean contains(ModuleDependency other) { if (other == null || !getName().equals(other.getName())) return false; try { return isVersionRange() ? getVersionAsRange().containsVersion(other.parseVersion()) : parseVersion().compareTo(other.parseVersion()) == 0; } catch (InvalidVersionSpecificationException ivse) { return false; } }