Set<Set<RefClass>> ( RefPackage aPackage, Set<RefClass> classes) { final Set<RefClass> allClasses = new HashSet<>(classes); final Set<Set<RefClass>> out = new HashSet<>(); while (!allClasses.isEmpty()) { final RefClass seed = allClasses.iterator().next(); allClasses.remove(seed); final Set<RefClass> currentComponent = new HashSet<>(); currentComponent.add(seed); final List<RefClass> pendingClasses = new ArrayList<>(); pendingClasses.add(seed); while (!pendingClasses.isEmpty()) { final RefClass classToProcess = pendingClasses.remove(0); final Set<RefClass> relatedClasses = getRelatedClasses(aPackage, classToProcess); for (RefClass relatedClass : relatedClasses) { if (!currentComponent.contains(relatedClass) && !pendingClasses.contains(relatedClass)) { currentComponent.add(relatedClass); pendingClasses.add(relatedClass); allClasses.remove(relatedClass); } } } out.add(currentComponent); } return out; }