Class HashGraph<T,U>
- All Implemented Interfaces:
Graph<T,U>
This class is used to construct and manage graph structures. This is a simple class that makes the navigation and usage of Graphs simple and accessible.
Graphs
Graphs are simple structures that model nodes with any number of connections between nodes. The connections are bi-directional and are called Edges. A two node graph with an edge between the nodes looks like this:
node1 <---> node2
The important point about Graphs is that they don't enforce a top level node that controls the entire structure like trees do. Instead, the graph has access to all nodes and the connections between them. This makes finding a Node easy and then traversing the graph also easy.
Generics
There are two generics for a Graph. The first variable T is the content of the nodes themselves. Each node can stored a single value. The second generic is the value that can be associated with the Edge between nodes. This is carried throughout the entire graph structure making it very strongly typed.
Internals
It is important to understand how the Graph works internally. Nodes are stored in a Map whose key is the value for
the node. If the graph is storing Strings then only a single node can exist with the value foo. This means
that the graph does not allow duplicates. Therefore it would be impossible to have two nodes whose values are
foo with different edges. The key of the Map is a HashGraph.HashNode object. The node stores the value as well
as all the edges.
Node values
Due to the implementation of the graph, all values must have a good equal and hashcode implementation. Using the object identity is allowed and will then manage the graph based on the heap location of the value objects (pointers are used for the java.lang.Object version of equals and hashcode).
Thread safety
The Graph is not thread safe. Classes must synchronize on the graph instance in order to protect multi-threaded use.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classThis class is the edge between nodes in the graph.protected static classThis class is a single node in the HashGraph.Nested classes/interfaces inherited from interface org.savantbuild.util.Graph
Graph.BasePath<T>, Graph.Edge<T,U>, Graph.EdgeFilter<T, U>, Graph.GraphConsumer<T, U>, Graph.GraphVisitor<T, U>, Graph.Path<T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an edge between the node whose value is the origin value given and the node whose value is the destination value given.protected HashGraph.HashNode<T,U> protected voidclearEdges(HashGraph.HashNode<T, U> node) booleanDetermines if the Graph contains the given value or not.booleanprotected TFinds the first node in the graph that satisfies the predicate using a depth first traversal of the graph.List<Graph.Edge<T,U>> getInboundEdges(T value) Returns a list of all the inbound edges for the node whose value is given.protected HashGraph.HashNode<T,U> List<Graph.Edge<T,U>> getOutboundEdges(T value) Returns a list of all the outbound edges for the node whose value is given.List<Graph.Path<T>>Determines the path from the given origin value to given destination value.inthashCode()voidRemoves all empty nodes from the graph except the given list of nodes.voidremoveEdge(T origin, T destination, U value) Removes the edge between the two nodes from the graph.voidremoveNode(T value) Removes the given value (node) from the graph and ensures that nothing is orphaned by the removal.intsize()protected voidtraverse(HashGraph.HashNode<T, U> root, HashGraph.HashEdge<T, U> traversedEdge, boolean visitNodesOnce, Set<T> cycleCheck, Set<T> visited, Graph.EdgeFilter<T, U> edgeFilter, Graph.GraphConsumer<T, U> consumer, int depth) voidtraverse(T rootValue, boolean visitNodesOnce, Graph.EdgeFilter<T, U> edgeFilter, Graph.GraphConsumer<T, U> consumer) Performs a depth first traversal of the graph.protected voidtraverseUp(HashGraph.HashNode<T, U> root, Set<T> visited, Graph.GraphVisitor<T, U> consumer, int depth) voidtraverseUp(T rootValue, Graph.GraphVisitor<T, U> visitor) Performs a depth first traversal of the graph.values()Returns a Set that contains all of the unique artifacts contained in the graph.
-
Constructor Details
-
HashGraph
public HashGraph()
-
-
Method Details
-
addEdge
Description copied from interface:GraphAdds an edge between the node whose value is the origin value given and the node whose value is the destination value given. This method works well for implementations that only allow values to exist once.If there are no nodes for the given value, this method should create nodes for each value and then create an edge between them. This reduces the work required to edge values.
-
contains
Description copied from interface:GraphDetermines if the Graph contains the given value or not. -
equals
-
find
Finds the first node in the graph that satisfies the predicate using a depth first traversal of the graph.- Specified by:
findin interfaceGraph<T,U> - Parameters:
rootValue- The value of the node to start the traversal from.predicate- The predicate used to find the node.- Returns:
- The value of the first node that matches the predicate starting at the rootValue node. Null if the rootValue not doesn't exist or if no nodes match the predicate.
- Throws:
CyclicException- If there is a cycle in the graph.
-
getInboundEdges
Description copied from interface:GraphReturns a list of all the inbound edges for the node whose value is given. This locates the first node with the value.- Specified by:
getInboundEdgesin interfaceGraph<T,U> - Parameters:
value- The value to find the edges for.- Returns:
- The edges or an empty list if the node exists and has no edges or null if the node does not exist.
-
getOutboundEdges
Description copied from interface:GraphReturns a list of all the outbound edges for the node whose value is given. This locates the first node with the value.- Specified by:
getOutboundEdgesin interfaceGraph<T,U> - Parameters:
value- The value to find the edges for.- Returns:
- The edges or an empty list if the node exists and has no edges or null if the node does not exist.
-
getPaths
Description copied from interface:GraphDetermines the path from the given origin value to given destination value. -
hashCode
public int hashCode() -
prune
Description copied from interface:GraphRemoves all empty nodes from the graph except the given list of nodes. -
removeEdge
Description copied from interface:GraphRemoves the edge between the two nodes from the graph.- Specified by:
removeEdgein interfaceGraph<T,U> - Parameters:
origin- The origin value.destination- The destination value.value- The edge value.
-
removeNode
Description copied from interface:GraphRemoves the given value (node) from the graph and ensures that nothing is orphaned by the removal.- Specified by:
removeNodein interfaceGraph<T,U> - Parameters:
value- The value to remove.- Throws:
CyclicException- If the graph has any cycles in it.
-
size
public int size() -
traverse
public void traverse(T rootValue, boolean visitNodesOnce, Graph.EdgeFilter<T, U> edgeFilter, Graph.GraphConsumer<T, throws CyclicExceptionU> consumer) Performs a depth first traversal of the graph. For each node, the GraphConsumer is called. The traversal WILL traverse the same node twice if it has multiple connections.- Specified by:
traversein interfaceGraph<T,U> - Parameters:
rootValue- The value of the node to start the traversal from.visitNodesOnce- Determines if nodes are visited once if they have multiple links.edgeFilter- The EdgeFilter that is used to control the traversal.consumer- The GraphConsumer that is called for each edge.- Throws:
CyclicException- If there is a cycle in the graph.
-
traverseUp
Performs a depth first traversal of the graph. For each node, the GraphConsumer is called. The traversal WILL traverse the same node twice if it has multiple connections.- Specified by:
traverseUpin interfaceGraph<T,U> - Parameters:
rootValue- The value of the node to start the traversal from.visitor- The GraphVisitor that is called for each edge.- Throws:
CyclicException- If there is a cycle in the graph.
-
values
Returns a Set that contains all of the unique artifacts contained in the graph. -
addNode
-
clearEdges
-
find
-
getNode
-
traverse
protected void traverse(HashGraph.HashNode<T, U> root, HashGraph.HashEdge<T, U> traversedEdge, boolean visitNodesOnce, Set<T> cycleCheck, Set<T> visited, Graph.EdgeFilter<T, U> edgeFilter, Graph.GraphConsumer<T, U> consumer, int depth) -
traverseUp
protected void traverseUp(HashGraph.HashNode<T, U> root, Set<T> visited, Graph.GraphVisitor<T, U> consumer, int depth)
-