3 * Time-stamp: "2008-10-13 16:55:16 anton"
6 import java
.util
.Vector
;
7 import java
.util
.Hashtable
;
8 import java
.util
.Enumeration
;
11 * GraphNode, used by Graph to keep track of the nodes
12 * in a network and to make a routing table.
14 * @author "Anton Johansson" <anton.johansson@gmail.com>
15 * @author "Victor Zamanian-Abasy" <zamanian87@gmail.com>
17 public class GraphNode
implements Comparable
{
18 private int maxNeighbours
;
19 private Hashtable neighbours
;
23 private double distanceToGoal
;
24 private boolean visited
;
26 private GraphNode parent
;
29 * Creates a new GraphNode instance.
31 * @param name The name of the node.
32 * @param maxNeighbours The maximum amount of
33 * neighbours the node can have.
35 public GraphNode(String name
, int x
, int y
, int maxNeighbours
) {
40 this.maxNeighbours
= maxNeighbours
;
41 neighbours
= new Hashtable(maxNeighbours
);
45 * Adds a neighbour to this node.
47 * @param node The neighbour node to be added.
48 * @param weight The weight of the potential edge between
49 * this node and the new neighbour.
51 public void addNeighbour(GraphNode node
, Integer weight
) {
52 neighbours
.put(node
, /*(Integer)*/ weight
);
55 public void deleteNeighbour(GraphNode node
) {
56 neighbours
.remove(node
);
60 * Inspects the weight of the potential edge between this
61 * node and one of its neighbours.
63 * @param node The neighbour of this node.
64 * @return The weight of the potential edge.
66 public int getWeight(GraphNode node
) {
67 return (Integer
) neighbours
.get(node
);
71 * Visists this node--sets its visited attribute to true.
78 * Inspects whether this node has been visited or not.
80 * @return true if this node has been visited, else false.
82 public boolean isVisited() {
87 * Inspects all the neighbours (nodes) of this node.
89 * @return A Vector with all neighbours.
91 public Enumeration
getNeighbours() {
92 return neighbours
.keys();
96 * Inspects the NAME address of this node.
98 * @return The NAME address of this node.
100 public String
getName() {
105 * Sets the distance to this node from the root node in
106 * the process of Dijkstras shortest-path algorithm.
108 * @param distance The distance to this node.
110 public void setDistance(int distance
) {
111 this.distance
= distance
;
115 * Inspects the distance from this node to the root node. See "setDistance."
117 * @return The distance from this node to the root node.
119 public int getDistance() {
120 return this.distance
;
124 * Sets this node's parent node. Used in Dijkstras algorithm.
126 * @param parent The parent to be set.
128 public void setParent(GraphNode parent
) {
129 this.parent
= parent
;
133 * Inspects this node's parent.
135 * @return The parent node of this parent.
137 public GraphNode
getParent() {
142 * Access to this nodes x-coordinate.
144 * @return the x-coordinate of this node.
151 * Access to this nodes y-coordinate.
153 * @return the y-coordinate of this node.
160 * Calculate and return the distance to x- y-coordinates
162 * @param x the x value to calculate the distance to.
163 * @param y the y value to calculate the distance to.
165 public void setDistanceToGoal(GraphNode goal
) {
166 this.distanceToGoal
= Math
.hypot((this.x
- goal
.getX()), (this.y
- goal
.getY()));
170 * Retun the distance to Goal.
172 * @return the distance to goal.
174 public double getDistanceToGoal() {
175 return this.distanceToGoal
;
179 * Compares this node to another graph node.
181 * @param node The node to compare this node to.
182 * @return 0 if the addresses match, a positive integer if
183 * this node has a higher NAME address than the other node,
184 * and a negative integer if this node has a lower NAME address
185 * than the other node.
187 public int compareTo(Object node
) {
188 return name
.compareTo(((GraphNode
) node
).getName());