KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > report > DateComparator


1 /*
2  * DateComparator.java
3  *
4  * Created on April 13, 2005, 12:16 PM
5  */

6
7 package com.sun.japex.report;
8
9 import java.io.File JavaDoc;
10 import java.util.Comparator JavaDoc;
11 import java.util.Date JavaDoc;
12
13
14 public class DateComparator implements Comparator JavaDoc {
15     public static final int ORDER_DESC = 1;
16     public static final int ORDER_ASC = 2;
17     private int order;
18     /** Creates a new instance of DateComparator */
19     public DateComparator() {
20         order = ORDER_DESC;
21     }
22     public DateComparator(int order) {
23         this.order = order;
24     }
25     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
26         if ( (o1 instanceof File JavaDoc) && (o2 instanceof File JavaDoc) )
27         {
28             long lm1 = ((File JavaDoc)o1).lastModified();
29             long lm2 = ((File JavaDoc)o2).lastModified();
30
31             if ( lm1 < lm2 ) {
32                 return (order == ORDER_DESC) ? -1 : 1;
33             } else if ( lm1 > lm2 ) {
34                 return (order == ORDER_DESC) ? 1 : -1;
35             } else {
36                 return 0;
37             }
38         } else if ( (o1 instanceof Comparable JavaDoc) && (o2 instanceof Comparable JavaDoc) ) {
39             return ((Comparable JavaDoc)o1).compareTo( ((Comparable JavaDoc)o2) );
40         }
41         else
42         {
43             return -1;
44         }
45     }
46     public boolean equals(Object JavaDoc obj) {
47         if ( obj instanceof DateComparator ) {
48             return true;
49         } else {
50             return false;
51         }
52     }
53 }
54
Popular Tags