root/trunk/Node.java

Revision 138, 2.2 kB (checked in by tfelker2, 3 years ago)

Now you can name nodes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 // Node.java
2 //
3 // stores all the information about a node that the searching algorithm
4 // will know about.
5
6 import java.io.*;
7 import java.util.*;
8
9 public abstract class Node implements Serializable {
10         static final long serialVersionUID = 3897565286366276995L;
11         Node(int id) {
12                 this.id = id;
13         }
14        
15         Node(int id, String name, String description) {
16                 this.id = id;
17                 this.name = name;
18                 this.description = description;
19         }
20         // returns a new (modifiable) cost smaller than the actual cost required
21         //      to reach the destination.
22         // called by a search algorithm, or possibly by edges (as part of a search)
23         // this must be overridden by all subclasses
24         public abstract Cost computeHCost(Search s, Node previousNode);
25
26         // note - eventually there will be methods to handle naming of nodes
27         // it was not necessary in this demo, however.
28
29         public boolean equals(Object o) {
30                 return (o instanceof Node) && (((Node)o).id == id);
31         }
32
33         public int hashCode() {
34                 return id;
35         }
36
37         // for debug purposes
38         public String toString() {
39                 return "Node\n" + infoToString();
40         }
41
42         public String infoToString() {
43                 return "\tid=" + id + "\n\tname=" + name + "\n\tdescription=" + description + "\n";     
44         }
45        
46         private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
47                 in.defaultReadObject();
48                 edges = new LinkedList();
49         }
50
51         public void setDescription(String desc) { description = desc; }
52         public String getDescription() { return description; }
53
54         public String getName() { return name; }
55         // changing the name of a node will break lookup by name in the graph
56         // that contains the node, however, this breakage won't be persistant
57         // across saves, so it's not a problem.
58         public void setName(String s) {
59                 name = s;
60         }
61        
62         // returns whether the node should be displayed with the given map
63         public boolean isOnMap(BackgroundMap m) {
64                 return true;
65         }
66
67         // this must be unique within a graph for serialization to work
68         public final int id;
69
70
71         // this is encouraged to be unique per graph, so lookup by name works
72         private String name = "";
73        
74         // this need not be unique, or in any specific format, and is designed
75         // for human consumption.
76         private String description = "";
77        
78
79         //TODO:  make get/set functions for this
80         public transient Collection edges = new LinkedList();
81 }
Note: See TracBrowser for help on using the browser.