Class AbstractDependencyGraph<T>

  • Type Parameters:
    T - the type of value. Some concrete classes might have restrictions on T.
    Direct Known Subclasses:
    DependencyGraph, DeterministicDependencyGraph

    public abstract class AbstractDependencyGraph<T>
    extends java.lang.Object
    Class for managing the visiting (processing) of a set of values where some values depend on other values being process before them. In other words, an acyclic directed graph will be formed where the vertexes are the values and the edges represent dependencies. Values can only be removed if they have no dependencies. Since the graph is acyclic, as values are removed that have no dependencies, other nodes that depend on those nodes will become eligible for processing and removal. If cycles are introduced, they will eventually cause an IllegalState exception to occur when removing and processing values. There is also a hasCycles() method that can be called before processing to find cycle problems up front without wasting time processing values.
    See Also:
    DependencyGraph, DeterministicDependencyGraph
    • Constructor Detail

      • AbstractDependencyGraph

        public AbstractDependencyGraph()
    • Method Detail

      • createNodeSet

        protected abstract java.util.Set<T> createNodeSet()
        Creates the Set of Nodes appropriate for the implementer.
        Returns:
        a new Set of Nodes.
      • addValue

        public void addValue​(T value)
        Adds the value to this graph.
        Parameters:
        value - the value to add
      • size

        public int size()
        Returns the number of values in this graph.
        Returns:
        the number of values in this graph.
      • isEmpty

        public boolean isEmpty()
        Returns true if the graph has no values;
        Returns:
        true if the graph has no values;
      • contains

        public boolean contains​(T value)
        Returns true if this graph has the given key.
        Parameters:
        value - the value to check if its in this graph
        Returns:
        true if this graph has the given key.
      • getValues

        public java.util.Set<T> getValues()
        Returns the set of values in this graph.
        Returns:
        the set of values in this graph.
      • getNodeMapValues

        public abstract java.util.Set<T> getNodeMapValues()
        Returns the set of values in this graph.
        Returns:
        the set of values in this graph.
      • addDependency

        public void addDependency​(T value1,
                                  T value2)
        Add a dependency such that value1 depends on value2. Both value1 and value2 will be added to the graph if they are not already in the graph.
        Parameters:
        value1 - the value that depends on value2
        value2 - the value that value1 is depending on
      • hasUnVisitedIndependentValues

        public boolean hasUnVisitedIndependentValues()
        Returns true if there are unvisited values ready (no dependencies) for processing.
        Returns:
        true if there are unvisited values ready for processing.
        Throws:
        java.lang.IllegalStateException - is thrown if the graph is not empty and there are no nodes without dependency which indicates there is a cycle in the graph.
      • pop

        public T pop()
        Removes and returns a value that has no dependencies from the graph. If the graph is empty or all the nodes without dependencies are currently visited, then null will be returned. NOTE: If the getUnvisitedIndependentValues() method has been called(), this method may return null until all those "visited" nodes are removed from the graph.
        Returns:
        return an arbitrary value that has no dependencies and hasn't been visited or null.
      • hasCycles

        public boolean hasCycles()
        Checks if this graph has cycles. Normal processing of this graph will eventually reveal a cycle and throw an exception at the time it is detected. This method allows for a "fail fast" way to detect cycles.
        Returns:
        true if cycles exist in the graph.
      • getUnvisitedIndependentValues

        public java.util.Set<T> getUnvisitedIndependentValues()
        Returns a set of all values that have no dependencies. As values are removed from the graph, dependencies will be removed and additional values will be eligible to be returned by this method. Once a value has been retrieved using this method, it will be considered "visited" and future calls to this method will not include those values. To continue processing the values in the graph, all values return from this method should eventually be deleted from the graph to "free up" other values. NOTE: values retrieved by this method will no longer be eligible for return by the pop() method.
        Returns:
        the set of values without dependencies that have never been returned by this method before.
      • getAllIndependentValues

        public java.util.Set<T> getAllIndependentValues()
        Returns the set of all values that have no dependencies regardless of whether or not they have been "visited" (by the getUnvisitedIndependentValues() method.
        Returns:
        return the set of all values that have no dependencies.
      • remove

        public void remove​(T value)
        Removes the value from the graph. Any dependency from this node to another will be removed, possible allowing nodes that depend on this node to be eligible for processing.
        Parameters:
        value - the value to remove from the graph.
      • getDependentValues

        public java.util.Set<T> getDependentValues​(T value)
        Returns a set of values that depend on the given value.
        Parameters:
        value - the value that other values may depend on.
        Returns:
        a set of values that depend on the given value.