import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; public class DependencyTracker { Map<String, Set<String>> theDependencies = new HashMap<>(); public Set<String> getDependencies(String dependencyName) { Set<String> resultingDependencies = new HashSet<>(); Set<String> visitedDependencies = new HashSet<>(); getDependenciesRecursive(resultingDependencies, visitedDependencies, dependencyName); return resultingDependencies; } public void registerDependency(String dependent, String dependedUpon) { Set<String> currentDependencies = getDirectDependencies(dependent); currentDependencies.add(dependedUpon); theDependencies.put(dependent, currentDependencies); } private Set<String> getDirectDependencies(String dependencyName) { return theDependencies.getOrDefault(dependencyName, new HashSet<>()); } private void getDependenciesRecursive(Set<String> resultingDependencies, Set<String> visitedDependencies, String dependencyName) { Set<String> directDependencies = getDirectDependencies(dependencyName); resultingDependencies.addAll(directDependencies); for (String subDependency : directDependencies) { if (!visitedDependencies.contains(subDependency)) { visitedDependencies.add(subDependency); getDependenciesRecursive(resultingDependencies, visitedDependencies, subDependency); } } } }
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.HashSet;
- public class DependencyTracker {
- Map<String, Set<String>> theDependencies = new HashMap<>();
- public Set<String> getDependencies(String dependencyName) {
Set<String> allDependencies = new HashSet<>();Set<String> directDependencies = getDirectDependencies(dependencyName);- Set<String> resultingDependencies = new HashSet<>();
- Set<String> visitedDependencies = new HashSet<>();
allDependencies.addAll(directDependencies);for (String subDependency : directDependencies) {allDependencies.addAll(getDependencies(subDependency));}return allDependencies;- getDependenciesRecursive(resultingDependencies, visitedDependencies, dependencyName);
- return resultingDependencies;
- }
- public void registerDependency(String dependent, String dependedUpon) {
- Set<String> currentDependencies = getDirectDependencies(dependent);
- currentDependencies.add(dependedUpon);
- theDependencies.put(dependent, currentDependencies);
- }
- private Set<String> getDirectDependencies(String dependencyName) {
- return theDependencies.getOrDefault(dependencyName, new HashSet<>());
- }
- private void getDependenciesRecursive(Set<String> resultingDependencies, Set<String> visitedDependencies, String dependencyName) {
- Set<String> directDependencies = getDirectDependencies(dependencyName);
- resultingDependencies.addAll(directDependencies);
- for (String subDependency : directDependencies) {
- if (!visitedDependencies.contains(subDependency)) {
- visitedDependencies.add(subDependency);
- getDependenciesRecursive(resultingDependencies, visitedDependencies, subDependency);
- }
- }
- }
- }
import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertEquals; class DependencyTrackerTest { private DependencyTracker tracker; @BeforeEach void setUp() { tracker = new DependencyTracker(); } @Test void noDependencies() { Set<String> dependencies = tracker.getDependencies("H"); assertEquals(Set.of(), dependencies); } @Test void oneDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("G"); assertEquals(Set.of("H"), dependencies); } @Test void noDependenciesWhenOtherDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("K"); assertEquals(Set.of(), dependencies); } @Test void twoDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("A","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void oneTransitiveDependency() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void twoTransitiveDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); tracker.registerDependency("C","D"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C", "D"), dependencies); } @Test void twoBranchingTransitiveDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("A","C"); tracker.registerDependency("B","D"); tracker.registerDependency("C","E"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C", "D", "E"), dependencies); } @Test void cyclicDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); tracker.registerDependency("C","A"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C", "A"), dependencies); } }
- import java.util.Set;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.BeforeEach;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class DependencyTrackerTest {
- private DependencyTracker tracker;
- @BeforeEach
- void setUp() {
- tracker = new DependencyTracker();
- }
- @Test
- void noDependencies() {
- Set<String> dependencies = tracker.getDependencies("H");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void oneDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("G");
- assertEquals(Set.of("H"), dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("K");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void twoDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void oneTransitiveDependency() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void twoTransitiveDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- tracker.registerDependency("C","D");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C", "D"), dependencies);
- }
- @Test
- void twoBranchingTransitiveDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- tracker.registerDependency("B","D");
- tracker.registerDependency("C","E");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C", "D", "E"), dependencies);
- }
- @Test
- void cyclicDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- tracker.registerDependency("C","A");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C", "A"), dependencies);
- }
- }
import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; public class DependencyTracker { Map<String, Set<String>> theDependencies = new HashMap<>(); public Set<String> getDependencies(String dependencyName) { return theDependencies.getOrDefault(dependencyName, Set.of()); } // If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon public void registerDependency(String dependent, String dependedUpon) { Set<String> currentDependencies = theDependencies.getOrDefault(dependent, new HashSet<>()); currentDependencies.add(dependedUpon); theDependencies.put(dependent, currentDependencies); } }
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.HashSet;
- public class DependencyTracker {
Map<String, String> theDependencies = new HashMap<>();- Map<String, Set<String>> theDependencies = new HashMap<>();
public String getDependencies(String dependencyName) {return theDependencies.getOrDefault(dependencyName, "");- public Set<String> getDependencies(String dependencyName) {
- return theDependencies.getOrDefault(dependencyName, Set.of());
- }
- // If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon
- public void registerDependency(String dependent, String dependedUpon) {
theDependencies.put(dependent, dependedUpon);- Set<String> currentDependencies = theDependencies.getOrDefault(dependent, new HashSet<>());
- currentDependencies.add(dependedUpon);
- theDependencies.put(dependent, currentDependencies);
- }
- }
import java.util.Set; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class DependencyTrackerTest { @Test void noDependencies() { DependencyTracker tracker = new DependencyTracker(); Set<String> dependencies = tracker.getDependencies("H"); assertEquals(Set.of(), dependencies); } @Test void oneDependency() { DependencyTracker tracker = new DependencyTracker(); tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("G"); assertEquals(Set.of("H"), dependencies); } @Test void noDependenciesWhenOtherDependency() { DependencyTracker tracker = new DependencyTracker(); tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("K"); assertEquals(Set.of(), dependencies); } @Test void twoDependencies() { DependencyTracker tracker = new DependencyTracker(); tracker.registerDependency("A","B"); tracker.registerDependency("A","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } }
- import java.util.Set;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class DependencyTrackerTest {
- @Test
- void noDependencies() {
- DependencyTracker tracker = new DependencyTracker();
String dependencies = tracker.getDependencies("H");assertEquals("", dependencies);- Set<String> dependencies = tracker.getDependencies("H");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void oneDependency() {
- DependencyTracker tracker = new DependencyTracker();
- tracker.registerDependency("G","H");
String dependencies = tracker.getDependencies("G");assertEquals("H", dependencies);- Set<String> dependencies = tracker.getDependencies("G");
- assertEquals(Set.of("H"), dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
- DependencyTracker tracker = new DependencyTracker();
- tracker.registerDependency("G","H");
String dependencies = tracker.getDependencies("K");assertEquals("", dependencies);- Set<String> dependencies = tracker.getDependencies("K");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void twoDependencies() {
- DependencyTracker tracker = new DependencyTracker();
- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- }