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