Class DependencyGraph<T>

java.lang.Object
tech.guilhermekaua.spigotboot.core.utils.DependencyGraph<T>

public class DependencyGraph<T> extends Object
Directed graph representing dependencies between two nodes.

An edge from A to B means "A depends on B" (A references B).

  • Constructor Details

    • DependencyGraph

      public DependencyGraph(@Nullable @Nullable Function<T,String> nodeLabeler)
    • DependencyGraph

      public DependencyGraph()
  • Method Details

    • addNode

      public void addNode(@NotNull T key)
      Adds a node to the graph without any edges.
      Parameters:
      key - the node key
    • addEdge

      public void addEdge(@NotNull T from, @NotNull T to)
      Adds a directed edge from source to target.

      This represents that from depends on to.

      Parameters:
      from - the source node (the one that has the reference)
      to - the target node (the one being referenced)
    • getAllNodes

      @NotNull public @NotNull Set<T> getAllNodes()
      Gets all nodes in the graph.
      Returns:
      unmodifiable set of all nodes
    • getDependencies

      @NotNull public @NotNull Set<T> getDependencies(@NotNull T key)
      Gets direct dependencies of a node.
      Parameters:
      key - the node key
      Returns:
      unmodifiable set of dependencies
    • getDirectDependents

      @NotNull public @NotNull Set<T> getDirectDependents(@NotNull T key)
      Gets direct dependents of a node (nodes that depend on this node).
      Parameters:
      key - the node key
      Returns:
      unmodifiable set of dependents
    • getTransitiveDependents

      @NotNull public @NotNull Set<T> getTransitiveDependents(@NotNull T key)
      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

      @NotNull public @NotNull Map<T,Set<T>> getReverseDependencies()
      Gets reverse dependencies map for reload propagation.
      Returns:
      unmodifiable map of node -> set of nodes that depend on it
    • topologicalOrder

      @NotNull public @NotNull List<T> topologicalOrder() throws CycleDetectedException
      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

      @NotNull public @NotNull Optional<List<T>> 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

      public void removeOutgoingEdges(@NotNull T from)
      Removes all outgoing edges from a node.
      Parameters:
      from - the source node
    • replaceOutgoingEdges

      public void replaceOutgoingEdges(@NotNull T from, @NotNull @NotNull Set<T> newTargets)
      Replaces all outgoing edges from a node with new targets.

      Equivalent to removeOutgoingEdges(from) followed by addEdge(from, target) for each new target.

      Parameters:
      from - the source node
      newTargets - the new set of target nodes
    • removeNode

      public void removeNode(@NotNull T key)
      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