001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.comparator;
018
019import java.io.File;
020import java.io.Serializable;
021import java.util.Comparator;
022
023import org.apache.commons.io.FileUtils;
024
025/**
026 * Compare the <strong>last modified date/time</strong> of two files for order
027 * (see {@link FileUtils#lastModifiedUnchecked(File)}).
028 * <p>
029 * This comparator can be used to sort lists or arrays of files
030 * by their last modified date/time.
031 * </p>
032 * <p>
033 * Example of sorting a list of files using the
034 * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
035 * </p>
036 * <pre>
037 *       List&lt;File&gt; list = ...
038 *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list);
039 * </pre>
040 * <p>
041 * Example of doing a <em>reverse</em> sort of an array of files using the
042 * {@link #LASTMODIFIED_REVERSE} singleton instance:
043 * </p>
044 * <pre>
045 *       File[] array = ...
046 *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array);
047 * </pre>
048 * <h2>Deprecating Serialization</h2>
049 * <p>
050 * <em>Serialization is deprecated and will be removed in 3.0.</em>
051 * </p>
052 *
053 * @since 1.4
054 */
055public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {
056
057    private static final long serialVersionUID = 7372168004395734046L;
058
059    /** Last modified comparator instance. */
060    public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
061
062    /** Reverse last modified comparator instance. */
063    public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseFileComparator(LASTMODIFIED_COMPARATOR);
064
065    /**
066     * Construct a new instance.
067     */
068    public LastModifiedFileComparator() {
069        // empty
070    }
071
072    /**
073     * Compares the last modified date/time of two files.
074     *
075     * @param file1 The first file to compare.
076     * @param file2 The second file to compare.
077     * @return a negative value if the first file's last modified date/time is less than the second, zero if the last
078     *         modified date/time are the same and a positive value if the first files last modified date/time is
079     *         greater than the second file.
080     */
081    @Override
082    public int compare(final File file1, final File file2) {
083        final long result = FileUtils.lastModifiedUnchecked(file1) - FileUtils.lastModifiedUnchecked(file2);
084        if (result < 0) {
085            return -1;
086        }
087        if (result > 0) {
088            return 1;
089        }
090        return 0;
091    }
092}