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.
Represents a Directed Acyclic Graph (dag).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassSimple iterator that iterates over the leaf path of nodes in the graph. -
Constructor Summary
ConstructorsConstructorDescriptionSeeDirectedAcyclicGraph(boolean)with `failFast = false`.DirectedAcyclicGraph(boolean failFast) Constructs a new Directed Acyclic Graph. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConnectedNodes(T child, T parent) Creates two new nodes (if no nodes exist for the given values) and creates a connection between them.voidaddConnection(Node<T> child, Node<T> parent) Adds a connection between two nodes.voidaddConnection(T child, T parent) Adds a connection between a child node and a parent node.Creates a new node on the graph if no node exists for the given value.voidclear()Clears the entire graph.getAllChildren(Node<T> node) Finds all children of a node in the graph.getAllChildren(T val) Finds all children of a value in the graph.Gets the path from the leaves (i.e. nodes without parents) down to the base of the graph.Tries to retrieve a node from the graph.iterator()Removes a node from this graph.Removes a value from this graph.voidremoveAll(Collection<T> values) Removes multiple values from this graph.voidremoveAllNodes(Collection<Node<T>> values) Removes multiple nodes from this graph.voidremoveConnection(T child, T parent) Removes a connection between a child and a parent if such a connection exists.intsize()Gets the current number of nodes in this graph.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
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()SeeDirectedAcyclicGraph(boolean)with `failFast = false`.
-
-
Method Details
-
addNode
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
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
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
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
Creates two new nodes (if no nodes exist for the given values) and creates a connection between them.- 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
Removes a value from this graph.- Parameters:
val- The value to remove.- Returns:
- The value if it was removed, otherwise null.
-
remove
Removes a node from this graph.- Parameters:
node- The node to remove.- Returns:
- The node if it was removed, otherwise null.
-
removeAll
Removes multiple values from this graph.- Parameters:
values- A collection of values.
-
removeAllNodes
Removes multiple nodes from this graph.- Parameters:
values- A collection of nodes.
-
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
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
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
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
- Specified by:
iteratorin interfaceIterable<T>- Throws:
IllegalStateException- If the acyclic constraint of this graph has been violated.
-