Class DependencyGraph<T>


  • @ThreadSafe
    public final class DependencyGraph<T>
    extends Object
    Graph to track dependencies between objects
    Since:
    7.0
    Author:
    gustavonalle
    • Constructor Detail

      • DependencyGraph

        public DependencyGraph()
    • Method Detail

      • topologicalSort

        public List<T> topologicalSort()
                                throws CyclicDependencyException
        Calculates a topological sort of the graph, in linear time
        Returns:
        List elements sorted respecting dependencies
        Throws:
        CyclicDependencyException - if cycles are present in the graph and thus no topological sort is possible
      • addDependency

        public void addDependency​(T from,
                                  T to)
        Add a dependency between two elements
        Parameters:
        from - From element
        to - To element
      • removeDependency

        public void removeDependency​(T from,
                                     T to)
        Remove a dependency
        Parameters:
        from - From element
        to - To element
        Throws:
        IllegalArgumentException - if either to or from don't exist
      • clearAll

        public void clearAll()
      • hasDependent

        public boolean hasDependent​(T element)
        Check if an element is depended on
        Parameters:
        element - Element stored in the graph
        Returns:
        true if exists any dependency on element
        Throws:
        IllegalArgumentException - if element is not present in the graph
      • getDependents

        public Set<T> getDependents​(T element)
        Return the dependents
        Parameters:
        element - Element contained in the graph
        Returns:
        list of elements depending on element
      • remove

        public void remove​(T element)
        Remove element from the graph
        Parameters:
        element - the element