Changed Graph implementation, renamed some methods
[ailab2.git] / src / GraphNode.java
blob66962f718bec8cc83f3aec83464c1777689fc4d3
1 /*
2 * @(#)GraphNode.java
3 * Time-stamp: "2008-10-13 10:56:11 anton"
4 */
6 import java.util.Vector;
7 import java.util.Hashtable;
8 import java.util.Enumeration;
10 /**
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;
20 private String name;
21 private int x;
22 private int y;
23 private boolean visited;
24 private int distance;
25 private GraphNode parent;
27 /**
28 * Creates a new GraphNode instance.
30 * @param name The name of the node.
31 * @param maxNeighbours The maximum amount of
32 * neighbours the node can have.
34 public GraphNode(String name, int x, int y, int maxNeighbours) {
35 this.name = name;
36 this.x = x;
37 this.y = y;
38 this.visited = false;
39 this.maxNeighbours = maxNeighbours;
40 neighbours = new Hashtable(maxNeighbours);
43 /**
44 * Adds a neighbour to this node.
46 * @param node The neighbour node to be added.
47 * @param weight The weight of the potential edge between
48 * this node and the new neighbour.
50 public void addNeighbour(GraphNode node, Integer weight) {
51 neighbours.put(node, /*(Integer)*/ weight);
54 public void deleteNeighbour(GraphNode node) {
55 neighbours.remove(node);
58 /**
59 * Inspects the weight of the potential edge between this
60 * node and one of its neighbours.
62 * @param node The neighbour of this node.
63 * @return The weight of the potential edge.
65 public int getWeight(GraphNode node) {
66 return (Integer) neighbours.get(node);
69 /**
70 * Visists this node--sets its visited attribute to true.
72 public void visit() {
73 this.visited = true;
76 /**
77 * Inspects whether this node has been visited or not.
79 * @return true if this node has been visited, else false.
81 public boolean isVisited() {
82 return this.visited;
85 /**
86 * Inspects all the neighbours (nodes) of this node.
88 * @return A Vector with all neighbours.
90 public Enumeration getNeighbours() {
91 return neighbours.keys();
94 /**
95 * Inspects the NAME address of this node.
97 * @return The NAME address of this node.
99 public String getName() {
100 return this.name;
104 * Sets the distance to this node from the root node in
105 * the process of Dijkstras shortest-path algorithm.
107 * @param distance The distance to this node.
109 public void setDistance(int distance) {
110 this.distance = distance;
114 * Inspects the distance from this node to the root node. See "setDistance."
116 * @return The distance from this node to the root node.
118 public int getDistance() {
119 return this.distance;
123 * Sets this node's parent node. Used in Dijkstras algorithm.
125 * @param parent The parent to be set.
127 public void setParent(GraphNode parent) {
128 this.parent = parent;
132 * Inspects this node's parent.
134 * @return The parent node of this parent.
136 public GraphNode getParent() {
137 return this.parent;
141 * @FIX
142 * Compares this node to another graph node.
144 * @param node The node to compare this node to.
145 * @return 0 if the addresses match, a positive integer if
146 * this node has a higher NAME address than the other node,
147 * and a negative integer if this node has a lower NAME address
148 * than the other node.
150 public int compareTo(Object node) {
151 return name.compareTo(((GraphNode) node).getName());