/Users/lyon/j4p/src/collections/sortable/SortableProduct.java

1    /* 
2     * SortableProduct.java 
3     * Created on December 3, 2002 
4     */ 
5     
6    package collections.sortable; 
7     
8    import java.util.Comparator; 
9     
10   /** 
11    * Demonstrates how to make a class sortable, by implementing 
12    * the Comparable interface to make the class comparable, and by 
13    * using the Comparator interface to create explicit comparators. 
14    * @author  Thomas Rowland 
15    */ 
16   public class SortableProduct implements Comparable { 
17       //explicit comparators 
18       public static final Comparator PRICE_COMPARATOR = new PriceComparator(); 
19       public static final Comparator QTY_COMPARATOR = new QtyComparator(); 
20    
21       private int id;     // unique 
22       private String name; 
23       private String descr; 
24       private int qty; 
25       private double priceEa; 
26       private double priceNet; 
27    
28       public SortableProduct(int _id, String _name, 
29                              String _descr, int _qty, double _price) { 
30           id = _id; 
31           name = _name; 
32           descr = _descr; 
33           qty = _qty; 
34           priceEa = _price; 
35           priceNet = qty * priceEa; 
36       } 
37    
38       public int hashCode() { 
39           return id; 
40       } 
41    
42       public boolean equals(Object o) { 
43           if (o != null && o.getClass().equals(this.getClass())) 
44               return (id == ((SortableProduct) o).getId()); 
45           return false; 
46       } 
47    
48       /** 
49        * Overridden method in the Comparator interface 
50        * makes the class comparable. Defines the Natural Ordering 
51        * and must be consistent with equals. 
52        */ 
53       public int compareTo(Object o) { 
54           // throws a ClassCastException if not a SortableProduct object 
55           SortableProduct p = (SortableProduct) o; 
56           return new Integer(id).compareTo(new Integer(p.getId())); 
57       } 
58    
59       public boolean isGreater(Object o) { 
60           SortableProduct p = (SortableProduct) o; 
61           return id > p.getId(); 
62       } 
63    
64       public String toString() { 
65           return "\nid:" + id + 
66                   "\nname=" + name + 
67                   "\ndescr=" + descr + 
68                   "\nqty=" + qty + 
69                   "\npriceEach=" + priceEa + 
70                   "\npriceNet=" + priceNet; 
71       } 
72    
73       /** 
74        * Explicit comparator object to sort by price. 
75        */ 
76       private static class PriceComparator implements Comparator { 
77           public int compare(Object o1, Object o2) { 
78               return ( 
79                       new Double(((SortableProduct) o1).getPriceEa())) 
80                       .compareTo( 
81                               new Double(((SortableProduct) o2).getPriceEa()) 
82                       ); 
83           } 
84       } 
85    
86       /** 
87        * Explicit comparator object to sort by quantity. 
88        */ 
89       private static class QtyComparator implements Comparator { 
90           public int compare(Object o1, Object o2) { 
91               return ( 
92                       new Double(((SortableProduct) o1).getQty())) 
93                       .compareTo( 
94                               new Double(((SortableProduct) o2).getQty()) 
95                       ); 
96           } 
97       } 
98    
99       public int getId() { 
100          return id; 
101      } 
102   
103      public String getName() { 
104          return name; 
105      } 
106   
107      public String getDescr() { 
108          return descr; 
109      } 
110   
111      public int getQty() { 
112          return qty; 
113      } 
114   
115      public double getPriceEa() { 
116          return priceEa; 
117      } 
118   
119      public double getPriceNet() { 
120          return priceNet; 
121      } 
122  }