001 package ps3.graph;
002
003 /**
004 * A WeightedNode class is a simple record type which contains a name
005 * and a cost
006 **/
007
008 public class WeightedNode implements Comparable<WeightedNode> {
009 public final String name;
010 public final int cost;
011
012 /**
013 * Constructs a new WeightedNode.
014 *
015 * @effects constructs a new WeightedNode with the name
016 * <code>name</code> and the cost <code>cost</code>.
017 *
018 **/
019 public WeightedNode(String name, int cost) {
020 this.name = name;
021 this.cost = cost;
022 }
023
024 /**
025 * @return this.name
026 **/
027 public String name() {
028 return name;
029 }
030
031 /**
032 * @return this.cost
033 **/
034 public int cost() {
035 return cost;
036 }
037
038 public boolean equals(Object o) {
039 if (o instanceof WeightedNode) {
040 WeightedNode other = (WeightedNode) o;
041 return this.name.equals(other.name) &&
042 (this.cost == other.cost);
043 }
044 return false;
045 }
046
047 // Specified by the Object superclass
048 public int hashCode() {
049 return name.hashCode();
050 }
051
052 // Specified by the Object superclass
053 public String toString() {
054 return "[" + name.toString() + ": " + cost + "]";
055 }
056
057 /**
058 * WeightedNodes are ordered lexicographically by their name. When
059 * two nodes share a name, their ordering is determined by the
060 * numeric ordering of their costs.
061 **/
062 public int compareTo(WeightedNode o) {
063 int c = name.compareTo(o.name());
064 if (c == 0) {
065 return cost - o.cost;
066 } else {
067 return c;
068 }
069 }
070
071 }