Added somewhat changed Graph implementation, and Junit tests for it. Changed ant...
[ailab2.git] / src / GraphNode.java
blobd1d754bf003c64cdb9c04ae78c809011830003e4
1 /*
2 * @(#)GraphNode.java
3 * Time-stamp: "2008-10-13 00:35:46 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 int ip;
21 private boolean visited;
22 private int distance;
23 private GraphNode parent;
25 /**
26 * Creates a new GraphNode instance.
28 * @param ip The IP address of the node.
29 * @param maxNeighbours The maximum amount of
30 * neighbours the node can have.
32 public GraphNode(int ip, int maxNeighbours) {
33 this.visited = false;
34 this.ip = ip;
35 this.maxNeighbours = maxNeighbours;
36 neighbours = new Hashtable(maxNeighbours);
39 /**
40 * Inspects all the neighbours (nodes) of this node.
42 * @return A Vector with all neighbours.
44 public Enumeration getNeighbours() {
45 return neighbours.keys();
48 /**
49 * Inspects the IP address of this node.
51 * @return The IP address of this node.
53 public int getIp() {
54 return this.ip;
57 /**
58 * Adds a neighbour to this node.
60 * @param node The neighbour node to be added.
61 * @param weight The weight of the potential edge between
62 * this node and the new neighbour.
64 public void addNeighbour(GraphNode node, Integer weight) {
65 neighbours.put(node, /*(Integer)*/ weight);
68 public void deleteNeighbour(GraphNode node) {
69 neighbours.remove(node);
72 /**
73 * Inspects the weight of the potential edge between this
74 * node and one of its neighbours.
76 * @param node The neighbour of this node.
77 * @return The weight of the potential edge.
79 public int getWeight(GraphNode node) {
80 return (Integer) neighbours.get(node);
83 /**
84 * Visists this node--sets its visited attribute to true.
86 public void visit() {
87 this.visited = true;
90 /**
91 * Inspects whether this node has been visited or not.
93 * @return true if this node has been visited, else false.
95 public boolean isVisited() {
96 return this.visited;
99 /**
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(int 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 int 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() {
133 return this.parent;
137 * Compares this node to another graph node.
139 * @param node The node to compare this node to.
140 * @return 0 if the addresses match, a positive integer if
141 * this node has a higher IP address than the other node,
142 * and a negative integer if this node has a lower IP address
143 * than the other node.
145 public int compareTo(Object node) {
146 return (this.ip - ((GraphNode) node).getIp());