    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     */
    017    package org.apache.commons.io.comparator;
    018
    019    import java.io.File;
    020    import java.io.Serializable;
    021    import 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     *       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     *       LastModifiedFileComparator.LASTMODIFIED_REVERSE.sort(array);
    042     * </pre>
    043     * <p>
    044     *
    045     * @version $Revision: 721626 $ $Date: 2008-11-29 04:46:54 +0000 (Sat, 29 Nov 2008) $
    046     * @since Commons IO 1.4
    047     */
    048    public class LastModifiedFileComparator extends AbstractFileComparator implements Serializable {
    049
    050        /** Last modified comparator instance */
    051        public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
    052
    053        /** Reverse last modified comparator instance */
    054        public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
    055
    056        /**
    057         * Compare the last the last modified date/time of two files.
    058         *
    059         * @param file1 The first file to compare
    060         * @param file2 The second file to compare
    061         * @return a negative value if the first file's lastmodified date/time
    062         * is less than the second, zero if the lastmodified date/time are the
    063         * same and a positive value if the first files lastmodified date/time
    064         * is greater than the second file.
    065         *
    066         */
    067        public int compare(File file1, File file2) {
    068            long result = file1.lastModified() - file2.lastModified();
    069            if (result < 0) {
    070                return -1;
    071            } else if (result > 0) {
    072                return 1;
    073            } else {
    074                return 0;
    075            }
    076        }
    077    }