root/trunk/Cost.java

Revision 3, 2.6 kB (checked in by dpaola2, 1 month ago)

old pathways project added

Line 
1 // Cost - copyright Tom Felker <tfelker2@uiuc.edu>, December 2004
2 //
3 // This represents the badness of getting from one node to another.  It's a
4 // class because later it may wrap more than an number - we may implement
5 // uncertainty, probability, walking distance vs. bus distance being different,
6 // etc...
7
8 // NOTE: this can be modified with add(), so you must be careful not to modify
9 // Costs that are members of other objects.  The convention followed will be,
10 // if the object is returning a reference to a Cost you shouldn't modify, the
11 // method will be called getFoo(), if it's returning a newly-created Cost that
12 // you can modify, it will be called computeFoo().  Why, oh why can't Java
13 // implement C++'s const keyword?  I sometimes want to return a const Cost&.
14
15 import java.text.*;
16
17 public class Cost implements Comparable {
18
19         public int compareTo(Object other) {
20
21 // this println is for testing purposes to see how many comparisons are made
22 // durnig various stages of the main loop, it should be commented out later.
23 //System.out.println("\t(" + toString() + ").compareTo(" + ((Cost)other).toString() + ")");
24
25 // this is for performance measurement reasons
26 ++comparisonCounter;
27
28                 if(time < ((Cost)other).time) return -1;
29                 else if(time == ((Cost)other).time) return 0;
30                 return 1;
31         }
32
33         public Cost() {
34                 time = 0;
35         }
36
37         public Cost(double time) {
38                 this.time = time;
39         }
40
41         // or i could figure out how Cloneable works, but this is quicker
42         // make sure this method copies all member variables
43         public Cost(Cost cost) {
44                 time = cost.time;
45         }
46
47         // increment myself by other
48         public void add(Cost other) {
49                 // this can be left out for performance, but it is important that nothing changes zero
50                 if(this == ZERO) throw new IllegalArgumentException("Quit trying to redefine 0!");
51                 time += other.time;
52         }
53
54         // returns a new cost which is the sum of a and b
55         public static Cost add(Cost a, Cost b) {
56                 Cost ret = new Cost();
57                 ret.time = a.time + b.time;
58                 return ret;
59         }
60
61         public double getTime() {
62                 return time;
63         }
64
65         public String toString() { return twoPlaces.format(time); }
66
67         // how long it took, in seconds
68         private double time;
69
70         //this is kinda a hack. Have to know which phyiscal node got onto bus
71         //this is only used by BusNode!!!
72         public Node physicalNodeGotOntoBus;
73
74         // this is a constant cost, so that when you need a cost to be zero,
75         // you needn't create an object.  Changing it should raise an exception,
76         // so don't try.
77         public static final Cost ZERO = new Cost(0);
78
79         // so we can print without being ugly
80         private static final DecimalFormat twoPlaces = new DecimalFormat("0.##");
81
82         // for performance benchmarking
83         public static int comparisonCounter;
84 }
Note: See TracBrowser for help on using the browser.