public class TimeComparator implements Comparator
1: // Fig. 19.10: TimeComparator.java
2: // Custom Comparator class that compares two Time2 objects.
3: import java.util.Comparator;
4:
5: public class TimeComparator implements Comparator< Time2 >
6: {
7: public int compare( Time2 time1, Time2 time2 )
8: {
9: int hourCompare = time1.getHour() - time2.getHour(); // compare hour
10:
11: // test the hour first
12: if ( hourCompare != 0 )
13: return hourCompare;
14:
15: int minuteCompare =
16: time1.getMinute() - time2.getMinute(); // compare minute
17:
18: // then test the minute
19: if ( minuteCompare != 0 )
20: return minuteCompare;
21:
22: int secondCompare =
23: time1.getSecond() - time2.getSecond(); // compare second
24:
25: return secondCompare; // return result of comparing seconds
26: } // end method compare
27: } // end class TimeComparator
28:
29: /**************************************************************************
30: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
31: * Pearson Education, Inc. All Rights Reserved. *
32: * *
33: * DISCLAIMER: The authors and publisher of this book have used their *
34: * best efforts in preparing the book. These efforts include the *
35: * development, research, and testing of the theories and programs *
36: * to determine their effectiveness. The authors and publisher make *
37: * no warranty of any kind, expressed or implied, with regard to these *
38: * programs or to the documentation contained in these books. The authors *
39: * and publisher shall not be liable in any event for incidental or *
40: * consequential damages in connection with, or arising out of, the *
41: * furnishing, performance, or use of these programs. *
42: *************************************************************************/