void ( RefPackage refPackage, Set<? super RefPackage> newDependents) { final LinkedList<RefPackage> pendingPackages = new LinkedList<>(); pendingPackages.addLast(refPackage); final Set<RefPackage> processedPackages = new HashSet<>(); while (!pendingPackages.isEmpty()) { final RefPackage packageToProcess = pendingPackages.removeFirst(); newDependents.add(packageToProcess); processedPackages.add(packageToProcess); final Set<RefPackage> dependents = calculateDependentsForPackage(packageToProcess); for (RefPackage dependent : dependents) { if (!pendingPackages.contains(dependent) && !processedPackages.contains(dependent)) { pendingPackages.addLast(dependent); } } } newDependents.remove(refPackage); }