@Test public void detect_shouldNotDetectACycle4() { List<Component<?>> components = Arrays.asList( Component.builder(TestInterface1.class) .add(Dependency.required(TestInterface3.class)) .add(Dependency.required(TestInterface4.class)) .factory(nullFactory()) .build(), Component.builder(TestInterface2.class) .add(Dependency.required(TestInterface5.class)) .add(Dependency.required(TestInterface6.class)) .factory(nullFactory()) .build(), Component.builder(TestInterface3.class).factory(nullFactory()).build(), Component.builder(TestInterface4.class).factory(nullFactory()).build(), Component.builder(TestInterface5.class).factory(nullFactory()).build(), Component.builder(TestInterface6.class).factory(nullFactory()).build(), Component.builder(TestInterface7.class).factory(nullFactory()).build()); twice(() -> detect(components)); }
static void detect(List<Component<?>> components) { Set<ComponentNode> graph = toGraph(components); Set<ComponentNode> roots = getRoots(graph); int numVisited = 0; while (!roots.isEmpty()) { ComponentNode node = roots.iterator().next(); roots.remove(node); numVisited++; for (ComponentNode dependent : node.getDependencies()) { dependent.removeDependent(node); if (dependent.isRoot()) { roots.add(dependent); } } } if (numVisited == components.size()) { return; } List<Component<?>> componentsInCycle = new ArrayList<>(); for (ComponentNode node : graph) { if (!node.isRoot() && !node.isLeaf()) { componentsInCycle.add(node.getComponent()); } } throw new DependencyCycleException(componentsInCycle); }
CycleDetector { static void detect(List<Component<?>> components) { Set<ComponentNode> graph = toGraph(components); Set<ComponentNode> roots = getRoots(graph); int numVisited = 0; while (!roots.isEmpty()) { ComponentNode node = roots.iterator().next(); roots.remove(node); numVisited++; for (ComponentNode dependent : node.getDependencies()) { dependent.removeDependent(node); if (dependent.isRoot()) { roots.add(dependent); } } } if (numVisited == components.size()) { return; } List<Component<?>> componentsInCycle = new ArrayList<>(); for (ComponentNode node : graph) { if (!node.isRoot() && !node.isLeaf()) { componentsInCycle.add(node.getComponent()); } } throw new DependencyCycleException(componentsInCycle); } }
CycleDetector { static void detect(List<Component<?>> components) { Set<ComponentNode> graph = toGraph(components); Set<ComponentNode> roots = getRoots(graph); int numVisited = 0; while (!roots.isEmpty()) { ComponentNode node = roots.iterator().next(); roots.remove(node); numVisited++; for (ComponentNode dependent : node.getDependencies()) { dependent.removeDependent(node); if (dependent.isRoot()) { roots.add(dependent); } } } if (numVisited == components.size()) { return; } List<Component<?>> componentsInCycle = new ArrayList<>(); for (ComponentNode node : graph) { if (!node.isRoot() && !node.isLeaf()) { componentsInCycle.add(node.getComponent()); } } throw new DependencyCycleException(componentsInCycle); }  }
CycleDetector { static void detect(List<Component<?>> components) { Set<ComponentNode> graph = toGraph(components); Set<ComponentNode> roots = getRoots(graph); int numVisited = 0; while (!roots.isEmpty()) { ComponentNode node = roots.iterator().next(); roots.remove(node); numVisited++; for (ComponentNode dependent : node.getDependencies()) { dependent.removeDependent(node); if (dependent.isRoot()) { roots.add(dependent); } } } if (numVisited == components.size()) { return; } List<Component<?>> componentsInCycle = new ArrayList<>(); for (ComponentNode node : graph) { if (!node.isRoot() && !node.isLeaf()) { componentsInCycle.add(node.getComponent()); } } throw new DependencyCycleException(componentsInCycle); }   }
CycleDetector { static void detect(List<Component<?>> components) { Set<ComponentNode> graph = toGraph(components); Set<ComponentNode> roots = getRoots(graph); int numVisited = 0; while (!roots.isEmpty()) { ComponentNode node = roots.iterator().next(); roots.remove(node); numVisited++; for (ComponentNode dependent : node.getDependencies()) { dependent.removeDependent(node); if (dependent.isRoot()) { roots.add(dependent); } } } if (numVisited == components.size()) { return; } List<Component<?>> componentsInCycle = new ArrayList<>(); for (ComponentNode node : graph) { if (!node.isRoot() && !node.isLeaf()) { componentsInCycle.add(node.getComponent()); } } throw new DependencyCycleException(componentsInCycle); }    }