KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > FileDateComparator


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  * Sorts files by date (newest files first).
30  *
31  * @author Luciano Vernaschi
32  */

33 public final class FileDateComparator implements Comparator, Serializable {
34
35   private final boolean forwards;
36
37   public FileDateComparator() {
38     forwards = true;
39   }
40
41   public FileDateComparator(boolean forwards) {
42     this.forwards = forwards;
43   }
44
45   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
46     try {
47       long f1 = ((File) o1).lastModified();
48       long f2 = ((File) o2).lastModified();
49       if (forwards) {
50         if (f1 > f2) {
51           return -1;
52         } else if (f1 < f2) {
53           return 1;
54         }
55       } else {
56         if (f2 > f1) {
57           return -1;
58         } else if (f2 < f1) {
59           return 1;
60         }
61       }
62     } catch (ClassCastException JavaDoc ex) {}
63     return 0;
64   }
65 }
66
Popular Tags