Merge branch anton with master for v2.0.
[ailab2.git] / src / GraphNode.java
blob0a59c4f4805d77246184df0817558f4ae06239b8
1 import java.util.Hashtable;
2 import java.util.Enumeration;
4 /**
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>
10 * @version 1.0
12 public class GraphNode implements Comparable {
13 private Hashtable<GraphNode, Object> neighbours;
14 private String name;
15 private int x;
16 private int y;
17 private boolean visited;
18 private Double distanceTraveled;
19 private GraphNode parent;
20 private Double evalFuncVal;
22 /**
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) {
32 this.name = name;
33 this.x = x;
34 this.y = y;
35 this.visited = false;
36 this.evalFuncVal = Double.MIN_VALUE;
37 neighbours = new Hashtable<GraphNode, Object>(maxNeighbours);
40 /**
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);
50 /**
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);
59 /**
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);
70 /**
71 * Visists this node--sets its visited attribute to true.
73 public void visit() {
74 this.visited = true;
77 /**
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() {
83 return this.visited;
86 /**
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();
95 /**
96 * Inspects the name of this node.
98 * @return The name of this node.
100 public String getName() {
101 return this.name;
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() {
138 return this.parent;
142 * Access to this nodes x-coordinate.
144 * @return the x-coordinate of this node.
146 public int getX() {
147 return this.x;
151 * Access to this nodes y-coordinate.
153 * @return the y-coordinate of this node.
155 public int getY() {
156 return this.y;
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
182 * algorithms.
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";
216 return returnString;