/Users/lyon/j4p/src/collections/map/StarInfo.java

1    /* 
2     * StatInfo.java 
3     * 
4     * Created on December 4 
5     */ 
6     
7    package collections.map; 
8     
9    import java.util.Comparator; 
10    
11   /** 
12    * StarInfo class containing two explicit comparators. 
13    * @author  Thomas Rowland 
14    */ 
15   public class StarInfo { 
16    
17       private String name; 
18       private double distance; 
19    
20       public static final Comparator 
21               NAME_COMPARATOR = new NameComparator(); 
22    
23       public static final Comparator 
24               DIST_COMPARATOR = new DistanceComparator(); 
25    
26    
27       StarInfo(String _name, double _dist) { 
28           name = _name; 
29           distance = _dist; 
30       } 
31    
32       /* equality based on name field */ 
33       public boolean equals(Object o) { 
34           if (o.getClass() != this.getClass()) 
35               return false; 
36           return ((StarInfo) o).getName().equals(name); 
37       } 
38    
39       /* hash code based on name field */ 
40       public int hashCode() { 
41           return name.hashCode(); 
42       } 
43    
44    
45       public String getName() { 
46           return name; 
47       } 
48    
49       public double getDistance() { 
50           return distance; 
51       } 
52    
53       /* Name Comparator */ 
54       private static class NameComparator 
55               implements Comparator { 
56           public int compare(Object o1, Object o2) { 
57               return ((StarInfo) o1).getName() 
58                       .compareTo( 
59                               ((StarInfo) o2).getName()); 
60           } 
61       } 
62    
63       /* Distance Comparator */ 
64       private static class DistanceComparator 
65               implements Comparator { 
66           public int compare(Object o1, Object o2) { 
67               return (new Double(((StarInfo) o1) 
68                       .getDistance())) 
69                       .compareTo( 
70                               new Double(((StarInfo) o2) 
71                       .getDistance())); 
72           } 
73       } 
74    
75   } 
76