@Test public void testComplex() { Collection<DependsOn<String>> dependencies = Lists.newArrayListWithCapacity(4); dependencies.add(new DependsOn<String>("foo", "bar")); dependencies.add(new DependsOn<String>("foo", "buzz")); dependencies.add(new DependsOn<String>("bing", "foo")); dependencies.add(new DependsOn<String>("bong", "foo")); DependenciesScheduler<String> scheduler = new DependenciesScheduler<String>(); List<Collection<String>> schedule = scheduler.schedule(dependencies); assertEquals(3, schedule.size()); assertEquals(Sets.newHashSet(Arrays.asList("bar", "buzz")), Sets.newHashSet(schedule.get(0))); assertEquals(Sets.newHashSet(Collections.singleton("foo")), Sets.newHashSet(schedule.get(1))); assertEquals(Sets.newHashSet(Arrays.asList("bing", "bong")), Sets.newHashSet(schedule.get(2))); }
List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); }
DependenciesScheduler { List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); } }
DependenciesScheduler { List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); }  }
DependenciesScheduler { List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); }   }
DependenciesScheduler { List<Collection<T>> schedule(Collection<DependsOn<T>> dependencies) { log.info("Scheduling: {}", dependencies); Map<T,Collection<T>> prerequisites = Maps.newHashMapWithExpectedSize(dependencies.size()); for (DependsOn<T> dependency : dependencies) { T first = dependency.getHappensFirst(); T next = dependency.getHappensNext(); if (!prerequisites.containsKey(first)) { prerequisites.put(first, Lists.<T>newArrayList()); } if (next != null) { Collection<T> required = prerequisites.get(next); if (required == null) { required = Lists.newArrayList(); prerequisites.put(next, required); } required.add(first); } } return schedule(prerequisites); }    }