Class DependencyGraph<T>


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

      Constructors 
      Constructor Description
      DependencyGraph()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addDependency​(T from, T to)
      Add a dependency between two elements
      void clearAll()  
      java.util.Set<T> getDependents​(T element)
      Return the dependents
      boolean hasDependent​(T element)
      Check if an element is depended on
      void remove​(T element)
      Remove element from the graph
      void removeDependency​(T from, T to)
      Remove a dependency
      java.util.List<T> topologicalSort()
      Calculates a topological sort of the graph, in linear time
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DependencyGraph

        public DependencyGraph()
    • Method Detail

      • topologicalSort

        public java.util.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:
        java.lang.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:
        java.lang.IllegalArgumentException - if element is not present in the graph
      • getDependents

        public java.util.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