@Test void testGetAllDependants() { when(getDepth.apply(1)).thenReturn(1); when(getDepth.apply(2)).thenReturn(1); when(getDepth.apply(3)).thenReturn(3); when(getDepth.apply(4)).thenReturn(1); when(getDependants.apply(0)).thenReturn(ImmutableSet.of(1, 4)); when(getDependants.apply(1)).thenReturn(ImmutableSet.of(2)); when(getDependants.apply(2)).thenReturn(ImmutableSet.of(3)); when(getDependants.apply(3)).thenReturn(ImmutableSet.of(2)); when(getDependants.apply(4)).thenReturn(emptySet()); assertEquals( of(1, 4, 3), genericDependencyResolver.getAllDependants(0, getDepth, getDependants)); verify(getDepth, never()).apply(0); }
public <A> Set<A> getAllDependants( A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants) { Set<A> currentGeneration = singleton(item); Set<A> result = newHashSet(); Set<A> visited = newHashSet(); for (int depth = 0; !currentGeneration.isEmpty(); depth++) { currentGeneration = copyOf(difference(getDirectDependants(currentGeneration, getDependants), visited)); result.addAll( currentGeneration.stream().filter(getDepthFilter(depth, getDepth)).collect(toSet())); visited.addAll(currentGeneration); } return result; }
GenericDependencyResolver { public <A> Set<A> getAllDependants( A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants) { Set<A> currentGeneration = singleton(item); Set<A> result = newHashSet(); Set<A> visited = newHashSet(); for (int depth = 0; !currentGeneration.isEmpty(); depth++) { currentGeneration = copyOf(difference(getDirectDependants(currentGeneration, getDependants), visited)); result.addAll( currentGeneration.stream().filter(getDepthFilter(depth, getDepth)).collect(toSet())); visited.addAll(currentGeneration); } return result; } }
GenericDependencyResolver { public <A> Set<A> getAllDependants( A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants) { Set<A> currentGeneration = singleton(item); Set<A> result = newHashSet(); Set<A> visited = newHashSet(); for (int depth = 0; !currentGeneration.isEmpty(); depth++) { currentGeneration = copyOf(difference(getDirectDependants(currentGeneration, getDependants), visited)); result.addAll( currentGeneration.stream().filter(getDepthFilter(depth, getDepth)).collect(toSet())); visited.addAll(currentGeneration); } return result; }  }
GenericDependencyResolver { public <A> Set<A> getAllDependants( A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants) { Set<A> currentGeneration = singleton(item); Set<A> result = newHashSet(); Set<A> visited = newHashSet(); for (int depth = 0; !currentGeneration.isEmpty(); depth++) { currentGeneration = copyOf(difference(getDirectDependants(currentGeneration, getDependants), visited)); result.addAll( currentGeneration.stream().filter(getDepthFilter(depth, getDepth)).collect(toSet())); visited.addAll(currentGeneration); } 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> Set<A> getAllDependants( A item, Function<A, Integer> getDepth, Function<A, Set<A>> getDependants) { Set<A> currentGeneration = singleton(item); Set<A> result = newHashSet(); Set<A> visited = newHashSet(); for (int depth = 0; !currentGeneration.isEmpty(); depth++) { currentGeneration = copyOf(difference(getDirectDependants(currentGeneration, getDependants), visited)); result.addAll( currentGeneration.stream().filter(getDepthFilter(depth, getDepth)).collect(toSet())); visited.addAll(currentGeneration); } 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);  }