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 two files using the <b>default</b> {@link File#compareTo(File)} method.
025     * <p>
026     * This comparator can be used to sort lists or arrays of files
027     * by using the default file comparison.
028     * <p>
029     * Example of sorting a list of files using the
030     * {@link #DEFAULT_COMPARATOR} singleton instance:
031     * <pre>
032     *       List&lt;File&gt; list = ...
033     *       Collections.sort(list, DefaultFileComparator.DEFAULT_COMPARATOR);
034     * </pre>
035     * <p>
036     * Example of doing a <i>reverse</i> sort of an array of files using the
037     * {@link #DEFAULT_REVERSE} singleton instance:
038     * <pre>
039     *       File[] array = ...
040     *       Arrays.sort(array, DefaultFileComparator.DEFAULT_REVERSE);
041     * </pre>
042     * <p>
043     *
044     * @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
045     * @since Commons IO 1.4
046     */
047    public class DefaultFileComparator implements Comparator, Serializable {
048    
049        /** Singleton default comparator instance */
050        public static final Comparator DEFAULT_COMPARATOR = new DefaultFileComparator();
051    
052        /** Singleton reverse default comparator instance */
053        public static final Comparator DEFAULT_REVERSE = new ReverseComparator(DEFAULT_COMPARATOR);
054    
055        /**
056         * Compare the two files using the {@link File#compareTo(File)} method.
057         * 
058         * @param obj1 The first file to compare
059         * @param obj2 The second file to compare
060         * @return the result of calling file1's
061         * {@link File#compareTo(File)} with file2 as the parameter.
062         */
063        public int compare(Object obj1, Object obj2) {
064            File file1 = (File)obj1;
065            File file2 = (File)obj2;
066            return file1.compareTo(file2);
067        }
068    }