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  import org.apache.commons.io.FileUtils;
24  
25  /**
26   * Compare the <b>last modified date/time</b> of two files for order
27   * (see {@link FileUtils#lastModifiedUnchecked(File)}).
28   * <p>
29   * This comparator can be used to sort lists or arrays of files
30   * by their last modified date/time.
31   * </p>
32   * <p>
33   * Example of sorting a list of files using the
34   * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
35   * </p>
36   * <pre>
37   *       List&lt;File&gt; list = ...
38   *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list);
39   * </pre>
40   * <p>
41   * Example of doing a <i>reverse</i> sort of an array of files using the
42   * {@link #LASTMODIFIED_REVERSE} singleton instance:
43   * </p>
44   * <pre>
45   *       File[] array = ...
46   *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array);
47   * </pre>
48   * <h2>Deprecating Serialization</h2>
49   * <p>
50   * <em>Serialization is deprecated and will be removed in 3.0.</em>
51   * </p>
52   *
53   * @since 1.4
54   */
55  public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {
56  
57      private static final long serialVersionUID = 7372168004395734046L;
58  
59      /** Last modified comparator instance. */
60      public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
61  
62      /** Reverse last modified comparator instance. */
63      public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseFileComparator(LASTMODIFIED_COMPARATOR);
64  
65      /**
66       * Compares the last modified date/time of two files.
67       *
68       * @param file1 The first file to compare.
69       * @param file2 The second file to compare.
70       * @return a negative value if the first file's last modified date/time is less than the second, zero if the last
71       *         modified date/time are the same and a positive value if the first files last modified date/time is
72       *         greater than the second file.
73       */
74      @Override
75      public int compare(final File file1, final File file2) {
76          final long result = FileUtils.lastModifiedUnchecked(file1) - FileUtils.lastModifiedUnchecked(file2);
77          if (result < 0) {
78              return -1;
79          }
80          if (result > 0) {
81              return 1;
82          }
83          return 0;
84      }
85  }