Class DependencyGraph<T>
java.lang.Object
tech.guilhermekaua.spigotboot.core.utils.DependencyGraph<T>
Directed graph representing dependencies between two nodes.
An edge from A to B means "A depends on B" (A references B).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a directed edge from source to target.voidAdds a node to the graph without any edges.voidclear()Clears all nodes and edges from the graph.Detects if the graph contains a cycle.Gets all nodes in the graph.getDependencies(T key) Gets direct dependencies of a node.getDirectDependents(T key) Gets direct dependents of a node (nodes that depend on this node).Gets reverse dependencies map for reload propagation.getTransitiveDependents(T key) Gets all transitive dependents of a node.booleanisEmpty()Checks if the graph is empty.voidremoveNode(T key) Removes a node completely from the graph (including all its edges).voidremoveOutgoingEdges(T from) Removes all outgoing edges from a node.voidreplaceOutgoingEdges(T from, @NotNull Set<T> newTargets) Replaces all outgoing edges from a node with new targets.intsize()Gets the number of nodes in the graph.Computes topological order of all nodes.topologicalOrderSubset(@NotNull Set<T> subset) Computes topological order for a subset of nodes.
-
Constructor Details
-
DependencyGraph
-
DependencyGraph
public DependencyGraph()
-
-
Method Details
-
addNode
Adds a node to the graph without any edges.- Parameters:
key- the node key
-
addEdge
Adds a directed edge from source to target.This represents that
fromdepends onto.- Parameters:
from- the source node (the one that has the reference)to- the target node (the one being referenced)
-
getAllNodes
Gets all nodes in the graph.- Returns:
- unmodifiable set of all nodes
-
getDependencies
Gets direct dependencies of a node.- Parameters:
key- the node key- Returns:
- unmodifiable set of dependencies
-
getDirectDependents
Gets direct dependents of a node (nodes that depend on this node).- Parameters:
key- the node key- Returns:
- unmodifiable set of dependents
-
getTransitiveDependents
Gets all transitive dependents of a node.This computes the closure of all nodes that directly or indirectly depend on the given node.
- Parameters:
key- the node key- Returns:
- set of all transitive dependents (not including the node itself)
-
getReverseDependencies
Gets reverse dependencies map for reload propagation.- Returns:
- unmodifiable map of node -> set of nodes that depend on it
-
topologicalOrder
Computes topological order of all nodes.Returns nodes in an order where dependencies come before dependents. This means if A depends on B, then B will appear before A in the result.
- Returns:
- ordered list of nodes
- Throws:
CycleDetectedException- if a cycle is detected
-
topologicalOrderSubset
@NotNull public @NotNull List<T> topologicalOrderSubset(@NotNull @NotNull Set<T> subset) throws CycleDetectedException Computes topological order for a subset of nodes.Only considers edges between nodes in the subset.
- Parameters:
subset- the subset of nodes to order- Returns:
- ordered list
- Throws:
CycleDetectedException- if a cycle is detected
-
detectCycle
Detects if the graph contains a cycle.- Returns:
- the cycle path if found, or empty if acyclic
-
clear
public void clear()Clears all nodes and edges from the graph. -
removeOutgoingEdges
Removes all outgoing edges from a node.- Parameters:
from- the source node
-
replaceOutgoingEdges
Replaces all outgoing edges from a node with new targets.Equivalent to
removeOutgoingEdges(from)followed byaddEdge(from, target)for each new target.- Parameters:
from- the source nodenewTargets- the new set of target nodes
-
removeNode
Removes a node completely from the graph (including all its edges).- Parameters:
key- the node to remove
-
size
public int size()Gets the number of nodes in the graph.- Returns:
- node count
-
isEmpty
public boolean isEmpty()Checks if the graph is empty.- Returns:
- true if no nodes
-