KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jfm > FileComparator


1 package de.jwi.jfm;
2
3 /*
4  * jFM - Java Web File Manager
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jFM.
9  *
10  * jFM is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jFM is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jFM; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25
26 import java.io.File JavaDoc;
27 import java.util.Comparator JavaDoc;
28
29
30 /**
31  * @author Jürgen Weber
32  * Source file created on 08.03.2004
33  */

34 public class FileComparator implements Comparator JavaDoc
35 {
36     public static final int SORT_NAME_UP = 1;
37     public static final int SORT_NAME_DOWN = 2;
38     public static final int SORT_DATE_UP = 3;
39     public static final int SORT_DATE_DOWN = 4;
40     public static final int SORT_SIZE_UP = 5;
41     public static final int SORT_SIZE_DOWN = 6;
42
43     
44     public static final Comparator JavaDoc nameUpInstance = new FileComparator(SORT_NAME_UP);
45     public static final Comparator JavaDoc nameDownInstance = new FileComparator(SORT_NAME_DOWN);
46     public static final Comparator JavaDoc dateUpInstance = new FileComparator(SORT_DATE_UP);
47     public static final Comparator JavaDoc dateDownInstance = new FileComparator(SORT_DATE_DOWN);
48     public static final Comparator JavaDoc sizeUpInstance = new FileComparator(SORT_SIZE_UP);
49     public static final Comparator JavaDoc sizeDownInstance = new FileComparator(SORT_SIZE_DOWN);
50     
51     
52     private int mode;
53     
54     FileComparator(int mode)
55     {
56         this.mode = mode;
57     }
58     
59     public int compare(Object JavaDoc o1,
60             Object JavaDoc o2)
61     {
62         FileWrapper fw1 = (FileWrapper)o1;
63         FileWrapper fw2 = (FileWrapper)o2;
64         
65         File JavaDoc f1 = fw1.getFile();
66         File JavaDoc f2 = fw2.getFile();
67         
68         int rc = 0;
69         
70         if (SORT_NAME_UP == mode)
71         {
72             rc= f1.getName().compareToIgnoreCase(f2.getName());
73         }
74         else if (SORT_NAME_DOWN == mode)
75         {
76             rc= f2.getName().compareToIgnoreCase(f1.getName());
77         }
78         else if (SORT_SIZE_UP == mode)
79         {
80              long l =(f1.length() - f2.length() ) ;
81              rc = l < 0l ? -1 : l == 0l ? 0 : 1;
82         }
83         else if (SORT_SIZE_DOWN == mode)
84         {
85             long l =(f2.length() - f1.length() ) ;
86             rc = l < 0l ? -1 : l == 0l ? 0 : 1;
87         }
88         
89         else if (SORT_DATE_UP == mode)
90         {
91              long l =(f1.lastModified() - f2.lastModified()) ;
92              rc = l < 0l ? -1 : l == 0l ? 0 : 1;
93         }
94         else if (SORT_DATE_DOWN == mode)
95         {
96             long l =(f2.lastModified() - f1.lastModified()) ;
97             rc = l < 0l ? -1 : l == 0l ? 0 : 1;
98         }
99         return rc;
100     }
101 }
102
Popular Tags