Class DirectedAcyclicGraph<T>

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.data.graph.DirectedAcyclicGraph<T>
Type Parameters:
T - The type of data to store in the nodes.
All Implemented Interfaces:
Iterable<Node<T>>

public final class DirectedAcyclicGraph<T> extends Object implements Iterable<Node<T>>
Represents a Directed Acyclic Graph (dag).
  • Constructor Details

    • DirectedAcyclicGraph

      public DirectedAcyclicGraph(boolean failFast)
      Constructs a new Directed Acyclic Graph.
      Parameters:
      failFast - Whether to fail 'fast'. This will cause the cyclic nature of this graph to be checked every time a connection is added. This has the advantage of failing immediately when trying to add a cyclic dependency, at the cost of running the expensive cyclic dependency check operation for every change.

      Unlike the name might imply, disabling this is going to be much faster overall, as it'll only verify the acyclic constraint when needed (e.g. for iterating) rather than for every insertion.

    • DirectedAcyclicGraph

      public DirectedAcyclicGraph()
      See DirectedAcyclicGraph(boolean) with `failFast = false`.
  • Method Details

    • addNode

      public Node<T> addNode(T val)
      Creates a new node on the graph if no node exists for the given value.

      Use addConnection(Object, Object) to add connections between nodes.

      Parameters:
      val - The value of the node.
    • getNode

      public Optional<Node<T>> getNode(T val)
      Tries to retrieve a node from the graph.
      Parameters:
      val - The value of the node to look for.
      Returns:
      The node if it could be found, otherwise an empty nullable.
    • getAllChildren

      public List<T> getAllChildren(T val)
      Finds all children of a value in the graph.
      Parameters:
      val - The value for which to retrieve all children.
      Returns:
      All children of the node associated with the provided value.
      Throws:
      IllegalStateException - If the acyclic constraint of this graph has been violated.
      NullPointerException - If no existing node could be found for the provided value.
    • getAllChildren

      public List<T> getAllChildren(Node<T> node)
      Finds all children of a node in the graph.
      Parameters:
      node - The node for which to retrieve all children.
      Returns:
      All children of the node.
      Throws:
      IllegalStateException - If the acyclic constraint of this graph has been violated.
    • clear

      public void clear()
      Clears the entire graph.
    • addConnectedNodes

      public void addConnectedNodes(T child, T parent)
      Creates two new nodes (if no nodes exist for the given values) and creates a connection between them.

      See addNode(Object) and addConnection(Object, Object).

      Parameters:
      child - The child node to add to the graph.
      parent - The parent node to add to the graph.
    • size

      public int size()
      Gets the current number of nodes in this graph.
      Returns:
      The size of this graph.
    • remove

      @Nullable public T remove(T val)
      Removes a value from this graph.
      Parameters:
      val - The value to remove.
      Returns:
      The value if it was removed, otherwise null.
    • remove

      @Nullable public Node<T> remove(Node<T> node)
      Removes a node from this graph.
      Parameters:
      node - The node to remove.
      Returns:
      The node if it was removed, otherwise null.
    • removeAll

      public void removeAll(Collection<T> values)
      Removes multiple values from this graph.
      Parameters:
      values - A collection of values.
    • removeAllNodes

      public void removeAllNodes(Collection<Node<T>> values)
      Removes multiple nodes from this graph.
      Parameters:
      values - A collection of nodes.
    • getLeafPath

      public List<T> getLeafPath()
      Gets the path from the leaves (i.e. nodes without parents) down to the base of the graph.

      This means that for an entry `x` in position n in this list, `x` might have one or more dependencies on any entries with `index < n`, but not on any entries with `index > n`.

      Returns:
      The leaf path.
      Throws:
      IllegalStateException - If the acyclic constraint of this graph has been violated.
    • addConnection

      public void addConnection(T child, T parent)
      Adds a connection between a child node and a parent node.
      Parameters:
      child - The child node. This is where the connection is coming from.
      parent - The parent node. This is the target of the connection.
      Throws:
      NullPointerException - If no existing node could be found for either the parent or the child value.
    • addConnection

      public void addConnection(Node<T> child, Node<T> parent)
      Adds a connection between two nodes.
      Parameters:
      child - The child node. This is where the connection is coming from.
      parent - The parent node. This is the target of the connection.
    • removeConnection

      public void removeConnection(T child, T parent)
      Removes a connection between a child and a parent if such a connection exists.
      Parameters:
      child - The child node.
      parent - The parent node.
      Throws:
      NullPointerException - If no existing node could be found for either the parent or the child value.
    • iterator

      public Iterator<Node<T>> iterator()
      Specified by:
      iterator in interface Iterable<T>
      Throws:
      IllegalStateException - If the acyclic constraint of this graph has been violated.