/** * Checks if any of the dependencies matches the given id * @param deps the list of dependencies * @param id the id * @return {@code true} if matches */ public static boolean matches(@Nonnull Dependency[] deps, @Nonnull PackageId id) { for (Dependency dep: deps) { if (dep.matches(id)) { return true; } } return false; }
private static void resolve(Dependency[] deps, Map<PackageId, Dependency[]> list, Map<PackageId, Boolean> result) throws CyclicDependencyException { // find the dep in the list for (Dependency dep: deps) { for (Map.Entry<PackageId, Dependency[]> entry: list.entrySet()) { PackageId id = entry.getKey(); if (dep.matches(id)) { Boolean res = result.get(id); if (res != null && !res) { log.error("Package dependencies cause cycle."); throw new CyclicDependencyException(); } else if (res == null) { result.put(id, res = false); } resolve(entry.getValue(), list, result); // shove at the end of the list if not resolved if (!res) { result.remove(id); result.put(id, true); } } } } }
/** * {@inheritDoc} */ @Nonnull @Override public PackageId[] usage(PackageId id) throws IOException { TreeSet<PackageId> usages = new TreeSet<PackageId>(); for (PackageId pid : packages()) { try (RegisteredPackage pkg = open(pid)) { if (pkg == null || !pkg.isInstalled()) { continue; } // noinspection resource for (Dependency dep : pkg.getPackage().getDependencies()) { if (dep.matches(id)) { usages.add(pid); break; } } } } return usages.toArray(new PackageId[usages.size()]); }
if (dependency.matches(id)) { if (bestId == null || id.getVersion().compareTo(bestId.getVersion()) > 0) { bestId = id;
/** * {@inheritDoc} */ @Override public PackageId resolve(Dependency dependency, boolean onlyInstalled) throws IOException { PackageId bestId = null; for (PackageId id : packages()) { if (!onlyInstalled || isInstalled(id)) { if (dependency.matches(id)) { if (bestId == null || id.getVersion().compareTo(bestId.getVersion()) > 0) { bestId = id; } } } } return bestId; }