14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.comparator;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.File;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.Serializable;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.Comparator;
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Compare the <b>last modified date/time</b> of two files for order
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (see {@link File#lastModified()}).
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * This comparator can be used to sort lists or arrays of files
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * by their last modified date/time.
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of sorting a list of files using the
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #LASTMODIFIED_COMPARATOR} singleton instance:
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       List&lt;File&gt; list = ...
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Example of doing a <i>reverse</i> sort of an array of files using the
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * {@link #LASTMODIFIED_REVERSE} singleton instance:
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       File[] array = ...
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *       Arrays.sort(array, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.4
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class LastModifiedFileComparator implements Comparator<File>, Serializable {
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Last modified comparator instance */
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** Reverse last modified comparator instance */
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public static final Comparator<File> LASTMODIFIED_REVERSE = new ReverseComparator<File>(LASTMODIFIED_COMPARATOR);
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Compare the last the last modified date/time of two files.
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param obj1 The first file to compare
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param obj2 The second file to compare
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return a negative value if the first file's lastmodified date/time
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * is less than the second, zero if the lastmodified date/time are the
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * same and a positive value if the first files lastmodified date/time
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * is greater than the second file.
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int compare(File file1, File file2) {
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        long result = file1.lastModified() - file2.lastModified();
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (result < 0) {
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            return -1;
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        } else if (result > 0) {
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            return 1;
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        } else {
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            return 0;
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
78