3 * Time-stamp: "2008-10-13 00:35:46 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
;
21 private boolean visited
;
23 private GraphNode parent
;
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
) {
35 this.maxNeighbours
= maxNeighbours
;
36 neighbours
= new Hashtable(maxNeighbours
);
40 * Inspects all the neighbours (nodes) of this node.
42 * @return A Vector with all neighbours.
44 public Enumeration
getNeighbours() {
45 return neighbours
.keys();
49 * Inspects the IP address of this node.
51 * @return The IP address of this node.
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
);
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
);
84 * Visists this node--sets its visited attribute to true.
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() {
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() {
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());