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