~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/io/comparator/LastModifiedFileComparator.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-02-21 13:26:43 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080221132643-p4c8f8lhb9rnqnlo
Tags: 1.4-1
* New upstream release
* Bump Standards-Version to 3.7.3
* Bump up debhelper compat to 6
* Replace XS-Vcs headers with Vcs
* debian/patches:
  - remove 01_no_ext_links.dpatch - not required
  - remove 02_no_mkdir_in_homedir.dpatch - not required
* Remove dpatch from Build-Depends
* Update debian/rules and debian/libcommons-io-java-doc.install
  with new target dirs
* debian/copyright: add copyright notice

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.commons.io.comparator;
 
18
 
 
19
import java.io.File;
 
20
import java.io.Serializable;
 
21
import java.util.Comparator;
 
22
 
 
23
/**
 
24
 * Compare the <b>last modified date/time</b> of two files for order 
 
25
 * (see {@link File#lastModified()}).
 
26
 * <p>
 
27
 * This comparator can be used to sort lists or arrays of files
 
28
 * by their last modified date/time.
 
29
 * <p>
 
30
 * Example of sorting a list of files using the
 
31
 * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
 
32
 * <pre>
 
33
 *       List&lt;File&gt; list = ...
 
34
 *       Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
 
35
 * </pre>
 
36
 * <p>
 
37
 * Example of doing a <i>reverse</i> sort of an array of files using the
 
38
 * {@link #LASTMODIFIED_REVERSE} singleton instance:
 
39
 * <pre>
 
40
 *       File[] array = ...
 
41
 *       Arrays.sort(array, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
 
42
 * </pre>
 
43
 * <p>
 
44
 *
 
45
 * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
 
46
 * @since Commons IO 1.4
 
47
 */
 
48
public class LastModifiedFileComparator implements Comparator, Serializable {
 
49
 
 
50
    /** Last modified comparator instance */
 
51
    public static final Comparator LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
 
52
 
 
53
    /** Reverse last modified comparator instance */
 
54
    public static final Comparator LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
 
55
 
 
56
    /**
 
57
     * Compare the last the last modified date/time of two files.
 
58
     * 
 
59
     * @param obj1 The first file to compare
 
60
     * @param obj2 The second file to compare
 
61
     * @return a negative value if the first file's lastmodified date/time
 
62
     * is less than the second, zero if the lastmodified date/time are the
 
63
     * same and a positive value if the first files lastmodified date/time
 
64
     * is greater than the second file.
 
65
     * 
 
66
     */
 
67
    public int compare(Object obj1, Object obj2) {
 
68
        File file1 = (File)obj1;
 
69
        File file2 = (File)obj2;
 
70
        long result = file1.lastModified() - file2.lastModified();
 
71
        if (result < 0) {
 
72
            return -1;
 
73
        } else if (result > 0) {
 
74
            return 1;
 
75
        } else {
 
76
            return 0;
 
77
        }
 
78
    }
 
79
}