Class GraphPath<N,A>
- Direct Known Subclasses:
CachedShortestPath,GeometricGraphPath
Graph path always starts and ends at a node. In a non-empty path, the number of arcs always equals the number of nodes minus one.
Path can be empty (e.g. to represent a non-existent path) or consist of only one node (e.g. path from some node to this node itself).
- Author:
- Andrey Malykhanov
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty graph path, i.e.Creates a graph path consisting of the specified sequence of graph arcs and corresponding graph nodes.Creates a graph path containing a single specified graph node.Creates a graph path that is a copy of other specified graph path.Creates a graph path consisting of the specified sequence of graph arcs and corresponding graph nodes. -
Method Summary
Modifier and TypeMethodDescriptionAdds the specified node to this path if this path is empty, otherwise throws aIllegalArgumentException.Appends the specified other path to this path.Appends the specified arc and its destination node to this path.booleancontainsArc(A value) booleancontainsNode(N value) Checks if the path contains the specified value in its nodes.static <N,A> GraphPath <N, A> Returns an instance of an empty path, i.e.booleangetArcs()Returns an unmodifiable list of arcs of this path.intReturns arcs count in this path.getCopy()Returns a copy of this path.Returns the first arc of this path, ornullif this path has no arcs.Returns the first node of this path.Returns the last arc of this path, ornullif this path has no arcs.Returns the last node of this path.getNextArc(Graph<N, A>.Node node) Returns the arc that follows the specified node in this path.Returns a path node containing the specified value.getNodes()Returns an unmodifiable list of nodes of this path.intReturns nodes count in this path.getPrevArc(Graph<N, A>.Node node) Returns the arc that precedes the specified node in this path.getSubPath(int fromNodeIndexInclusive, int toNodeIndexInclusive) Returns a sub-path of this path located between the nodes with the specified indices.getSubPathFrom(int fromNodeIndexInclusive) Returns a sub-path of this path located between the node with the specified index and the last node of this path.getSubPathTo(int toNodeIndexInclusive) Returns a sub-path of this path located between its first node and the node with the specified index.inthashCode()booleanisEmpty()Checks whether this path is empty, i.e.Prepends the specified other path to this path.prependArc(Graph<N, A>.Arc arc) Prepends the specified arc and its source node to this path.toString()
-
Constructor Details
-
GraphPath
public GraphPath()Creates an empty graph path, i.e. a graph path with no nodes and arcs. -
GraphPath
Creates a graph path containing a single specified graph node.- Parameters:
node- specified graph node
-
GraphPath
Creates a graph path consisting of the specified sequence of graph arcs and corresponding graph nodes.- Parameters:
arcs- sequence of graph arcs
-
GraphPath
Creates a graph path consisting of the specified sequence of graph arcs and corresponding graph nodes.- Parameters:
arcs- sequence of graph arcs
-
GraphPath
Creates a graph path that is a copy of other specified graph path.- Parameters:
path- other specified graph path
-
-
Method Details
-
addNode
Adds the specified node to this path if this path is empty, otherwise throws aIllegalArgumentException.- Parameters:
node- specified node- Returns:
- reference to this instance, for convenience of building new instances
-
prependArc
Prepends the specified arc and its source node to this path. Destination node of the specified arc must be the same as the first node of this path, otherwise aIllegalArgumentExceptionis thrown.If this arc is empty, simply adds the specified arc and both nodes to this path.
- Parameters:
arc- arc being prepended to this path- Returns:
- reference to this instance, for convenience of building new instances
-
appendArc
Appends the specified arc and its destination node to this path. Source node of the specified arc must be the same as the last node of this path, otherwise aIllegalArgumentExceptionis thrown.If this arc is empty, simply adds the specified arc and both nodes to this path.
- Parameters:
arc- arc being appended to this path- Returns:
- reference to this instance, for convenience of building new instances
-
getNodesCount
public int getNodesCount()Returns nodes count in this path.- Returns:
- Nodes count in this path
-
getArcsCount
public int getArcsCount()Returns arcs count in this path.- Returns:
- Arcs count in this path
-
containsArc
-
getArc
-
containsNode
Checks if the path contains the specified value in its nodes.- Parameters:
value- The specified value- Returns:
- True if the graph contains the specified value, false otherwise
-
getNode
Returns a path node containing the specified value.- Parameters:
value- The specified value- Returns:
- Node containing the specified value if such node exists,
nullotherwise
-
getFirstNode
Returns the first node of this path. If this path is empty, returnsnull.- Returns:
- The first node of this path, or
nullif this path is empty
-
getLastNode
Returns the last node of this path. If this path is empty, returnsnull.- Returns:
- The last node of this path, or
nullif this path is empty
-
getNodes
Returns an unmodifiable list of nodes of this path.- Returns:
- Unmodifiable list of nodes
-
getLastArc
Returns the last arc of this path, ornullif this path has no arcs.- Returns:
- last arc of this path, or
nullif this path has no arcs
-
getFirstArc
Returns the first arc of this path, ornullif this path has no arcs.- Returns:
- first arc of this path, or
nullif this path has no arcs
-
getArcs
Returns an unmodifiable list of arcs of this path.- Returns:
- Unmodifiable list of arcs
-
getNextArc
Returns the arc that follows the specified node in this path.Returns
nullin the following cases:- if this path does not contain the specified node
- if the specified node is the last node of this path
- Parameters:
node- specified node- Returns:
- he arc that follows the specified node, or
null
-
getPrevArc
Returns the arc that precedes the specified node in this path.Returns
nullin the following cases:- if this path does not contain the specified node
- if the specified node is the first node of this path
- Parameters:
node- specified node- Returns:
- he arc that precedes the specified node, or
null
-
append
Appends the specified other path to this path. This path gets mutated as result of call to this method.The first node of the specified other path must be the same as the last node of this path, otherwise a
IllegalArgumentExceptionis thrown.- Parameters:
otherPath- other path being appended to this one- Returns:
- reference to this instance, for convenience of building new instances
-
prepend
Prepends the specified other path to this path. This path gets mutated as result of call to this method.The last node of the specified other path must be the same as the first node of this path, otherwise a
IllegalArgumentExceptionis thrown.- Parameters:
otherPath- other path being prepended to this one- Returns:
- reference to this instance, for convenience of building new instances
-
getCopy
Returns a copy of this path.- Returns:
- copy of this path
-
getSubPath
Returns a sub-path of this path located between the nodes with the specified indices.The specified indices must be non-negative and less than nodes count in this path (i.e. the result of
getNodesCount()method). fromNodeIndexInclusive must be less than or equal to toNodeIndexInclusive. If any of the constraints above are violated, aIllegalArgumentExceptionis thrown.If fromNodeIndexInclusive = toNodeIndexInclusive, a path containing only one node is returned.
- Parameters:
fromNodeIndexInclusive- index of the first node of the sub-pathtoNodeIndexInclusive- index of the last node of the sub-path- Returns:
- sub-path of this path located between the nodes with the specified indices
-
getSubPathTo
Returns a sub-path of this path located between its first node and the node with the specified index.The specified index must be non-negative and less than nodes count in this path (i.e. the result of
getNodesCount()method). If this constraint is violated, aRuntimeExceptionis thrown.- Parameters:
toNodeIndexInclusive- index of the last node of the sub-path- Returns:
- sub-path of this path located between its first node and the node with the specified index
-
getSubPathFrom
Returns a sub-path of this path located between the node with the specified index and the last node of this path.The specified index must be non-negative and less than nodes count in this path (i.e. the result of
getNodesCount()method). If this constraint is violated, aRuntimeExceptionis thrown.- Parameters:
fromNodeIndexInclusive- index of the first node of the sub-path- Returns:
- sub-path of this path located between the node with the specified index and the last node of this path
-
isEmpty
public boolean isEmpty()Checks whether this path is empty, i.e. does not contain any nodes or arcs. Note that path with a single node is not empty.- Returns:
trueis this path is empty,falseotherwise
-
emptyPath
Returns an instance of an empty path, i.e. path with no arcs and nodes.- Type Parameters:
N- type of graph node valuesA- type of graph arc values- Returns:
- instance of an empty path, i.e. path with no arcs and nodes
-
toString
-
hashCode
public int hashCode() -
equals
-