1 import java
.util
.Vector
;
2 import java
.util
.Hashtable
;
3 import java
.util
.Enumeration
;
6 * GraphNode, used by Graph to keep track of the nodes
7 * in a network and to make a routing table.
9 * @author "Anton Johansson" <anton.johansson@gmail.com>
10 * @author "Victor Zamanian-Abasy" <zamanian87@gmail.com>
12 public class GraphNode
implements Comparable
{
13 private int maxNeighbours
;
14 private Hashtable neighbours
;
18 private Double distanceToGoal
;
19 private boolean visited
;
20 private Double distance
;
21 private GraphNode parent
;
24 * Creates a new GraphNode instance.
26 * @param name The name of the node.
27 * @param maxNeighbours The maximum amount of
28 * neighbours the node can have.
30 public GraphNode(String name
, int x
, int y
, int maxNeighbours
) {
35 this.maxNeighbours
= maxNeighbours
;
36 neighbours
= new Hashtable(maxNeighbours
);
40 * Adds a neighbour to this node.
42 * @param node The neighbour node to be added.
43 * @param weight The weight of the potential edge between
44 * this node and the new neighbour.
46 public void addNeighbour(GraphNode node
, Double weight
) {
47 neighbours
.put(node
, /*(Integer)*/ weight
);
50 public void deleteNeighbour(GraphNode node
) {
51 neighbours
.remove(node
);
55 * Inspects the weight of the potential edge between this
56 * node and one of its neighbours.
58 * @param node The neighbour of this node.
59 * @return The weight of the potential edge.
61 public Double
getWeight(GraphNode node
) {
62 return (Double
) neighbours
.get(node
);
66 * Visists this node--sets its visited attribute to true.
73 * Inspects whether this node has been visited or not.
75 * @return true if this node has been visited, else false.
77 public boolean isVisited() {
82 * Inspects all the neighbours (nodes) of this node.
84 * @return A Vector with all neighbours.
86 public Enumeration
getNeighbours() {
87 return neighbours
.keys();
91 * Inspects the NAME address of this node.
93 * @return The NAME address of this node.
95 public String
getName() {
100 * Sets the distance to this node from the root node in
101 * the process of Dijkstras shortest-path algorithm.
103 * @param distance The distance to this node.
105 public void setDistance(Double distance
) {
106 this.distance
= distance
;
110 * Inspects the distance from this node to the root node. See "setDistance."
112 * @return The distance from this node to the root node.
114 public Double
getDistance() {
115 return this.distance
;
119 * Sets this node's parent node. Used in Dijkstras algorithm.
121 * @param parent The parent to be set.
123 public void setParent(GraphNode parent
) {
124 this.parent
= parent
;
128 * Inspects this node's parent.
130 * @return The parent node of this parent.
132 public GraphNode
getParent() {
137 * Access to this nodes x-coordinate.
139 * @return the x-coordinate of this node.
146 * Access to this nodes y-coordinate.
148 * @return the y-coordinate of this node.
155 * Calculate and return the distance to x- y-coordinates
157 * @param x the x value to calculate the distance to.
158 * @param y the y value to calculate the distance to.
160 public void setDistanceToGoal(GraphNode goal
) {
161 this.distanceToGoal
= Math
.hypot((this.x
- goal
.getX()), (this.y
- goal
.getY()));
165 * Retun the distance to Goal.
167 * @return the distance to goal.
169 public Double
getDistanceToGoal() {
170 return this.distanceToGoal
;
174 * Compares this node to another graph node.
176 * @param node The node to compare this node to.
177 * @return 0 if the addresses match, a positive integer if
178 * this node has a higher NAME address than the other node,
179 * and a negative integer if this node has a lower NAME address
180 * than the other node.
182 public int compareTo(Object node
) {
183 return distance
.compareTo(((GraphNode
) node
).getDistance());
186 public String
toString() {
187 String returnString
= name
+ "\n";
188 for (Enumeration e
= getNeighbours(); e
.hasMoreElements();) {
189 GraphNode neighbour
= (GraphNode
) e
.nextElement();
190 returnString
+= " " + neighbour
.getName() + ", traveltime: " + this.getWeight(neighbour
) +"\n";