root/trunk/Edge.java

Revision 3, 3.4 kB (checked in by dpaola2, 1 week ago)

old pathways project added

Line 
1 // Edge.java
2 //
3 // This stores all the information a search algorithm will use to manipulate
4 // edges.  it has no position information.
5
6 import java.io.*;
7
8 public abstract class Edge implements Serializable {
9         static final long serialVersionUID = 2843536174507280704L;
10        
11         public Edge(int a, int b) {
12                 aID = a;
13                 bID = b;
14         }
15
16         // return the Node with id id
17         public Node getNode(int id) {
18                 if(aID == id) return a;
19                 else if(bID == id) return b;
20                 throw new IllegalArgumentException(this + " doesn't have node " + id);
21         }
22
23
24         // return the other endpoint of the edge
25         public int getOtherID(Node n) {
26                 if(a == n) return bID;
27                 else if(b == n) return aID;
28                 throw new IllegalArgumentException(this + " doesn't have node " + n.id);
29         }
30
31         // return the other endpoint of the edge
32         public int getOtherID(int id) {
33                 if(aID == id) return bID;
34                 else if(bID == id) return aID;
35                 throw new IllegalArgumentException(this + " doesn't have node " + id);
36         }
37
38         // return the Node opposite the one with id n
39         public Node getOtherNode(int id) {
40                 if(aID == id) return b;
41                 else if(bID == id) return a;
42                 throw new IllegalArgumentException(this + " doesn't have node " + id);
43         }
44
45         // return the Node opposite n
46         public Node getOtherNode(Node n) {
47                 if(a == n) return b;
48                 else if(b == n) return a;
49                 throw new IllegalArgumentException(this + " doesn't have node " + n.id);
50         }
51
52         // do we have this node?
53         public boolean hasNode(Node n) { return a == n || b == n; }
54         public boolean hasNode(int id) { return aID == id || bID == id; }
55
56
57         // get and set the nodes referenced, ensuring the ids match
58         public Node getNodeA() { return a; }
59         public Node getNodeB() { return b; }
60         public void setNodeA(Node a) {
61                 if(a.id != aID) throw new IllegalArgumentException("a.id != this.aID");
62                 this.a = a;
63         }
64         public void setNodeB(Node b) {
65                 if(b.id != bID) throw new IllegalArgumentException("b.id != this.bID");
66                 this.b = b;
67         }
68
69         public void setNode(Node n) {
70                 if(n.id == aID) a = n;
71                 else if(n.id == bID) b = n;
72                 else throw new IllegalArgumentException(this + " doesn't have node " + n.id);
73         }
74
75         // these let you use 0 or 1 instead of a or b
76         public Node getNodeN(int n) {
77                 return n == 0 ? a : b;
78         }
79         public int getNID(int n) {
80                 return n == 0 ? aID : bID;
81         }
82         public void setNodeN(int n, Node node) {
83                 if(n == 0) setNodeA(node); else setNodeB(node);
84         }
85
86         // any two edges that connect the same nodes, even in the reverse order,
87         // are equal.  Note that this is used mainly for generating random
88         // graphs, so it's ok if subclasses don't get this exactly right.
89         public boolean equals(Object o) {
90                 return (o instanceof Edge) && (
91                        (aID == ((Edge)o).aID && bID == ((Edge)o).bID) ||
92                        (aID == ((Edge)o).bID && bID == ((Edge)o).aID)
93                        );
94         }
95
96         // since we changed equals, we also have to change this so hashsets work
97         public int hashCode() {
98                 return aID * bID;
99         }
100
101         // returns a new (modifiable) cost to get from from to the other node
102         // this should not add gCostOfFrom to the cost it creates. (it just may
103         // need the gCostOfFrom to compute bus schedules, etc.)
104         // all subclasses must override this
105         public abstract Cost computeDeltaGCost(Node from, Cost gCostOfFrom, Search s);
106        
107         // returns whether the node should be displayed with the given map
108         public boolean isOnMap(BackgroundMap m) {
109                 return true;
110         }
111        
112         public String toString() {
113                 return "Edge(" + aID + ", " + bID + ")";
114         }
115
116         public final int aID, bID;
117
118         private transient Node a, b;
119 }
Note: See TracBrowser for help on using the browser.