Ad
Code
Diff
  • 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);
    • }
    • }
    • }
    • }
Code
Diff
  • 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);
    • }
    • }