    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 <b>last modified date/time</b> 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 * Example of sorting a list of files using the
    033 * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
    034 * <pre>
    035 *       List&lt;File&gt; list = ...
    036 *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_COMPARATOR).sort(list);
    037 * </pre>
    038 * <p>
    039 * Example of doing a <i>reverse</i> sort of an array of files using the
    040 * {@link #LASTMODIFIED_REVERSE} singleton instance:
    041 * <pre>
    042 *       File[] array = ...
    043 *       ((AbstractFileComparator) LastModifiedFileComparator.LASTMODIFIED_REVERSE).sort(array);
    044 * </pre>
    045 * <p>
    046 *
    047 * @since 1.4
    048 */
    049public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {
    050
    051    private static final long serialVersionUID = 7372168004395734046L;
    052
    053    /** Last modified comparator instance. */
    054    public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
    055
    056    /** Reverse last modified comparator instance. */
    057    public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseFileComparator(LASTMODIFIED_COMPARATOR);
    058
    059    /**
    060     * Compares the last the last modified date/time of two files.
    061     *
    062     * @param file1 The first file to compare.
    063     * @param file2 The second file to compare.
    064     * @return a negative value if the first file's last modified date/time is less than the second, zero if the last
    065     *         modified date/time are the same and a positive value if the first files last modified date/time is
    066     *         greater than the second file.
    067     */
    068    @Override
    069    public int compare(final File file1, final File file2) {
    070        final long result = FileUtils.lastModifiedUnchecked(file1) - FileUtils.lastModifiedUnchecked(file2);
    071        if (result < 0) {
    072            return -1;
    073        }
    074        if (result > 0) {
    075            return 1;
    076        }
    077        return 0;
    078    }
    079}