Package software.amazon.smithy.utils
Class DependencyGraph<T>
java.lang.Object
software.amazon.smithy.utils.DependencyGraph<T>
- Type Parameters:
T- The data type stored in the graph.
- All Implemented Interfaces:
Iterable<T>,Collection<T>
A basic dependency graph.
Iteration and ordering in collection methods are based on insertion order.
A topographically sorted view of the graph is provided by toSortedList().
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new dependency graph with the default initial capacity (16).DependencyGraph(int initialCapacity) Constructs a new, empty dependency graph with the specified initial capacity.Constructs a new dependency graph with the same elements as the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> c) voidaddDependencies(T what, Collection<T> dependsOn) Adds a set of dependencies to a node.voidaddDependency(T what, T dependsOn) Adds a dependency between two nodes.voidclear()booleanbooleancontainsAll(Collection<?> c) Finds all strongly-connected components of the graph.getDirectDependants(T node) Gets all the nodes that depend on a given node.getDirectDependencies(T node) Gets all the direct dependencies of a given node.booleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> c) voidremoveDependencies(T what, Collection<T> dependsOn) Removes a set of dependencies from a node.voidremoveDependency(T what, T dependsOn) Removes a dependency from a node.booleanretainAll(Collection<?> c) voidsetDependencies(T what, Collection<T> dependsOn) Sets the dependencies for a node.intsize()Object[]toArray()<T1> T1[]toArray(T1[] a) Gets a topographically sorted view of the graph.toSortedList(Comparator<T> comparator) Gets a topographically sorted view of the graph where independent nodes are evaluated in the order given by the comparator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
DependencyGraph
public DependencyGraph(int initialCapacity) Constructs a new, empty dependency graph with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the dependency graph.
-
DependencyGraph
Constructs a new dependency graph with the same elements as the specified collection. The dependency graph is created with an initial capacity equal to the size of the collection.- Parameters:
c- the collection whose elements are to be added to this graph.
-
DependencyGraph
public DependencyGraph()Constructs a new dependency graph with the default initial capacity (16).
-
-
Method Details
-
add
- Specified by:
addin interfaceCollection<T>
-
addDependency
Adds a dependency between two nodes.If either node is not already present in the graph, it is added.
- Parameters:
what- The node to add a dependency to.dependsOn- The dependency to add.
-
addDependencies
Adds a set of dependencies to a node.If any node is not already present in the graph, it is added.
- Parameters:
what- The node to add dependencies to.dependsOn- The dependencies to add.
-
setDependencies
Sets the dependencies for a node.If the node already has dependencies, they will be replaced.
- Parameters:
what- The node to set dependencies for.dependsOn- The dependencies to set.
-
remove
- Specified by:
removein interfaceCollection<T>
-
removeDependency
Removes a dependency from a node.- Parameters:
what- The node to remove a dependency from.dependsOn- The dependency to remove.
-
removeDependencies
Removes a set of dependencies from a node.- Parameters:
what- The node to remove dependencies from.dependsOn- The dependencies to remove.
-
getIndependentNodes
- Returns:
- Returns a set of nodes that have no remaining dependencies.
-
getDirectDependencies
Gets all the direct dependencies of a given node.- Parameters:
node- The node whose dependencies should be fetched.- Returns:
- A set of dependencies.
-
getDirectDependants
Gets all the nodes that depend on a given node.- Parameters:
node- The node whose dependants should be fetched.- Returns:
- A set of dependants.
-
findCycles
Finds all strongly-connected components of the graph.Cycles returned are not *elementary* cycles. That is, if two or more cycles share any nodes, they will be returned as a single cycle. For example, take the following graph:
A -> B -> C -> A B -> D -> BThis graph has one set of strongly-connected components (
{B, C, A, D}) made up of two elementary cycles ({B, C, A}andD, B). This method will return the set of strongly-connected components,{B, C, A, D}.- Returns:
- A list of all strongly-connected components in the graph.
-
size
public int size()- Specified by:
sizein interfaceCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<T>
-
contains
- Specified by:
containsin interfaceCollection<T>
-
iterator
-
toArray
- Specified by:
toArrayin interfaceCollection<T>
-
toArray
public <T1> T1[] toArray(T1[] a) - Specified by:
toArrayin interfaceCollection<T>
-
toSortedList
Gets a topographically sorted view of the graph.- Returns:
- Returns a topographically sorted list view of the graph.
-
toSortedList
Gets a topographically sorted view of the graph where independent nodes are evaluated in the order given by the comparator.- Parameters:
comparator- A comparator used to sort independent nodes.- Returns:
- Returns a topographically sorted list view of the graph.
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>
-
addAll
- Specified by:
addAllin interfaceCollection<T>
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>
-
retainAll
- Specified by:
retainAllin interfaceCollection<T>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>
-