1 import java
.util
.Hashtable
;
2 import java
.util
.Enumeration
;
5 * GraphNode, used by Graph to keep track of the nodes
6 * in a network and to make a routing table.
8 * @author "Anton Johansson" <anton.johansson@gmail.com>
9 * @author "Victor Zamanian" <victor.zamanian@gmail.com>
12 public class GraphNode
implements Comparable
{
13 private Hashtable
<GraphNode
, Object
> neighbours
;
17 private boolean visited
;
18 private Double distanceTraveled
;
19 private GraphNode parent
;
20 private Double evalFuncVal
;
23 * Creates a new GraphNode instance.
25 * @param name The name of the node.
26 * @param x the x-coordinate of this node.
27 * @param y the y-coordinate of this node.
28 * @param maxNeighbours The maximum amount of
29 * neighbours the node can have.
31 public GraphNode(String name
, int x
, int y
, int maxNeighbours
) {
36 this.evalFuncVal
= Double
.MIN_VALUE
;
37 neighbours
= new Hashtable
<GraphNode
, Object
>(maxNeighbours
);
41 * Adds a neighbour to this node.
43 * @param node The neighbour node to be added.
44 * @param edge The edge between this node and the new neighbour.
46 public void addNeighbour(GraphNode node
, Object edge
) {
47 neighbours
.put(node
, edge
);
51 * Removes the specified neighbour of this node.
53 * @param node The neighbour to remove.
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 Object
getEdge(GraphNode node
) {
67 return 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 An Enumeration of all neighbours of this node..
91 public Enumeration
<GraphNode
> getNeighbours() {
92 return neighbours
.keys();
96 * Inspects the name of this node.
98 * @return The name of this node.
100 public String
getName() {
105 * Sets the distance to this node from the root node. This is
106 * usefull in algorithms.
108 * @param distanceTraveled The distanceTraveled by this node.
110 public void setDistanceTraveled(Double distanceTraveled
) {
111 this.distanceTraveled
= distanceTraveled
;
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 Double
getDistanceTraveled() {
120 return this.distanceTraveled
;
124 * Sets this node's parent node. Used in algorithms.
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 of the
161 * node provided as a parameter.
163 * @param node the Node to calculate distance to.
164 * @return the distance to node.
166 public Double
getDistanceToNode(GraphNode node
) {
167 return Math
.hypot((this.x
- node
.getX()),
168 (this.y
- node
.getY()));
172 * Sets the value evalFuncVal, this is used in search algorithms.
174 * @param evalFuncVal the evalFuncVal value.
176 public void setEvalFuncVal(Double evalFuncVal
) {
177 this.evalFuncVal
= evalFuncVal
;
181 * Returns the value evalFuncVal, this is used in search
184 * @return The evalFuncVal value.
186 public Double
getEvalFuncVal() {
187 return this.evalFuncVal
;
191 * Compares this node to another graph node. The comparison uses
192 * the nodes evalFuncVal values.
194 * @param node The node to compare this node to.
195 * @return 0 if the addresses match, a positive integer if
196 * this node has a higher NAME address than the other node,
197 * and a negative integer if this node has a lower NAME address
198 * than the other node.
200 public int compareTo(Object node
) {
201 return this.evalFuncVal
.compareTo(((GraphNode
) node
).getEvalFuncVal());
205 * Returns a String representation of this GraphNode.
207 * @return a a String representation of this GraphNode.
209 public String
toString() {
210 String returnString
= name
+ "\n";
211 for (Enumeration
<GraphNode
> e
= getNeighbours(); e
.hasMoreElements();) {
212 GraphNode neighbour
= (GraphNode
) e
.nextElement();
213 returnString
+= " " + neighbour
.getName()
214 + ", traveltime: " + this.getEdge(neighbour
) +"\n";