LastModifiedFileComparator.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.util.Comparator;

  21. import org.apache.commons.io.FileUtils;

  22. /**
  23.  * Compare the <strong>last modified date/time</strong> of two files for order
  24.  * (see {@link FileUtils#lastModifiedUnchecked(File)}).
  25.  * <p>
  26.  * This comparator can be used to sort lists or arrays of files
  27.  * by their last modified date/time.
  28.  * </p>
  29.  * <p>
  30.  * Example of sorting a list of files using the
  31.  * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
  32.  * </p>
  33.  * <pre>
  34.  *       List&lt;File&gt; list = ...
  35.  *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list);
  36.  * </pre>
  37.  * <p>
  38.  * Example of doing a <em>reverse</em> sort of an array of files using the
  39.  * {@link #LASTMODIFIED_REVERSE} singleton instance:
  40.  * </p>
  41.  * <pre>
  42.  *       File[] array = ...
  43.  *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array);
  44.  * </pre>
  45.  * <h2>Deprecating Serialization</h2>
  46.  * <p>
  47.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  48.  * </p>
  49.  *
  50.  * @since 1.4
  51.  */
  52. public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {

  53.     private static final long serialVersionUID = 7372168004395734046L;

  54.     /** Last modified comparator instance. */
  55.     public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();

  56.     /** Reverse last modified comparator instance. */
  57.     public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseFileComparator(LASTMODIFIED_COMPARATOR);

  58.     /**
  59.      * Construct a new instance.
  60.      */
  61.     public LastModifiedFileComparator() {
  62.         // empty
  63.     }

  64.     /**
  65.      * Compares the last modified date/time of two files.
  66.      *
  67.      * @param file1 The first file to compare.
  68.      * @param file2 The second file to compare.
  69.      * @return a negative value if the first file's last modified date/time is less than the second, zero if the last
  70.      *         modified date/time are the same and a positive value if the first files last modified date/time is
  71.      *         greater than the second file.
  72.      */
  73.     @Override
  74.     public int compare(final File file1, final File file2) {
  75.         final long result = FileUtils.lastModifiedUnchecked(file1) - FileUtils.lastModifiedUnchecked(file2);
  76.         if (result < 0) {
  77.             return -1;
  78.         }
  79.         if (result > 0) {
  80.             return 1;
  81.         }
  82.         return 0;
  83.     }
  84. }