View Javadoc

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: 619103 $ $Date: 2008-02-06 19:01:17 +0000 (Wed, 06 Feb 2008) $
46   * @since Commons IO 1.4
47   */
48  public class LastModifiedFileComparator implements Comparator<File>, Serializable {
49  
50      /** Last modified comparator instance */
51      public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
52  
53      /** Reverse last modified comparator instance */
54      public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
55  
56      /**
57       * Compare the last the last modified date/time of two files.
58       * 
59       * @param file1 The first file to compare
60       * @param file2 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(File file1, File file2) {
68          long result = file1.lastModified() - file2.lastModified();
69          if (result < 0) {
70              return -1;
71          } else if (result > 0) {
72              return 1;
73          } else {
74              return 0;
75          }
76      }
77  }