@SuppressWarnings("deprecation") @Test void testCyclicDependencies() { DependentOn d1 = new DependentOn("1"); DependentOn d2 = new DependentOn("2"); DependentOn d3 = new DependentOn("3"); d1.addDependency(d2); d2.addDependency(d3); d3.addDependency(d1); assertThrows( MolgenisDataException.class, () -> genericDependencyResolver.resolve( Sets.newHashSet(d1, d2, d3), DependentOn::getDependencies)); }
public <A> List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies) { List<A> result = newArrayList(); Set<A> alreadyResolved = newHashSet(); Set<A> stillToResolve = newHashSet(items); while (!stillToResolve.isEmpty()) { List<A> newlyResolved = stillToResolve.stream() .filter(item -> alreadyResolved.containsAll(getDependencies.apply(item))) .collect(Collectors.toList()); if (newlyResolved.isEmpty()) { throw new MolgenisDataException( "Could not resolve dependencies of items " + stillToResolve + ". Are there circular dependencies?"); } alreadyResolved.addAll(newlyResolved); stillToResolve.removeAll(newlyResolved); result.addAll(newlyResolved); } return result; }
GenericDependencyResolver { public <A> List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies) { List<A> result = newArrayList(); Set<A> alreadyResolved = newHashSet(); Set<A> stillToResolve = newHashSet(items); while (!stillToResolve.isEmpty()) { List<A> newlyResolved = stillToResolve.stream() .filter(item -> alreadyResolved.containsAll(getDependencies.apply(item))) .collect(Collectors.toList()); if (newlyResolved.isEmpty()) { throw new MolgenisDataException( "Could not resolve dependencies of items " + stillToResolve + ". Are there circular dependencies?"); } alreadyResolved.addAll(newlyResolved); stillToResolve.removeAll(newlyResolved); result.addAll(newlyResolved); } return result; } }
GenericDependencyResolver { public <A> List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies) { List<A> result = newArrayList(); Set<A> alreadyResolved = newHashSet(); Set<A> stillToResolve = newHashSet(items); while (!stillToResolve.isEmpty()) { List<A> newlyResolved = stillToResolve.stream() .filter(item -> alreadyResolved.containsAll(getDependencies.apply(item))) .collect(Collectors.toList()); if (newlyResolved.isEmpty()) { throw new MolgenisDataException( "Could not resolve dependencies of items " + stillToResolve + ". Are there circular dependencies?"); } alreadyResolved.addAll(newlyResolved); stillToResolve.removeAll(newlyResolved); result.addAll(newlyResolved); } return result; }  }
GenericDependencyResolver { public <A> List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies) { List<A> result = newArrayList(); Set<A> alreadyResolved = newHashSet(); Set<A> stillToResolve = newHashSet(items); while (!stillToResolve.isEmpty()) { List<A> newlyResolved = stillToResolve.stream() .filter(item -> alreadyResolved.containsAll(getDependencies.apply(item))) .collect(Collectors.toList()); if (newlyResolved.isEmpty()) { throw new MolgenisDataException( "Could not resolve dependencies of items " + stillToResolve + ". Are there circular dependencies?"); } alreadyResolved.addAll(newlyResolved); stillToResolve.removeAll(newlyResolved); result.addAll(newlyResolved); } return result; }  List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies); Set<A> getAllDependants(
      A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants); }
GenericDependencyResolver { public <A> List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies) { List<A> result = newArrayList(); Set<A> alreadyResolved = newHashSet(); Set<A> stillToResolve = newHashSet(items); while (!stillToResolve.isEmpty()) { List<A> newlyResolved = stillToResolve.stream() .filter(item -> alreadyResolved.containsAll(getDependencies.apply(item))) .collect(Collectors.toList()); if (newlyResolved.isEmpty()) { throw new MolgenisDataException( "Could not resolve dependencies of items " + stillToResolve + ". Are there circular dependencies?"); } alreadyResolved.addAll(newlyResolved); stillToResolve.removeAll(newlyResolved); result.addAll(newlyResolved); } return result; }  List<A> resolve(Collection<A> items, Function<A, Set<A>> getDependencies); Set<A> getAllDependants(
      A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants);  }